UNPKG

reka-ui

Version:

Vue port for Radix UI Primitives.

1 lines 16.3 kB
{"version":3,"file":"DateFieldRoot.cjs","sources":["../../src/DateField/DateFieldRoot.vue"],"sourcesContent":["<script lang=\"ts\">\nimport type { DateValue } from '@internationalized/date'\n\nimport type { Ref } from 'vue'\nimport type { PrimitiveProps } from '@/Primitive'\nimport { type Formatter, createContext, isNullish, useDateFormatter, useDirection, useKbd, useLocale } from '@/shared'\nimport {\n type Granularity,\n type HourCycle,\n type SegmentPart,\n type SegmentValueObj,\n getDefaultDate,\n} from '@/shared/date'\nimport { type Matcher, hasTime, isBefore } from '@/date'\nimport { createContent, getSegmentElements, initializeSegmentValues, isSegmentNavigationKey, syncSegmentValues } from '@/shared/date'\nimport type { Direction, FormFieldProps } from '@/shared/types'\n\ntype DateFieldRootContext = {\n locale: Ref<string>\n modelValue: Ref<DateValue | undefined>\n placeholder: Ref<DateValue>\n isDateUnavailable?: Matcher\n isInvalid: Ref<boolean>\n disabled: Ref<boolean>\n readonly: Ref<boolean>\n formatter: Formatter\n hourCycle: HourCycle\n segmentValues: Ref<SegmentValueObj>\n segmentContents: Ref<{ part: SegmentPart, value: string }[]>\n elements: Ref<Set<HTMLElement>>\n focusNext: () => void\n setFocusedElement: (el: HTMLElement) => void\n}\n\nexport interface DateFieldRootProps extends PrimitiveProps, FormFieldProps {\n /** The default value for the calendar */\n defaultValue?: DateValue\n /** The default placeholder date */\n defaultPlaceholder?: DateValue\n /** The placeholder date, which is used to determine what month to display when no date is selected. This updates as the user navigates the calendar and can be used to programmatically control the calendar view */\n placeholder?: DateValue\n /** The controlled checked state of the calendar. Can be bound as `v-model`. */\n modelValue?: DateValue | null\n /** The hour cycle used for formatting times. Defaults to the local preference */\n hourCycle?: HourCycle\n /** The granularity to use for formatting times. Defaults to day if a CalendarDate is provided, otherwise defaults to minute. The field will render segments for each part of the date up to and including the specified granularity */\n granularity?: Granularity\n /** Whether or not to hide the time zone segment of the field */\n hideTimeZone?: boolean\n /** The maximum date that can be selected */\n maxValue?: DateValue\n /** The minimum date that can be selected */\n minValue?: DateValue\n /** The locale to use for formatting dates */\n locale?: string\n /** Whether or not the date field is disabled */\n disabled?: boolean\n /** Whether or not the date field is readonly */\n readonly?: boolean\n /** A function that returns whether or not a date is unavailable */\n isDateUnavailable?: Matcher\n /** Id of the element */\n id?: string\n /** The reading direction of the date field when applicable. <br> If omitted, inherits globally from `ConfigProvider` or assumes LTR (left-to-right) reading mode. */\n dir?: Direction\n}\n\nexport type DateFieldRootEmits = {\n /** Event handler called whenever the model value changes */\n 'update:modelValue': [date: DateValue | undefined]\n /** Event handler called whenever the placeholder value changes */\n 'update:placeholder': [date: DateValue]\n}\n\nexport const [injectDateFieldRootContext, provideDateFieldRootContext]\n = createContext<DateFieldRootContext>('DateFieldRoot')\n</script>\n\n<script setup lang=\"ts\">\nimport { computed, nextTick, onMounted, ref, toRefs, watch } from 'vue'\nimport { Primitive, usePrimitiveElement } from '@/Primitive'\nimport { useVModel } from '@vueuse/core'\nimport { VisuallyHidden } from '@/VisuallyHidden'\n\ndefineOptions({\n inheritAttrs: false,\n})\n\nconst props = withDefaults(defineProps<DateFieldRootProps>(), {\n defaultValue: undefined,\n disabled: false,\n readonly: false,\n placeholder: undefined,\n isDateUnavailable: undefined,\n})\nconst emits = defineEmits<DateFieldRootEmits>()\ndefineSlots<{\n default: (props: {\n /** The current date of the field */\n modelValue: DateValue | undefined\n /** The date field segment contents */\n segments: { part: SegmentPart, value: string }[]\n /** Value if the input is invalid */\n isInvalid: boolean\n }) => any\n}>()\n\nconst { disabled, readonly, isDateUnavailable: propsIsDateUnavailable, granularity, defaultValue, dir: propDir, locale: propLocale } = toRefs(props)\nconst locale = useLocale(propLocale)\nconst dir = useDirection(propDir)\n\nconst formatter = useDateFormatter(locale.value)\nconst { primitiveElement, currentElement: parentElement }\n = usePrimitiveElement()\nconst segmentElements = ref<Set<HTMLElement>>(new Set())\n\nonMounted(() => {\n getSegmentElements(parentElement.value).forEach(item => segmentElements.value.add(item as HTMLElement))\n})\n\nconst modelValue = useVModel(props, 'modelValue', emits, {\n defaultValue: defaultValue.value,\n passive: (props.modelValue === undefined) as false,\n}) as Ref<DateValue>\n\nconst defaultDate = getDefaultDate({\n defaultPlaceholder: props.placeholder,\n granularity: granularity.value,\n defaultValue: modelValue.value,\n locale: props.locale,\n})\n\nconst placeholder = useVModel(props, 'placeholder', emits, {\n defaultValue: props.defaultPlaceholder ?? defaultDate.copy(),\n passive: (props.placeholder === undefined) as false,\n}) as Ref<DateValue>\n\nconst inferredGranularity = computed(() => {\n if (props.granularity)\n return !hasTime(placeholder.value) ? 'day' : props.granularity\n\n return hasTime(placeholder.value) ? 'minute' : 'day'\n})\n\nconst isInvalid = computed(() => {\n if (!modelValue.value)\n return false\n\n if (propsIsDateUnavailable.value?.(modelValue.value))\n return true\n\n if (props.minValue && isBefore(modelValue.value, props.minValue))\n return true\n\n if (props.maxValue && isBefore(props.maxValue, modelValue.value))\n return true\n\n return false\n})\n\nconst initialSegments = initializeSegmentValues(inferredGranularity.value)\n\nconst segmentValues = ref<SegmentValueObj>(modelValue.value ? { ...syncSegmentValues({ value: modelValue.value, formatter }) } : { ...initialSegments })\n\nconst allSegmentContent = computed(() => createContent({\n granularity: inferredGranularity.value,\n dateRef: placeholder.value,\n formatter,\n hideTimeZone: props.hideTimeZone,\n hourCycle: props.hourCycle,\n segmentValues: segmentValues.value,\n locale,\n}))\n\nconst segmentContents = computed(() => allSegmentContent.value.arr)\n\nconst editableSegmentContents = computed(() => segmentContents.value.filter(({ part }) => part !== 'literal'))\n\nwatch(locale, (value) => {\n if (formatter.getLocale() !== value) {\n formatter.setLocale(value)\n // Locale changed, so we need to clear the segment elements and re-get them (different order)\n // Get the focusable elements again on the next tick\n nextTick(() => {\n segmentElements.value.clear()\n getSegmentElements(parentElement.value).forEach(item => segmentElements.value.add(item as HTMLElement))\n })\n }\n})\n\nwatch(modelValue, (_modelValue) => {\n if (!isNullish(_modelValue) && placeholder.value.compare(_modelValue) !== 0) {\n placeholder.value = _modelValue.copy()\n }\n})\n\nwatch([modelValue, locale], ([_modelValue]) => {\n if (!isNullish(_modelValue)) {\n segmentValues.value = { ...syncSegmentValues({ value: _modelValue, formatter }) }\n }\n // If segment has null value, means that user modified it, thus do not reset the segmentValues\n else if (Object.values(segmentValues.value).every(value => value !== null) && isNullish(_modelValue)) {\n segmentValues.value = { ...initialSegments }\n }\n})\n\nconst currentFocusedElement = ref<HTMLElement | null>(null)\n\nconst currentSegmentIndex = computed(() =>\n Array.from(segmentElements.value).findIndex(el =>\n el.getAttribute('data-reka-date-field-segment')\n === currentFocusedElement.value?.getAttribute('data-reka-date-field-segment')))\n\nconst nextFocusableSegment = computed(() => {\n const sign = dir.value === 'rtl' ? -1 : 1\n const nextCondition = sign < 0 ? currentSegmentIndex.value < 0 : currentSegmentIndex.value > segmentElements.value.size - 1\n if (nextCondition)\n return null\n const segmentToFocus = Array.from(segmentElements.value)[currentSegmentIndex.value + sign]\n return segmentToFocus\n})\n\nconst prevFocusableSegment = computed(() => {\n const sign = dir.value === 'rtl' ? -1 : 1\n const prevCondition = sign > 0 ? currentSegmentIndex.value < 0 : currentSegmentIndex.value > segmentElements.value.size - 1\n if (prevCondition)\n return null\n\n const segmentToFocus = Array.from(segmentElements.value)[currentSegmentIndex.value - sign]\n return segmentToFocus\n})\n\nconst kbd = useKbd()\n\nfunction handleKeydown(e: KeyboardEvent) {\n if (!isSegmentNavigationKey(e.key))\n return\n if (e.key === kbd.ARROW_LEFT)\n prevFocusableSegment.value?.focus()\n if (e.key === kbd.ARROW_RIGHT)\n nextFocusableSegment.value?.focus()\n}\n\nfunction setFocusedElement(el: HTMLElement) {\n currentFocusedElement.value = el\n}\n\nprovideDateFieldRootContext({\n isDateUnavailable: propsIsDateUnavailable.value,\n locale,\n modelValue,\n placeholder,\n disabled,\n formatter,\n hourCycle: props.hourCycle,\n readonly,\n segmentValues,\n isInvalid,\n segmentContents: editableSegmentContents,\n elements: segmentElements,\n setFocusedElement,\n focusNext() {\n nextFocusableSegment.value?.focus()\n },\n})\n\ndefineExpose({\n /** Helper to set the focused element inside the DateField */\n setFocusedElement,\n})\n</script>\n\n<template>\n <Primitive\n v-bind=\"$attrs\"\n ref=\"primitiveElement\"\n role=\"group\"\n :aria-disabled=\"disabled ? true : undefined\"\n :data-disabled=\"disabled ? '' : undefined\"\n :data-readonly=\"readonly ? '' : undefined\"\n :data-invalid=\"isInvalid ? '' : undefined\"\n :dir=\"dir\"\n @keydown.left.right=\"handleKeydown\"\n >\n <slot\n :model-value=\"modelValue\"\n :segments=\"segmentContents\"\n :is-invalid=\"isInvalid\"\n />\n\n <VisuallyHidden\n :id=\"id\"\n as=\"input\"\n feature=\"focusable\"\n tabindex=\"-1\"\n :value=\"modelValue ? modelValue.toString() : ''\"\n :name=\"name\"\n :disabled=\"disabled\"\n :required=\"required\"\n @focus=\"Array.from(segmentElements)?.[0]?.focus()\"\n />\n </Primitive>\n</template>\n"],"names":["createContext","toRefs","useLocale","useDirection","useDateFormatter","usePrimitiveElement","ref","onMounted","getSegmentElements","useVModel","getDefaultDate","computed","hasTime","isBefore","initializeSegmentValues","syncSegmentValues","createContent","watch","nextTick","isNullish","useKbd","isSegmentNavigationKey"],"mappings":";;;;;;;;;;;;;;;;;AA0EO,MAAM,CAAC,0BAAA,EAA4B,2BAA2B,CAAA,GACjEA,mCAAoC,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAavD,IAAA,MAAM,KAAQ,GAAA,OAAA;AAOd,IAAA,MAAM,KAAQ,GAAA,MAAA;AAYd,IAAA,MAAM,EAAE,QAAA,EAAU,QAAU,EAAA,iBAAA,EAAmB,sBAAwB,EAAA,WAAA,EAAa,YAAc,EAAA,GAAA,EAAK,OAAS,EAAA,MAAA,EAAQ,UAAW,EAAA,GAAIC,WAAO,KAAK,CAAA;AACnJ,IAAM,MAAA,MAAA,GAASC,2BAAU,UAAU,CAAA;AACnC,IAAM,MAAA,GAAA,GAAMC,iCAAa,O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