Skip to content

Commit 86114b7

Browse files
committed
reworked for allowing multiple quick dates. Added date request to types.
1 parent 718d103 commit 86114b7

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

packages/docs/page-config/ui-elements/date-input/api-description.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default defineApiDescription({
6363
placement: "Sets the placement of the dropdown content. [More about placements](/ui-elements/dropdown#placement-and-offset)",
6464
rangeDelimiter: "The delimiter used when turning model value to string",
6565
trigger: "Action that will triggered when open and close dropdown.",
66-
quickDate: 'When supplied will automatically input the defined date. The default key detected is `t`. The property value must be either `{ date: new Date() } , { date: \'01/01/2024\' key: \'s\' } or just exist with no value. When supplying a custom value, you must supply a `date` but `key` is optional.',
66+
quickDate: 'When supplied will automatically input the defined dates for desired keys. The default key detected is `t`. The property value must be either `[{ date: new Date() }] , [{ date: \'01/01/2024\' key: \'s\' }] or just exist with no value. When supplying a custom values, you must supply a `date` but `key` is optional.',
6767
},
6868
events: {
6969
clear: "Emitted if select value has been cleared",

packages/ui/src/components/va-date-input/VaDateInput.demo.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@
204204
</VbCard>
205205

206206
<VbCard title="Quick date using default date and key">
207-
<va-date-input v-model="quickDateDefault" manual-input label="Start date on 't' press (Enters todays date)" quick-date />
207+
<va-date-input v-model="quickDateDefault" manual-input label="Start date on 't' press (Enters todays date)" quick-dates />
208208
</VbCard>
209209

210210
<VbCard title="Start date on 's' press">
211-
<va-date-input v-model="quickDate" manual-input label="Start date on 's' press (Enters todays date)" :quick-date="{ date: new Date(), key: 's' }" />
211+
<va-date-input v-model="quickDate" manual-input label="Start date on 's' press (Enters todays date) or 'e' (Enters 01/01/2000)" :quick-dates="[{ date: new Date(), key: 's' }, {date: '01/01/2000', key: 'e'}]" />
212212
</VbCard>
213213

214214
<VbCard title="Start date on 't' press with string date for property">
215-
<va-date-input v-model="strQuickDate" manual-input label="Start date on 't' press (Enters 01/01/2022)" :quick-date="{ date: '01/01/2022' }" />
215+
<va-date-input v-model="strQuickDate" manual-input label="Start date on 't' press (Enters 01/01/2022)" :quick-dates="[{ date: '01/01/2022' }]" />
216216
</VbCard>
217217
</VbDemo>
218218
</template>

packages/ui/src/components/va-date-input/VaDateInput.vue

+21-13
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
import { computed, PropType, toRef, toRefs, watch, ref, shallowRef, nextTick, Ref, useAttrs, useSlots } from 'vue'
8585
import omit from 'lodash/omit'
8686
87-
import { filterComponentProps, extractComponentProps, extractComponentEmits } from '../../utils/component-options'
87+
import { filterComponentProps, extractComponentProps, extractComponentEmits, DateRequest } from '../../utils/component-options'
8888
import {
8989
useComponentPresetProp,
9090
useClearable, useClearableEmits, useClearableProps,
@@ -153,7 +153,7 @@ const props = defineProps({
153153
ariaResetLabel: useTranslationProp('$t:resetDate'),
154154
ariaSelectedDateLabel: useTranslationProp('$t:selectedDate'),
155155
156-
quickDate: { type: Object as PropType<{date: Date | string; key?: string}| boolean>, default: () => {}, required: false },
156+
quickDates: { type: Array as PropType<Array<DateRequest>| boolean>, default: () => {}, required: false },
157157
})
158158
159159
const emit = defineEmits([
@@ -274,20 +274,28 @@ const onInputTextChanged = ({ target }: Event) => {
274274
}
275275
276276
const onKeyDown = (event: KeyboardEvent) => {
277-
const defaultKey = 't'
278-
const quickDate = (props.quickDate as any)?.key ?? defaultKey
279-
const shouldInput = !!(event.key.toLocaleLowerCase() === quickDate)
277+
const keyboardKey = event.key.toLocaleLowerCase()
278+
const isAlpha = !!keyboardKey.match(/^[A-Za-z]$|^$/)
280279
281-
if (event.key.match(/^[a-zA-Z]+$/)) {
280+
if (isAlpha) {
282281
event.preventDefault()
283-
valueComputed.value = props.clearValue
284-
}
285282
286-
if (shouldInput) {
287-
if (typeof props.quickDate === 'string') {
288-
valueComputed.value = parseDateInputValue(new Date().toString())
289-
} else {
290-
valueComputed.value = typeof ((props.quickDate as any).date) === 'string' ? parseDateInputValue((props.quickDate as any).date as string) : parseDateInputValue((props.quickDate as any).date.toString())
283+
const defaultKey = 't'
284+
const today = new Date().toString()
285+
286+
let shouldInput = false
287+
let keyProps
288+
if (Array.isArray(props.quickDates)) {
289+
keyProps = props.quickDates.length === 1 && !props.quickDates[0]?.key ? props.quickDates[0] : props.quickDates.find(d => d.key?.toLowerCase() === keyboardKey.toLowerCase())
290+
if (keyProps) { shouldInput = true }
291+
} else if (keyboardKey === defaultKey) { shouldInput = true }
292+
293+
if (shouldInput) {
294+
if (keyboardKey === defaultKey && !keyProps) {
295+
valueComputed.value = parseDateInputValue(today)
296+
} else if (keyProps) {
297+
valueComputed.value = typeof (keyProps.date) === 'string' ? parseDateInputValue(keyProps.date as string) : parseDateInputValue(keyProps.date.toString())
298+
}
291299
}
292300
}
293301
}

packages/ui/src/utils/component-options/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ export type ExtractComponentProps<T> = true extends boolean ? ExtractDefineCompo
4545
export type ExtractComponentEmits<T> = ComponentEmit<T>
4646

4747
export type ExtractComponentPropTypes<T extends DefineComponentOptions> = ComponentProps<T>
48+
49+
export type DateRequest = {date: Date | string, key?: string}

0 commit comments

Comments
 (0)