-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathPopupPanel.tsx
More file actions
124 lines (108 loc) · 3.51 KB
/
PopupPanel.tsx
File metadata and controls
124 lines (108 loc) · 3.51 KB
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
import * as React from 'react';
import PickerPanel, { type PickerPanelProps } from '../../PickerPanel';
import { PickerHackContext, type PickerHackContextProps } from '../../PickerPanel/context';
import PickerContext from '../context';
import { offsetPanelDate } from '../hooks/useRangePickerValue';
import { type FooterProps } from './Footer';
export type MustProp<DateType extends object> = Required<
Pick<PickerPanelProps<DateType>, 'mode' | 'onPanelChange'>
>;
export type PopupPanelProps<DateType extends object = any> = MustProp<DateType> &
Omit<PickerPanelProps<DateType>, 'onPickerValueChange' | 'showTime'> &
FooterProps<DateType> & {
multiplePanel?: boolean;
range?: boolean;
onPickerValueChange: (date: DateType) => void;
};
export function getPopupPanelSharedContext(
needConfirm: boolean,
onSubmit: VoidFunction,
): PickerHackContextProps {
return {
onCellDblClick: () => {
if (needConfirm) {
onSubmit();
}
},
};
}
export function getPopupPanelPickerProps<DateType extends object = any>(
props: PopupPanelProps<DateType>,
): PickerPanelProps<DateType> {
const { picker, hoverValue, range } = props;
const pickerProps = {
...props,
hoverValue: null,
hoverRangeValue: null,
hideHeader: picker === 'time',
} as PickerPanelProps<DateType>;
if (range) {
pickerProps.hoverRangeValue = hoverValue as PickerPanelProps<DateType>['hoverRangeValue'];
} else {
pickerProps.hoverValue = hoverValue as PickerPanelProps<DateType>['hoverValue'];
}
return pickerProps;
}
export default function PopupPanel<DateType extends object = any>(
props: PopupPanelProps<DateType>,
) {
const { picker, multiplePanel, pickerValue, onPickerValueChange, needConfirm, onSubmit } = props;
const { prefixCls, generateConfig } = React.useContext(PickerContext);
// ======================== Offset ========================
const internalOffsetDate = React.useCallback(
(date: DateType, offset: number) => {
return offsetPanelDate(generateConfig, picker, date, offset);
},
[generateConfig, picker],
);
const nextPickerValue = React.useMemo(
() => internalOffsetDate(pickerValue, 1),
[pickerValue, internalOffsetDate],
);
// Outside
const onSecondPickerValueChange = (nextDate: DateType) => {
onPickerValueChange(internalOffsetDate(nextDate, -1));
};
// ======================= Context ========================
const sharedContext = getPopupPanelSharedContext(needConfirm, onSubmit);
// ======================== Props =========================
const pickerProps = getPopupPanelPickerProps(props);
// ======================== Render ========================
// Multiple
if (multiplePanel) {
return (
<div className={`${prefixCls}-panels`}>
<PickerHackContext.Provider
value={{
...sharedContext,
hideNext: true,
}}
>
<PickerPanel<DateType> {...pickerProps} />
</PickerHackContext.Provider>
<PickerHackContext.Provider
value={{
...sharedContext,
hidePrev: true,
}}
>
<PickerPanel<DateType>
{...pickerProps}
pickerValue={nextPickerValue}
onPickerValueChange={onSecondPickerValueChange}
/>
</PickerHackContext.Provider>
</div>
);
}
// Single
return (
<PickerHackContext.Provider
value={{
...sharedContext,
}}
>
<PickerPanel {...pickerProps} />
</PickerHackContext.Provider>
);
}