-
Notifications
You must be signed in to change notification settings - Fork 138
/
usePickerState.ts
87 lines (73 loc) 路 2.8 KB
/
usePickerState.ts
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
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import { type ComputedRef, toRaw } from 'vue'
import { isArray } from 'lodash-es'
import { type FormAccessor, ValidateStatus, useAccessorAndControl } from '@idux/cdk/forms'
import { callEmit, convertArray } from '@idux/cdk/utils'
import { type DateConfig } from '@idux/components/config'
import { FormSize, useFormItemRegister, useFormSize, useFormStatus } from '@idux/components/form'
import { convertToDate, sortRangeValue } from '@idux/components/utils'
import { type DatePickerProps, type DateRangePickerProps } from '../types'
type StateValueType<T extends DatePickerProps | DateRangePickerProps> = T extends DatePickerProps
? Date | undefined
: (Date | undefined)[] | undefined
export interface PickerStateContext<T extends DatePickerProps | DateRangePickerProps> {
accessor: FormAccessor<T['value']>
mergedSize: ComputedRef<FormSize>
mergedStatus: ComputedRef<ValidateStatus | undefined>
handleChange: (value: StateValueType<T>) => void
handleClear: (evt: MouseEvent) => void
handleFocus: (evt: FocusEvent) => void
handleBlur: (evt: FocusEvent) => void
}
export function usePickerState<T extends DatePickerProps | DateRangePickerProps>(
props: T,
config: { size: FormSize },
dateConfig: DateConfig,
formatRef: ComputedRef<string>,
setOverlayOpened: (overlayOpened: boolean) => void,
): PickerStateContext<T> {
const { accessor, control } = useAccessorAndControl<T['value']>()
useFormItemRegister(control)
const mergedSize = useFormSize(props, config)
const mergedStatus = useFormStatus(props, control)
function handleChange(value: StateValueType<T>) {
const newValue = (isArray(value) ? sortRangeValue(dateConfig, value) : value) as StateValueType<T>
if (convertArray(newValue).some(v => props.disabledDate?.(v))) {
return
}
let oldValue = toRaw(accessor.value) as StateValueType<T>
oldValue = (
isArray(oldValue)
? oldValue.map(v => convertToDate(dateConfig, v, formatRef.value))
: convertToDate(dateConfig, oldValue, formatRef.value)
) as StateValueType<T>
accessor.setValue(newValue as T['value'])
callEmit(props.onChange as (value: StateValueType<T>, oldValue: StateValueType<T>) => void, newValue, oldValue)
}
function handleClear(evt: MouseEvent) {
callEmit(props.onClear, evt)
handleChange(undefined)
}
function handleFocus(evt: FocusEvent) {
callEmit(props.onFocus, evt)
}
function handleBlur(evt: FocusEvent) {
accessor.markAsBlurred()
setOverlayOpened(false)
callEmit(props.onBlur, evt)
}
return {
accessor,
mergedSize,
mergedStatus,
handleChange,
handleClear,
handleFocus,
handleBlur,
}
}