-
Notifications
You must be signed in to change notification settings - Fork 138
/
Panel.tsx
96 lines (84 loc) 路 3.38 KB
/
Panel.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
/**
* @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 { computed, defineComponent } from 'vue'
import { callEmit } from '@idux/cdk/utils'
import { 傻DatePanel } from '@idux/components/_private/date-panel'
import { 傻TimePanel } from '@idux/components/_private/time-panel'
import { useDateConfig, useGlobalConfig } from '@idux/components/config'
import { useThemeToken } from '@idux/components/theme'
import { getTimePickerThemeTokens } from '@idux/components/time-picker'
import { applyDateTime } from '@idux/components/utils'
import { getThemeTokens } from '../../theme'
import { useActiveValue } from '../composables/useActiveValue'
import { datePanelProps } from '../types'
import { convertPickerTypeToConfigType } from '../utils'
export default defineComponent({
name: 'IxDatePanel',
props: datePanelProps,
setup(props, { slots }) {
const common = useGlobalConfig('common')
const { globalHashId, hashId, registerToken } = useThemeToken('datePicker')
const { registerToken: registerTimePickerToken } = useThemeToken('timePicker')
registerToken(getThemeTokens)
registerTimePickerToken(getTimePickerThemeTokens)
const mergedPrefixCls = computed(() => `${common.prefixCls}-date-picker-panel`)
const dateConfig = useDateConfig()
const { activeValue, setActiveValue } = useActiveValue(dateConfig, props)
function handleChange(value: Date | undefined) {
callEmit(props.onChange, value)
}
function handleDatePanelChange(value: Date) {
handleChange(
props.value
? applyDateTime(dateConfig, props.value, value, ['hour', 'minute', 'second', 'millisecond'])
: value,
)
}
function handleTimePanelChange(value: Date) {
handleChange(props.value ? applyDateTime(dateConfig, props.value, value, ['year', 'month', 'date']) : value)
}
const handleDatePanelCellClick = (value: Date) => {
handleDatePanelChange(value)
}
const handleMouseDown = (e: MouseEvent) => {
if (!(e.target instanceof HTMLInputElement)) {
e.preventDefault()
}
}
return () => {
const datePanelType = convertPickerTypeToConfigType(props.type)
const datePanelProps = {
cellTooltip: props.cellTooltip,
disabledDate: props.disabledDate,
type: datePanelType,
value: props.value,
visible: props.type === 'datetime' ? props.visible === 'datePanel' : !!props.visible,
activeDate: activeValue.value,
onCellClick: handleDatePanelCellClick,
'onUpdate:activeDate': setActiveValue,
}
const _timePanelProps = {
...props.timePanelOptions,
activeValue: activeValue.value,
value: props.value,
visible: props.type === 'datetime' ? props.visible === 'timePanel' : false,
onChange: handleTimePanelChange,
'onUpdate:activeValue': setActiveValue,
}
return (
<div
class={[mergedPrefixCls.value, globalHashId.value, hashId.value]}
tabindex={-1}
onMousedown={handleMouseDown}
>
<傻DatePanel v-show={props.visible !== 'timePanel'} v-slots={slots} {...datePanelProps} />
{props.type === 'datetime' && <傻TimePanel v-show={props.visible === 'timePanel'} {..._timePanelProps} />}
</div>
)
}
},
})