-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathindex.tsx
More file actions
329 lines (276 loc) Β· 9.75 KB
/
index.tsx
File metadata and controls
329 lines (276 loc) Β· 9.75 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
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
import { clsx } from 'clsx';
import ResizeObserver, { type ResizeObserverProps } from '@rc-component/resize-observer';
import * as React from 'react';
import type {
PanelRenderPanelProps,
RangeTimeProps,
SharedPickerProps,
SharedTimeProps,
ValueDate,
} from '../../interface';
import PickerPanel, { type PickerPanelProps, type PickerPanelRef } from '../../PickerPanel';
import { PickerHackContext, type PickerHackContextProps } from '../../PickerPanel/context';
import { toArray } from '../../utils/miscUtil';
import PickerContext from '../context';
import Footer, { type FooterProps } from './Footer';
import PopupPanel, {
getPopupPanelPickerProps,
getPopupPanelSharedContext,
type PopupPanelProps,
} from './PopupPanel';
import PresetPanel from './PresetPanel';
interface PanelRenderContextProps<DateType extends object = any> {
pickerProps: PickerPanelProps<DateType>;
sharedContext: PickerHackContextProps;
}
const PanelRenderContext = React.createContext<PanelRenderContextProps>(null!);
const InternalPanelRenderPanel = React.forwardRef(
<DateType extends object = any>(
props: PanelRenderPanelProps<DateType>,
ref: React.Ref<PickerPanelRef>,
) => {
const context = React.useContext<PanelRenderContextProps<DateType>>(PanelRenderContext);
if (!context) {
return <PickerPanel ref={ref} {...(props as PickerPanelProps<DateType>)} />;
}
const { pickerProps, sharedContext } = context;
const mergedPicker = props.picker ?? pickerProps.picker;
const mergedProps = {
...pickerProps,
...props,
picker: mergedPicker,
mode: props.mode ?? (props.picker !== undefined ? mergedPicker : pickerProps.mode),
hideHeader: props.hideHeader ?? mergedPicker === 'time',
components: props.components
? { ...pickerProps.components, ...props.components }
: pickerProps.components,
classNames: props.classNames
? { ...pickerProps.classNames, ...props.classNames }
: pickerProps.classNames,
styles: props.styles ? { ...pickerProps.styles, ...props.styles } : pickerProps.styles,
} as PickerPanelProps<DateType>;
return (
<PickerHackContext.Provider value={sharedContext}>
<PickerPanel ref={ref} {...mergedProps} />
</PickerHackContext.Provider>
);
},
);
const PanelRenderPanel = InternalPanelRenderPanel as <DateType extends object = any>(
props: PanelRenderPanelProps<DateType> & React.RefAttributes<PickerPanelRef>,
) => React.ReactElement<any>;
if (process.env.NODE_ENV !== 'production') {
InternalPanelRenderPanel.displayName = 'PanelRenderPanel';
}
export type PopupShowTimeConfig<DateType extends object = any> = Omit<
RangeTimeProps<DateType>,
'defaultValue' | 'defaultOpenValue' | 'disabledTime'
> &
Pick<SharedTimeProps<DateType>, 'disabledTime'>;
export interface PopupProps<DateType extends object = any, PresetValue = DateType>
extends Pick<React.InputHTMLAttributes<HTMLDivElement>, 'onFocus' | 'onBlur'>,
FooterProps<DateType>,
PopupPanelProps<DateType> {
panelRender?: SharedPickerProps['panelRender'];
// Presets
presets: ValueDate<DateType>[];
onPresetHover: (presetValue: PresetValue) => void;
onPresetSubmit: (presetValue: PresetValue) => void;
// Range
activeInfo?: [activeInputLeft: number, activeInputRight: number, selectorWidth: number];
// Direction
direction?: 'ltr' | 'rtl';
// Fill
/** TimePicker or showTime only */
defaultOpenValue: DateType;
// Change
needConfirm: boolean;
isInvalid: (date: DateType | DateType[]) => boolean;
onOk: VoidFunction;
onPanelMouseDown?: React.MouseEventHandler<HTMLDivElement>;
classNames?: SharedPickerProps['classNames'];
styles?: SharedPickerProps['styles'];
}
export default function Popup<DateType extends object = any>(props: PopupProps<DateType>) {
const {
panelRender,
internalMode,
picker,
showNow,
// Range
range,
multiple,
activeInfo = [0, 0, 0],
// Presets
presets,
onPresetHover,
onPresetSubmit,
// Focus
onFocus,
onBlur,
onPanelMouseDown,
// Direction
direction,
// Change
value,
onSelect,
needConfirm,
isInvalid,
defaultOpenValue,
onOk,
onSubmit,
classNames,
styles,
} = props;
const { prefixCls } = React.useContext(PickerContext);
const panelPrefixCls = `${prefixCls}-panel`;
const rtl = direction === 'rtl';
// ========================= Refs =========================
const arrowRef = React.useRef<HTMLDivElement>(null);
const wrapperRef = React.useRef<HTMLDivElement>(null);
// ======================== Offset ========================
const [containerWidth, setContainerWidth] = React.useState<number>(0);
const [containerOffset, setContainerOffset] = React.useState<number>(0);
const [arrowOffset, setArrowOffset] = React.useState<number>(0);
const onResize: ResizeObserverProps['onResize'] = (info) => {
if (info.width) {
setContainerWidth(info.width);
}
};
const [activeInputLeft, activeInputRight, selectorWidth] = activeInfo;
const [retryTimes, setRetryTimes] = React.useState(0);
React.useEffect(() => {
setRetryTimes(10);
}, [activeInputLeft]);
React.useEffect(() => {
// `activeOffset` is always align with the active input element
// So we need only check container contains the `activeOffset`
if (range && wrapperRef.current) {
// Offset in case container has border radius
const arrowWidth = arrowRef.current?.offsetWidth || 0;
// Arrow Offset
const wrapperRect = wrapperRef.current.getBoundingClientRect();
if (!wrapperRect.height || wrapperRect.right < 0) {
setRetryTimes((times) => Math.max(0, times - 1));
return;
}
const nextArrowOffset =
(rtl ? activeInputRight - arrowWidth : activeInputLeft) - wrapperRect.left;
setArrowOffset(nextArrowOffset);
// Container Offset
if (containerWidth && containerWidth < selectorWidth) {
const offset = rtl
? wrapperRect.right - (activeInputRight - arrowWidth + containerWidth)
: activeInputLeft + arrowWidth - wrapperRect.left - containerWidth;
const safeOffset = Math.max(0, offset);
setContainerOffset(safeOffset);
} else {
setContainerOffset(0);
}
}
}, [retryTimes, rtl, containerWidth, activeInputLeft, activeInputRight, selectorWidth, range]);
// ======================== Custom ========================
function filterEmpty<T>(list: T[]) {
return list.filter((item) => item);
}
const valueList = React.useMemo(() => filterEmpty(toArray(value)), [value]);
const isTimePickerEmptyValue = picker === 'time' && !valueList.length;
const footerSubmitValue = React.useMemo(() => {
if (isTimePickerEmptyValue) {
return filterEmpty([defaultOpenValue]);
}
return valueList;
}, [isTimePickerEmptyValue, valueList, defaultOpenValue]);
const popupPanelValue = isTimePickerEmptyValue ? defaultOpenValue : valueList;
const disableSubmit = React.useMemo(() => {
// Empty is invalid
if (!footerSubmitValue.length) {
return true;
}
return footerSubmitValue.some((val) => isInvalid(val));
}, [footerSubmitValue, isInvalid]);
const onFooterSubmit = () => {
// For TimePicker, we will additional trigger the value update
if (isTimePickerEmptyValue) {
onSelect(defaultOpenValue);
}
onOk();
onSubmit();
};
const panelRenderContext = {
pickerProps: getPopupPanelPickerProps({ ...props, value: popupPanelValue }),
sharedContext: getPopupPanelSharedContext(needConfirm, onSubmit),
};
let mergedNodes: React.ReactNode = (
<div className={`${prefixCls}-panel-layout`}>
{/* `any` here since PresetPanel is reused for both Single & Range Picker which means return type is not stable */}
<PresetPanel<any>
prefixCls={prefixCls}
presets={presets}
onClick={onPresetSubmit}
onHover={onPresetHover}
/>
<div>
<PopupPanel {...props} value={popupPanelValue} />
<Footer
{...props}
showNow={multiple ? false : showNow}
invalid={disableSubmit}
onSubmit={onFooterSubmit}
/>
</div>
</div>
);
if (typeof panelRender === 'function') {
mergedNodes = panelRender(mergedNodes, { components: { Panel: PanelRenderPanel } });
}
mergedNodes = (
<PanelRenderContext.Provider value={panelRenderContext}>
{mergedNodes}
</PanelRenderContext.Provider>
);
// ======================== Render ========================
const containerPrefixCls = `${panelPrefixCls}-container`;
const marginLeft = 'marginLeft';
const marginRight = 'marginRight';
// Container
let renderNode = (
<div
onMouseDown={onPanelMouseDown}
tabIndex={-1}
className={clsx(
containerPrefixCls,
// Used for Today Button style, safe to remove if no need
`${prefixCls}-${internalMode}-panel-container`,
classNames?.popup?.container,
)}
style={{
[rtl ? marginRight : marginLeft]: containerOffset,
[rtl ? marginLeft : marginRight]: 'auto',
...styles?.popup?.container,
}}
// Still wish not to lose focus on mouse down
// onMouseDown={(e) => {
// // e.preventDefault();
// }}
onFocus={onFocus}
onBlur={onBlur}
>
{mergedNodes}
</div>
);
if (range) {
renderNode = (
<div
onMouseDown={onPanelMouseDown}
ref={wrapperRef}
className={clsx(`${prefixCls}-range-wrapper`, `${prefixCls}-${picker}-range-wrapper`)}
>
<div ref={arrowRef} className={`${prefixCls}-range-arrow`} style={{ left: arrowOffset }} />
{/* Watch for container size */}
<ResizeObserver onResize={onResize}>{renderNode}</ResizeObserver>
</div>
);
}
return renderNode;
}