|
13 | 13 | <slot name="action-select" v-if="$slots['action-select']" :value="internalModelValue" />
|
14 | 14 | <template v-if="!$slots['action-select']">
|
15 | 15 | <span
|
16 |
| - v-if="!config.inline" |
| 16 | + v-if="!inline" |
17 | 17 | class="dp__action dp__cancel"
|
18 | 18 | ref="cancelButtonRef"
|
19 | 19 | tabindex="0"
|
20 | 20 | @click="$emit('close-picker')"
|
21 | 21 | @keydown.enter="$emit('close-picker')"
|
22 | 22 | @keydown.space="$emit('close-picker')"
|
23 |
| - >{{ config.cancelText }}</span |
| 23 | + >{{ cancelText }}</span |
24 | 24 | >
|
25 | 25 | <span
|
26 | 26 | :class="selectClass"
|
|
30 | 30 | @click="selectDate"
|
31 | 31 | data-test="select-button"
|
32 | 32 | ref="selectButtonRef"
|
33 |
| - >{{ config.selectText }}</span |
| 33 | + >{{ selectText }}</span |
34 | 34 | >
|
35 | 35 | </template>
|
36 | 36 | </div>
|
|
41 | 41 | import { computed, onMounted, ref } from 'vue';
|
42 | 42 |
|
43 | 43 | import { convertType, unrefElement } from '@/utils/util';
|
44 |
| - import { useArrowNavigation, useState, useUtils } from '@/components/composables'; |
| 44 | + import { useArrowNavigation, useUtils } from '@/components/composables'; |
| 45 | + import { MergedProps } from '@/utils/props'; |
45 | 46 |
|
46 | 47 | import type { PropType } from 'vue';
|
47 |
| -
|
48 |
| - const { config, internalModelValue } = useState(); |
49 |
| - const { formatDate, getDate, isDateAfter, isDateBefore, isValidTime } = useUtils(); |
50 |
| - const { buildMatrix } = useArrowNavigation(); |
| 48 | + import type { InternalModuleValue } from '@/interfaces'; |
51 | 49 |
|
52 | 50 | const emit = defineEmits(['close-picker', 'select-date', 'invalid-select']);
|
53 | 51 |
|
54 | 52 | const props = defineProps({
|
55 | 53 | calendarWidth: { type: Number as PropType<number>, default: 0 },
|
56 | 54 | menuMount: { type: Boolean as PropType<boolean>, default: false },
|
| 55 | + internalModelValue: { type: [Date, Array] as PropType<InternalModuleValue>, default: null }, |
| 56 | + ...MergedProps, |
57 | 57 | });
|
| 58 | +
|
| 59 | + const { formatDate, getDate, isDateAfter, isDateBefore, isValidTime } = useUtils(props); |
| 60 | + const { buildMatrix } = useArrowNavigation(); |
| 61 | +
|
58 | 62 | const cancelButtonRef = ref(null);
|
59 | 63 | const selectButtonRef = ref(null);
|
60 | 64 |
|
61 | 65 | onMounted(() => {
|
62 |
| - if (config.value.arrowNavigation) { |
| 66 | + if (props.arrowNavigation) { |
63 | 67 | buildMatrix([unrefElement(cancelButtonRef), unrefElement(selectButtonRef)] as HTMLElement[], 'actionRow');
|
64 | 68 | }
|
65 | 69 | });
|
66 | 70 |
|
67 | 71 | const validDateRange = computed(() => {
|
68 |
| - return config.value.range && !config.value.partialRange && internalModelValue.value |
69 |
| - ? (internalModelValue.value as Date[]).length === 2 |
| 72 | + return props.range && !props.partialRange && props.internalModelValue |
| 73 | + ? (props.internalModelValue as Date[]).length === 2 |
70 | 74 | : true;
|
71 | 75 | });
|
72 | 76 |
|
|
77 | 81 | }));
|
78 | 82 |
|
79 | 83 | const isTimeValid = computed((): boolean => {
|
80 |
| - if (!config.value.enableTimePicker || config.value.ignoreTimeValidation) return true; |
81 |
| - return isValidTime(internalModelValue.value); |
| 84 | + if (!props.enableTimePicker || props.ignoreTimeValidation) return true; |
| 85 | + return isValidTime(props.internalModelValue); |
82 | 86 | });
|
83 | 87 |
|
84 | 88 | const isMonthValid = computed((): boolean => {
|
85 |
| - if (!config.value.monthPicker) return true; |
86 |
| - return isMonthWithinRange(internalModelValue.value as Date); |
| 89 | + if (!props.monthPicker) return true; |
| 90 | + return isMonthWithinRange(props.internalModelValue as Date); |
87 | 91 | });
|
88 | 92 |
|
89 | 93 | const handleCustomPreviewFormat = () => {
|
90 |
| - const formatFn = config.value.previewFormat as (val: Date | Date[]) => string | string[]; |
| 94 | + const formatFn = props.previewFormat as (val: Date | Date[]) => string | string[]; |
91 | 95 |
|
92 |
| - if (config.value.timePicker) return formatFn(convertType(internalModelValue.value)); |
| 96 | + if (props.timePicker) return formatFn(convertType(props.internalModelValue)); |
93 | 97 |
|
94 |
| - if (config.value.monthPicker) return formatFn(convertType(internalModelValue.value as Date)); |
| 98 | + if (props.monthPicker) return formatFn(convertType(props.internalModelValue as Date)); |
95 | 99 |
|
96 |
| - return formatFn(convertType(internalModelValue.value)); |
| 100 | + return formatFn(convertType(props.internalModelValue)); |
97 | 101 | };
|
98 | 102 |
|
99 | 103 | const formatRangeDate = () => {
|
100 |
| - const dates = internalModelValue.value as Date[]; |
101 |
| - if (config.value.multiCalendars > 0) { |
| 104 | + const dates = props.internalModelValue as Date[]; |
| 105 | + if (props.multiCalendars > 0) { |
102 | 106 | return `${formatDate(dates[0])} - ${formatDate(dates[1])}`;
|
103 | 107 | }
|
104 | 108 | return [formatDate(dates[0]), formatDate(dates[1])];
|
105 | 109 | };
|
106 | 110 |
|
107 | 111 | const previewValue = computed((): string | string[] => {
|
108 |
| - if (!internalModelValue.value || !props.menuMount) return ''; |
109 |
| - if (typeof config.value.previewFormat === 'string') { |
110 |
| - if (Array.isArray(internalModelValue.value)) { |
111 |
| - if (internalModelValue.value.length === 2 && internalModelValue.value[1]) { |
| 112 | + if (!props.internalModelValue || !props.menuMount) return ''; |
| 113 | + if (typeof props.previewFormat === 'string') { |
| 114 | + if (Array.isArray(props.internalModelValue)) { |
| 115 | + if (props.internalModelValue.length === 2 && props.internalModelValue[1]) { |
112 | 116 | return formatRangeDate();
|
113 | 117 | }
|
114 |
| - if (config.value.multiDates) { |
115 |
| - return internalModelValue.value.map((date) => `${formatDate(date)}`); |
| 118 | + if (props.multiDates) { |
| 119 | + return props.internalModelValue.map((date) => `${formatDate(date)}`); |
116 | 120 | }
|
117 |
| - if (config.value.modelAuto) { |
118 |
| - return `${formatDate(internalModelValue.value[0])}`; |
| 121 | + if (props.modelAuto) { |
| 122 | + return `${formatDate(props.internalModelValue[0])}`; |
119 | 123 | }
|
120 |
| - return `${formatDate(internalModelValue.value[0])} -`; |
| 124 | + return `${formatDate(props.internalModelValue[0])} -`; |
121 | 125 | }
|
122 |
| - return formatDate(internalModelValue.value); |
| 126 | + return formatDate(props.internalModelValue); |
123 | 127 | }
|
124 | 128 | return handleCustomPreviewFormat();
|
125 | 129 | });
|
126 | 130 |
|
127 | 131 | const isMonthWithinRange = (date: Date | string): boolean => {
|
128 |
| - if (!config.value.monthPicker) return true; |
| 132 | + if (!props.monthPicker) return true; |
129 | 133 | let valid = true;
|
130 |
| - if (config.value.minDate && config.value.maxDate) { |
| 134 | + if (props.minDate && props.maxDate) { |
131 | 135 | return (
|
132 |
| - isDateAfter(getDate(date), getDate(config.value.minDate)) && |
133 |
| - isDateBefore(getDate(date), getDate(config.value.maxDate)) |
| 136 | + isDateAfter(getDate(date), getDate(props.minDate)) && |
| 137 | + isDateBefore(getDate(date), getDate(props.maxDate)) |
134 | 138 | );
|
135 | 139 | }
|
136 |
| - if (config.value.minDate) { |
137 |
| - valid = isDateAfter(getDate(date), getDate(config.value.minDate)); |
| 140 | + if (props.minDate) { |
| 141 | + valid = isDateAfter(getDate(date), getDate(props.minDate)); |
138 | 142 | }
|
139 |
| - if (config.value.maxDate) { |
140 |
| - valid = isDateBefore(getDate(date), getDate(config.value.maxDate)); |
| 143 | + if (props.maxDate) { |
| 144 | + valid = isDateBefore(getDate(date), getDate(props.maxDate)); |
141 | 145 | }
|
142 | 146 |
|
143 | 147 | return valid;
|
|
0 commit comments