import { createContext } from "../shared/createContext.js"; import { isNullish } from "../shared/nullish.js"; import { getDefaultDate, hasTime, isBefore, isSameDateSelection, isSameDateValue } from "../date/comparators.js"; import { getInputType, normalizeDateStep, normalizeHourCycle, normalizeInputValue } from "../date/utils.js"; import { useDateFormatter } from "../shared/useDateFormatter.js"; import { useDirection } from "../shared/useDirection.js"; import { useKbd } from "../shared/useKbd.js"; import { useLocale } from "../shared/useLocale.js"; import { Primitive } from "../Primitive/Primitive.js"; import { usePrimitiveElement } from "../Primitive/usePrimitiveElement.js"; import { VisuallyHidden_default } from "../VisuallyHidden/VisuallyHidden.js"; import { createContent, initializeSegmentValues, syncSegmentValues } from "../date/parser.js"; import { getSegmentElements, isSegmentNavigationKey } from "../date/segment.js"; import { useSegmentNavigation } from "../date/useSegmentNavigation.js"; import { injectFieldRootContext } from "../Field/FieldRoot.js"; import { computed, createBlock, createVNode, defineComponent, mergeProps, nextTick, onBeforeUnmount, onMounted, openBlock, ref, renderSlot, toRefs, unref, useAttrs, watch, withCtx, withKeys } from "vue"; import { useVModel } from "@vueuse/core"; //#region src/DateField/DateFieldRoot.vue?vue&type=script&setup=true&lang.ts const [injectDateFieldRootContext, provideDateFieldRootContext] = /*#__PURE__*/ createContext("DateFieldRoot"); var DateFieldRoot_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({ inheritAttrs: false, __name: "DateFieldRoot", props: { defaultValue: { type: null, required: false, default: void 0 }, defaultPlaceholder: { type: null, required: false }, placeholder: { type: null, required: false, default: void 0 }, modelValue: { type: null, required: false }, hourCycle: { type: null, required: false }, step: { type: Object, required: false }, stepSnapping: { type: Boolean, required: false, default: false }, granularity: { type: String, required: false }, hideTimeZone: { type: Boolean, required: false }, maxValue: { type: null, required: false }, minValue: { type: null, required: false }, locale: { type: String, required: false }, disabled: { type: Boolean, required: false, default: false }, readonly: { type: Boolean, required: false, default: false }, isDateUnavailable: { type: Function, required: false, default: void 0 }, id: { type: String, required: false }, dir: { type: String, required: false }, asChild: { type: Boolean, required: false }, as: { type: null, required: false }, name: { type: String, required: false }, required: { type: Boolean, required: false } }, emits: ["update:modelValue", "update:placeholder"], setup(__props, { expose: __expose, emit: __emit }) { const props = __props; const emits = __emit; const { disabled: propDisabled, readonly, isDateUnavailable: propsIsDateUnavailable, granularity, defaultValue, stepSnapping, dir: propDir, locale: propLocale } = toRefs(props); const locale = useLocale(propLocale); const dir = useDirection(propDir); const fieldContext = injectFieldRootContext(null); const disabled = computed(() => Boolean(propDisabled.value || fieldContext?.disabled.value)); const resolvedName = computed(() => props.name ?? fieldContext?.name.value); const resolvedRequired = computed(() => fieldContext ? props.required || fieldContext.required.value : props.required); const formatter = useDateFormatter(locale.value, { hourCycle: normalizeHourCycle(props.hourCycle) }); const { primitiveElement, currentElement: parentElement } = usePrimitiveElement(); const segmentElements = ref(/* @__PURE__ */ new Set()); onMounted(() => { getSegmentElements(parentElement.value).forEach((item) => segmentElements.value.add(item)); }); const modelValue = useVModel(props, "modelValue", emits, { defaultValue: defaultValue.value, passive: props.modelValue === void 0 }); const defaultDate = getDefaultDate({ defaultPlaceholder: props.placeholder, granularity: granularity.value, defaultValue: modelValue.value, locale: props.locale }); const placeholder = useVModel(props, "placeholder", emits, { defaultValue: props.defaultPlaceholder ?? defaultDate.copy(), passive: props.placeholder === void 0 }); const step = computed(() => normalizeDateStep(props)); const inferredGranularity = computed(() => { if (props.granularity) return !hasTime(placeholder.value) ? "day" : props.granularity; return hasTime(placeholder.value) ? "minute" : "day"; }); const isInvalid = computed(() => { if (!modelValue.value) return false; if (propsIsDateUnavailable.value?.(modelValue.value)) return true; if (props.minValue && isBefore(modelValue.value, props.minValue)) return true; if (props.maxValue && isBefore(props.maxValue, modelValue.value)) return true; return false; }); const initialSegments = initializeSegmentValues(inferredGranularity.value); const segmentValues = ref(modelValue.value ? { ...syncSegmentValues({ value: modelValue.value, formatter }) } : { ...initialSegments }); const allSegmentContent = computed(() => createContent({ granularity: inferredGranularity.value, dateRef: placeholder.value, formatter, hideTimeZone: props.hideTimeZone, hourCycle: props.hourCycle, segmentValues: segmentValues.value, locale })); const segmentContents = computed(() => allSegmentContent.value.arr); const editableSegmentContents = computed(() => segmentContents.value.filter(({ part }) => part !== "literal")); watch(locale, (value) => { if (formatter.getLocale() !== value) { formatter.setLocale(value); nextTick(() => { segmentElements.value.clear(); getSegmentElements(parentElement.value).forEach((item) => segmentElements.value.add(item)); }); } }); watch(modelValue, (value, previous) => { if (isSameDateSelection(previous, value, isSameDateValue)) return; if (!isNullish(value) && !isSameDateValue(placeholder.value, value)) placeholder.value = value.copy(); }); watch([modelValue, locale], ([_modelValue]) => { if (!isNullish(_modelValue)) segmentValues.value = { ...syncSegmentValues({ value: _modelValue, formatter }) }; else if (Object.values(segmentValues.value).every((value) => value !== null) && isNullish(_modelValue)) segmentValues.value = { ...initialSegments }; }); const currentFocusedElement = ref(null); const { nextFocusableSegment, prevFocusableSegment, focusNext } = useSegmentNavigation({ segmentElements, currentFocusedElement, dir, segmentAttributes: ["data-reka-date-field-segment"] }); const inputType = computed(() => getInputType(inferredGranularity.value)); const inputValue = computed(() => normalizeInputValue(modelValue.value, inferredGranularity.value)); const inputMaxValue = computed(() => props.maxValue ? normalizeInputValue(props.maxValue, inferredGranularity.value) : void 0); const inputMinValue = computed(() => props.minValue ? normalizeInputValue(props.minValue, inferredGranularity.value) : void 0); const kbd = useKbd(); function handleKeydown(e) { if (e.isComposing) return; if (!isSegmentNavigationKey(e.key)) return; if (e.key === kbd.ARROW_LEFT) prevFocusableSegment.value?.focus(); if (e.key === kbd.ARROW_RIGHT) nextFocusableSegment.value?.focus(); } function setFocusedElement(el) { currentFocusedElement.value = el; } const attrs = useAttrs(); function getFieldAriaAttrs() { const mergeIds = (consumerValue, fieldValue) => [consumerValue, fieldValue].filter(Boolean).join(" ") || void 0; return { "aria-labelledby": mergeIds(attrs["aria-labelledby"], fieldContext?.labelId.value), "aria-describedby": mergeIds(attrs["aria-describedby"], fieldContext?.describedBy.value), "aria-invalid": attrs["aria-invalid"] ?? (fieldContext?.invalid.value || void 0) }; } const nativeInputId = computed(() => props.id ?? fieldContext?.fieldId.value); const nativeInput = ref(); let unregisterControl; onMounted(() => { unregisterControl = fieldContext?.registerControl({ id: () => nativeInputId.value, element: () => Array.from(segmentElements.value)[0], getValue: () => modelValue.value, validityElement: () => nativeInput.value?.$el }); }); onBeforeUnmount(() => unregisterControl?.()); const isFocusWithin = ref(false); watch(modelValue, (value) => { if (isFocusWithin.value) fieldContext?.handleControlInput({ value }); else fieldContext?.reportControlState({ filled: !isNullish(value) }); }, { flush: "post" }); function handleFocusin() { if (!isFocusWithin.value) fieldContext?.handleControlFocus(); isFocusWithin.value = true; } function handleFocusout(event) { if (parentElement.value?.contains(event.relatedTarget)) return; isFocusWithin.value = false; fieldContext?.handleControlBlur(); } provideDateFieldRootContext({ isDateUnavailable: propsIsDateUnavailable.value, locale, modelValue, placeholder, disabled, formatter, hourCycle: props.hourCycle, step, stepSnapping, readonly, segmentValues, isInvalid, segmentContents: editableSegmentContents, elements: segmentElements, setFocusedElement, focusNext }); __expose({ setFocusedElement }); return (_ctx, _cache) => { return openBlock(), createBlock(unref(Primitive), mergeProps({ ...unref(fieldContext)?.dataAttributes.value, ..._ctx.$attrs, ...getFieldAriaAttrs() }, { ref_key: "primitiveElement", ref: primitiveElement, role: "group", "aria-disabled": disabled.value ? true : void 0, "data-disabled": disabled.value ? "" : void 0, "data-readonly": unref(readonly) ? "" : void 0, "data-invalid": isInvalid.value || unref(fieldContext)?.invalid.value ? "" : void 0, dir: unref(dir), onKeydown: withKeys(handleKeydown, ["left", "right"]), onFocusin: handleFocusin, onFocusout: handleFocusout }), { default: withCtx(() => [renderSlot(_ctx.$slots, "default", { modelValue: unref(modelValue), segments: segmentContents.value, isInvalid: isInvalid.value }), createVNode(unref(VisuallyHidden_default), { id: nativeInputId.value, ref_key: "nativeInput", ref: nativeInput, as: "input", type: inputType.value, feature: "focusable", "aria-hidden": "true", tabindex: "-1", value: inputValue.value, name: resolvedName.value, disabled: disabled.value, required: resolvedRequired.value, max: inputMaxValue.value, min: inputMinValue.value, onFocus: _cache[0] || (_cache[0] = ($event) => Array.from(segmentElements.value)?.[0]?.focus()) }, null, 8, [ "id", "type", "value", "name", "disabled", "required", "max", "min" ])]), _: 3 }, 16, [ "aria-disabled", "data-disabled", "data-readonly", "data-invalid", "dir" ]); }; } }); //#endregion //#region src/DateField/DateFieldRoot.vue var DateFieldRoot_default = DateFieldRoot_vue_vue_type_script_setup_true_lang_default; //#endregion export { DateFieldRoot_default, injectDateFieldRootContext }; //# sourceMappingURL=DateFieldRoot.js.map