|
| 1 | +<script lang="ts"> |
| 2 | +import type { TimeFieldRootProps, TimeFieldRootEmits } from 'reka-ui' |
| 3 | +import type { AppConfig } from '@nuxt/schema' |
| 4 | +import theme from '#build/ui/input-time' |
| 5 | +import type { ComponentConfig } from '../types/tv' |
| 6 | +
|
| 7 | +type InputTime = ComponentConfig<typeof theme, AppConfig, 'inputTime'> |
| 8 | +
|
| 9 | +export interface InputTimeProps extends Omit<TimeFieldRootProps, 'as' | 'locale' | 'dir'> { |
| 10 | + /** |
| 11 | + * The element or component this component should render as. |
| 12 | + * @defaultValue 'div' |
| 13 | + */ |
| 14 | + as?: any |
| 15 | + color?: InputTime['variants']['color'] |
| 16 | + variant?: InputTime['variants']['variant'] |
| 17 | + size?: InputTime['variants']['size'] |
| 18 | + /** Highlight the ring color like a focus state. */ |
| 19 | + highlight?: boolean |
| 20 | + autofocus?: boolean |
| 21 | + autofocusDelay?: number |
| 22 | + /** |
| 23 | + * The locale to use for formatting and parsing numbers. |
| 24 | + * @defaultValue UApp.locale.code |
| 25 | + */ |
| 26 | + locale?: string |
| 27 | + class?: any |
| 28 | + ui?: InputTime['slots'] |
| 29 | +} |
| 30 | +
|
| 31 | +export interface InputTimeEmits extends TimeFieldRootEmits { |
| 32 | + change: [event: Event] |
| 33 | + blur: [event: FocusEvent] |
| 34 | + focus: [event: FocusEvent] |
| 35 | +} |
| 36 | +
|
| 37 | +export interface InputTimeSlots { |
| 38 | + leading(props: { ui: InputTime['ui'] }): any |
| 39 | + default(props: { ui: InputTime['ui'] }): any |
| 40 | + trailing(props: { ui: InputTime['ui'] }): any |
| 41 | +} |
| 42 | +</script> |
| 43 | + |
| 44 | +<script setup lang="ts"> |
| 45 | +import type { ComponentPublicInstance } from 'vue' |
| 46 | +import { computed, onMounted, ref } from 'vue' |
| 47 | +import { TimeFieldRoot, TimeFieldInput, useForwardPropsEmits, Primitive } from 'reka-ui' |
| 48 | +import { reactivePick } from '@vueuse/core' |
| 49 | +import { useAppConfig } from '#imports' |
| 50 | +import { useFieldGroup } from '../composables/useFieldGroup' |
| 51 | +import { useComponentIcons } from '../composables/useComponentIcons' |
| 52 | +import { useFormField } from '../composables/useFormField' |
| 53 | +import { useLocale } from '../composables/useLocale' |
| 54 | +import { tv } from '../utils/tv' |
| 55 | +
|
| 56 | +const props = withDefaults(defineProps<InputTimeProps>(), { |
| 57 | + autofocusDelay: 0 |
| 58 | +}) |
| 59 | +const emits = defineEmits<InputTimeEmits>() |
| 60 | +const slots = defineSlots<InputTimeSlots>() |
| 61 | +
|
| 62 | +const { code: codeLocale, dir } = useLocale() |
| 63 | +const appConfig = useAppConfig() as InputTime['AppConfig'] |
| 64 | +
|
| 65 | +const rootProps = useForwardPropsEmits(reactivePick(props, 'disabled', 'id', 'name', 'required'), emits) |
| 66 | +
|
| 67 | +const { emitFormBlur, emitFormFocus, emitFormChange, emitFormInput, id, color, size: formGroupSize, name, highlight, disabled, ariaAttrs } = useFormField<InputTimeProps>(props) |
| 68 | +const { orientation, size: fieldGroupSize } = useFieldGroup<InputTimeProps>(props) |
| 69 | +const { isLeading, isTrailing, leadingIconName, trailingIconName } = useComponentIcons(props) |
| 70 | +
|
| 71 | +const locale = computed(() => props.locale || codeLocale.value) |
| 72 | +const inputSize = computed(() => fieldGroupSize.value || formGroupSize.value) |
| 73 | +
|
| 74 | +const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.inputTime || {}) })({ |
| 75 | + color: color.value, |
| 76 | + variant: props.variant, |
| 77 | + size: inputSize.value, |
| 78 | + highlight: highlight.value, |
| 79 | + trailing: isTrailing.value || !!slots.trailing, |
| 80 | + fieldGroup: orientation.value |
| 81 | +})) |
| 82 | +
|
| 83 | +const inputsRef = ref<ComponentPublicInstance[]>([]) |
| 84 | +
|
| 85 | +function onUpdate(value: any) { |
| 86 | + // @ts-expect-error - 'target' does not exist in type 'EventInit' |
| 87 | + const event = new Event('change', { target: { value } }) |
| 88 | + emits('change', event) |
| 89 | +
|
| 90 | + emitFormChange() |
| 91 | + emitFormInput() |
| 92 | +} |
| 93 | +
|
| 94 | +function onBlur(event: FocusEvent) { |
| 95 | + emitFormBlur() |
| 96 | + emits('blur', event) |
| 97 | +} |
| 98 | +
|
| 99 | +function onFocus(event: FocusEvent) { |
| 100 | + emitFormFocus() |
| 101 | + emits('focus', event) |
| 102 | +} |
| 103 | +
|
| 104 | +function autoFocus() { |
| 105 | + if (props.autofocus) { |
| 106 | + inputsRef.value[0]?.$el?.focus() |
| 107 | + } |
| 108 | +} |
| 109 | +
|
| 110 | +onMounted(() => { |
| 111 | + setTimeout(() => { |
| 112 | + autoFocus() |
| 113 | + }, props.autofocusDelay) |
| 114 | +}) |
| 115 | +
|
| 116 | +defineExpose({ |
| 117 | + inputsRef |
| 118 | +}) |
| 119 | +</script> |
| 120 | + |
| 121 | +<template> |
| 122 | + <Primitive :as="as" :class="ui.root({ class: [props.class, props.ui?.root] })"> |
| 123 | + <TimeFieldRoot |
| 124 | + v-bind="{ ...rootProps, ...ariaAttrs }" |
| 125 | + :id="id" |
| 126 | + v-slot="{ segments }" |
| 127 | + :model-value="modelValue" |
| 128 | + :default-value="defaultValue" |
| 129 | + :default-placeholder="defaultPlaceholder" |
| 130 | + :placeholder="placeholder" |
| 131 | + :required="required" |
| 132 | + :disabled="disabled" |
| 133 | + :locale="locale" |
| 134 | + :name="name" |
| 135 | + :dir="dir" |
| 136 | + :class="ui.base({ class: [props.ui?.base] })" |
| 137 | + @update:model-value="onUpdate" |
| 138 | + @blur="onBlur" |
| 139 | + @focus="onFocus" |
| 140 | + > |
| 141 | + <TimeFieldInput |
| 142 | + v-for="(segment, index) in segments" |
| 143 | + :key="segment.part" |
| 144 | + :ref="el => (inputsRef[index] = el as ComponentPublicInstance)" |
| 145 | + :part="segment.part" |
| 146 | + :class="ui.segment({ class: props.ui?.segment })" |
| 147 | + > |
| 148 | + {{ segment.value }} |
| 149 | + </TimeFieldInput> |
| 150 | + |
| 151 | + <slot :ui="ui" /> |
| 152 | + |
| 153 | + <span v-if="isLeading || !!slots.leading" :class="ui.leading({ class: props.ui?.leading })"> |
| 154 | + <slot name="leading" :ui="ui"> |
| 155 | + <UIcon v-if="isLeading && leadingIconName" :name="leadingIconName" :class="ui.leadingIcon({ class: props.ui?.leadingIcon })" /> |
| 156 | + </slot> |
| 157 | + </span> |
| 158 | + |
| 159 | + <span v-if="isTrailing || !!slots.trailing" :class="ui.trailing({ class: props.ui?.trailing })"> |
| 160 | + <slot name="trailing" :ui="ui"> |
| 161 | + <UIcon v-if="trailingIconName" :name="trailingIconName" :class="ui.trailingIcon({ class: props.ui?.trailingIcon })" /> |
| 162 | + </slot> |
| 163 | + </span> |
| 164 | + </TimeFieldRoot> |
| 165 | + </Primitive> |
| 166 | +</template> |
0 commit comments