-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VTimeInput.tsx
146 lines (122 loc) Β· 4.08 KB
/
VTimeInput.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Components
import { VMenu } from '@/components/VMenu/VMenu'
import { makeVTextFieldProps, VTextField } from '@/components/VTextField/VTextField'
import { makeVConfirmEditProps, VConfirmEdit } from '@/labs/VConfirmEdit/VConfirmEdit'
import { makeVTimePickerProps, VTimePicker } from '@/labs/VTimePicker/VTimePicker'
// Composables
import { useDate } from '@/composables/date'
import { makeFocusProps, useFocus } from '@/composables/focus'
import { useLocale } from '@/composables/locale'
import { useProxiedModel } from '@/composables/proxiedModel'
// Utilities
import { computed, shallowRef } from 'vue'
import { genericComponent, omit, propsFactory, useRender, wrapInArray } from '@/util'
// Types
export interface VTimeInputSlots {
default: never
}
export const makeVTimeInputProps = propsFactory({
hideActions: Boolean,
...makeFocusProps(),
...makeVConfirmEditProps(),
...makeVTextFieldProps({
placeholder: 'hh:mm',
prependIcon: '$clock',
}),
...omit(makeVTimePickerProps({
weeksInMonth: 'dynamic' as const,
hideHeader: true,
}), ['active']),
}, 'VTimeInput')
export const VTimeInput = genericComponent()({
name: 'VTimeInput',
props: makeVTimeInputProps(),
emits: {
'update:modelValue': (val: string) => true,
},
setup (props, { slots }) {
const { t } = useLocale()
const adapter = useDate()
const { isFocused, focus, blur } = useFocus(props)
const model = useProxiedModel(props, 'modelValue', props.multiple ? [] : null)
const menu = shallowRef(false)
const display = computed(() => {
const value = wrapInArray(model.value)
if (!value.length) return null
return adapter.isValid(model.value) ? adapter.format(model.value, 'keyboardDate') : ''
})
function onKeydown (e: KeyboardEvent) {
if (e.key !== 'Enter') return
if (!menu.value || !isFocused.value) {
menu.value = true
return
}
const target = e.target as HTMLInputElement
model.value = adapter.date(target.value)
}
function onClick (e: MouseEvent) {
e.preventDefault()
e.stopPropagation()
menu.value = true
}
function onSave () {
menu.value = false
}
useRender(() => {
const confirmEditProps = VConfirmEdit.filterProps(props)
const timePickerProps = VTimePicker.filterProps(omit(props, ['active']))
const textFieldProps = VTextField.filterProps(props)
return (
<VTextField
{ ...textFieldProps }
modelValue={ display.value }
onKeydown={ onKeydown }
focused={ menu.value || isFocused.value }
onFocus={ focus }
onBlur={ blur }
onClick:control={ onClick }
onClick:prepend={ onClick }
>
<VMenu
v-model={ menu.value }
activator="parent"
min-width="0"
closeOnContentClick={ false }
openOnClick={ false }
>
<VConfirmEdit
{ ...confirmEditProps }
v-model={ model.value }
onSave={ onSave }
>
{{
default: ({ actions, model: proxyModel }) => {
return (
<VTimePicker
{ ...timePickerProps }
modelValue={ props.hideActions ? model.value : proxyModel.value }
onUpdate:modelValue={ val => {
if (!props.hideActions) {
proxyModel.value = val
} else {
model.value = val
}
}}
onMousedown={ (e: MouseEvent) => e.preventDefault() }
>
{{
actions: !props.hideActions ? () => actions : undefined,
}}
</VTimePicker>
)
},
}}
</VConfirmEdit>
</VMenu>
{ slots.default?.() }
</VTextField>
)
})
},
})
export type VTimeInput = InstanceType<typeof VTimeInput>