-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VTimePicker.tsx
363 lines (310 loc) Β· 12.5 KB
/
VTimePicker.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Styles
import './VTimePicker.sass'
// Components
import { VTimePickerClock } from './VTimePickerClock'
import { VTimePickerControls } from './VTimePickerControls'
import { pad } from '@/components/VDatePicker/util'
import { makeVPickerProps, VPicker } from '@/labs/VPicker/VPicker'
// Composables
import { useLocale } from '@/composables/locale'
// Utilities
import { computed, onMounted, ref, watch } from 'vue'
import { SelectingTimes } from './SelectingTimes'
import { createRange, genericComponent, omit, propsFactory, useRender } from '@/util'
// Types
import type { PropType } from 'vue'
import type { VPickerSlots } from '@/labs/VPicker/VPicker'
type Period = 'am' | 'pm'
type AllowFunction = (val: number) => boolean
const rangeHours24 = createRange(24)
const rangeHours12am = createRange(12)
const rangeHours12pm = rangeHours12am.map(v => v + 12)
const range60 = createRange(60)
const selectingNames = { 1: 'hour', 2: 'minute', 3: 'second' }
export { SelectingTimes }
export type VTimePickerSlots = Omit<VPickerSlots, 'header'>
export const makeVTimePickerProps = propsFactory({
allowedHours: [Function, Array] as PropType<AllowFunction | number[]>,
allowedMinutes: [Function, Array] as PropType<AllowFunction | number[]>,
allowedSeconds: [Function, Array] as PropType<AllowFunction | number[]>,
ampmInTitle: Boolean,
disabled: Boolean,
format: {
type: String as PropType<'ampm' | '24hr'>,
default: 'ampm',
},
max: String,
min: String,
modelValue: null as any as PropType<any>,
readonly: Boolean,
scrollable: Boolean,
useSeconds: Boolean,
...omit(makeVPickerProps({ title: '$vuetify.timePicker.title' }), ['landscape']),
}, 'VTimePicker')
export const VTimePicker = genericComponent<VTimePickerSlots>()({
name: 'VTimePicker',
props: makeVTimePickerProps(),
emits: {
'update:hour': (val: number) => val,
'update:minute': (val: number) => val,
'update:period': (val: Period) => val,
'update:second': (val: number) => val,
'update:modelValue': (val: string) => val,
},
setup (props, { emit, slots }) {
const { t } = useLocale()
const inputHour = ref(null as number | null)
const inputMinute = ref(null as number | null)
const inputSecond = ref(null as number | null)
const lazyInputHour = ref(null as number | null)
const lazyInputMinute = ref(null as number | null)
const lazyInputSecond = ref(null as number | null)
const period = ref('am' as Period)
const selecting = ref(SelectingTimes.Hour)
const controlsRef = ref<VTimePickerControls | null>(null)
const clockRef = ref<VTimePickerClock | null>(null)
const isAllowedHourCb = computed((): AllowFunction => {
let cb: AllowFunction
if (props.allowedHours instanceof Array) {
cb = (val: number) => (props.allowedHours as number[]).includes(val)
} else {
cb = props.allowedHours as AllowFunction
}
if (!props.min && !props.max) return cb
const minHour = props.min ? Number(props.min.split(':')[0]) : 0
const maxHour = props.max ? Number(props.max.split(':')[0]) : 23
return (val: number) => {
return val >= minHour * 1 &&
val <= maxHour * 1 &&
(!cb || cb(val))
}
})
const isAllowedMinuteCb = computed((): AllowFunction => {
let cb: AllowFunction
const isHourAllowed = !isAllowedHourCb.value || inputHour.value === null || isAllowedHourCb.value(inputHour.value)
if (props.allowedMinutes instanceof Array) {
cb = (val: number) => (props.allowedMinutes as number[]).includes(val)
} else {
cb = props.allowedMinutes as AllowFunction
}
if (!props.min && !props.max) {
return isHourAllowed ? cb : () => false
}
const [minHour, minMinute] = props.min ? props.min.split(':').map(Number) : [0, 0]
const [maxHour, maxMinute] = props.max ? props.max.split(':').map(Number) : [23, 59]
const minTime = minHour * 60 + minMinute * 1
const maxTime = maxHour * 60 + maxMinute * 1
return (val: number) => {
const time = 60 * inputHour.value! + val
return time >= minTime &&
time <= maxTime &&
isHourAllowed &&
(!cb || cb(val))
}
})
const isAllowedSecondCb = computed((): AllowFunction => {
let cb: AllowFunction
const isHourAllowed = !isAllowedHourCb.value || inputHour.value === null || isAllowedHourCb.value(inputHour.value)
const isMinuteAllowed = isHourAllowed &&
(!isAllowedMinuteCb.value ||
inputMinute.value === null ||
isAllowedMinuteCb.value(inputMinute.value)
)
if (props.allowedSeconds instanceof Array) {
cb = (val: number) => (props.allowedSeconds as number[]).includes(val)
} else {
cb = props.allowedSeconds as AllowFunction
}
if (!props.min && !props.max) {
return isMinuteAllowed ? cb : () => false
}
const [minHour, minMinute, minSecond] = props.min ? props.min.split(':').map(Number) : [0, 0, 0]
const [maxHour, maxMinute, maxSecond] = props.max ? props.max.split(':').map(Number) : [23, 59, 59]
const minTime = minHour * 3600 + minMinute * 60 + (minSecond || 0) * 1
const maxTime = maxHour * 3600 + maxMinute * 60 + (maxSecond || 0) * 1
return (val: number) => {
const time = 3600 * inputHour.value! + 60 * inputMinute.value! + val
return time >= minTime &&
time <= maxTime &&
isMinuteAllowed &&
(!cb || cb(val))
}
})
const isAmPm = computed((): boolean => {
return props.format === 'ampm'
})
watch(() => props.modelValue, val => setInputData(val))
onMounted(() => {
setInputData(props.modelValue)
})
function genValue () {
if (inputHour.value != null && inputMinute.value != null && (!props.useSeconds || inputSecond.value != null)) {
return `${pad(inputHour.value)}:${pad(inputMinute.value)}` + (props.useSeconds ? `:${pad(inputSecond.value!)}` : '')
}
return null
}
function emitValue () {
const value = genValue()
if (value !== null) emit('update:modelValue', value)
}
function convert24to12 (hour: number) {
return hour ? ((hour - 1) % 12 + 1) : 12
}
function convert12to24 (hour: number, period: Period) {
return hour % 12 + (period === 'pm' ? 12 : 0)
}
function setInputData (value: string | null | Date) {
if (value == null || value === '') {
inputHour.value = null
inputMinute.value = null
inputSecond.value = null
} else if (value instanceof Date) {
inputHour.value = value.getHours()
inputMinute.value = value.getMinutes()
inputSecond.value = value.getSeconds()
} else {
const [hour, , minute, , second, period] = value.trim().toLowerCase().match(/^(\d+):(\d+)(:(\d+))?([ap]m)?$/) || new Array(6)
inputHour.value = period ? convert12to24(parseInt(hour, 10), period as Period) : parseInt(hour, 10)
inputMinute.value = parseInt(minute, 10)
inputSecond.value = parseInt(second || 0, 10)
}
period.value = (inputHour.value == null || inputHour.value < 12) ? 'am' : 'pm'
}
function firstAllowed (type: 'hour' | 'minute' | 'second', value: number) {
const allowedFn = type === 'hour' ? isAllowedHourCb.value : (type === 'minute' ? isAllowedMinuteCb.value : isAllowedSecondCb.value)
if (!allowedFn) return value
// TODO: clean up (Note from V2 code)
const range = type === 'minute'
? range60
: (type === 'second'
? range60
: (isAmPm.value
? (value < 12
? rangeHours12am
: rangeHours12pm)
: rangeHours24))
const first = range.find(v => allowedFn((v + value) % range.length + range[0]))
return ((first || 0) + value) % range.length + range[0]
}
function setPeriod (val: Period) {
period.value = val
if (inputHour.value != null) {
const newHour = inputHour.value! + (period.value === 'am' ? -12 : 12)
inputHour.value = firstAllowed('hour', newHour)
}
emit('update:period', val)
emitValue()
return true
}
function onInput (value: number) {
if (selecting.value === SelectingTimes.Hour) {
inputHour.value = isAmPm.value ? convert12to24(value, period.value) : value
} else if (selecting.value === SelectingTimes.Minute) {
inputMinute.value = value
} else {
inputSecond.value = value
}
}
function onChange (value: number) {
switch (selectingNames[selecting.value]) {
case 'hour':
emit('update:hour', value)
break
case 'minute':
emit('update:minute', value)
break
case 'second':
emit('update:second', value)
break
default:
break
}
const emitChange = selecting.value === (props.useSeconds ? SelectingTimes.Second : SelectingTimes.Minute)
if (selecting.value === SelectingTimes.Hour) {
selecting.value = SelectingTimes.Minute
} else if (props.useSeconds && selecting.value === SelectingTimes.Minute) {
selecting.value = SelectingTimes.Second
}
if (inputHour.value === lazyInputHour.value &&
inputMinute.value === lazyInputMinute.value &&
(!props.useSeconds || inputSecond.value === lazyInputSecond.value)
) return
const time = genValue()
if (time === null) return
lazyInputHour.value = inputHour.value
lazyInputMinute.value = inputMinute.value
props.useSeconds && (lazyInputSecond.value = inputSecond.value)
emitChange && emitValue()
}
useRender(() => {
const pickerProps = VPicker.filterProps(props)
const timePickerControlsProps = VTimePickerControls.filterProps(props)
const timePickerClockProps = VTimePickerClock.filterProps(omit(props, ['format', 'modelValue', 'min', 'max']))
return (
<VPicker
{ ...pickerProps }
color={ undefined }
class={[
'v-time-picker',
props.class,
]}
style={ props.style }
v-slots={{
title: () => slots.title?.() ?? (
<div class="v-time-picker__title">
{ t(props.title) }
</div>
),
header: () => (
<VTimePickerControls
{ ...timePickerControlsProps }
ampm={ isAmPm.value || props.ampmInTitle }
ampmReadonly={ isAmPm.value && !props.ampmInTitle }
hour={ inputHour.value as number }
minute={ inputMinute.value as number }
period={ period.value }
second={ inputSecond.value as number }
selecting={ selecting.value }
onUpdate:period={ (val: Period) => setPeriod(val) }
onUpdate:selecting={ (value: 1 | 2 | 3) => (selecting.value = value) }
ref={ controlsRef }
/>
),
default: () => (
<VTimePickerClock
{ ...timePickerClockProps }
allowedValues={
selecting.value === SelectingTimes.Hour
? isAllowedHourCb.value
: (selecting.value === SelectingTimes.Minute
? isAllowedMinuteCb.value
: isAllowedSecondCb.value)
}
double={ selecting.value === SelectingTimes.Hour && !isAmPm.value }
format={ selecting.value === SelectingTimes.Hour
? (isAmPm.value ? convert24to12 : (val: number) => val)
: (val: number) => pad(val, 2)
}
max={ selecting.value === SelectingTimes.Hour ? (isAmPm.value && period.value === 'am' ? 11 : 23) : 59 }
min={ selecting.value === SelectingTimes.Hour && isAmPm.value && period.value === 'pm' ? 12 : 0 }
size={ 20 }
step={ selecting.value === SelectingTimes.Hour ? 1 : 5 }
modelValue={ selecting.value === SelectingTimes.Hour
? inputHour.value as number
: (selecting.value === SelectingTimes.Minute
? inputMinute.value as number
: inputSecond.value as number)
}
onChange={ onChange }
onInput={ onInput }
ref={ clockRef }
/>
),
actions: slots.actions,
}}
/>
)
})
},
})
export type VTimePicker = InstanceType<typeof VTimePicker>