generated from react-component/footer
-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
index.tsx
237 lines (200 loc) Β· 6.4 KB
/
index.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
import classNames from 'classnames';
import ResizeObserver, { type ResizeObserverProps } from 'rc-resize-observer';
import * as React from 'react';
import type {
RangeTimeProps,
SharedPickerProps,
SharedTimeProps,
ValueDate,
} from '../../interface';
import { toArray } from '../../utils/miscUtil';
import { getRealPlacement, getoffsetUnit } from '../../utils/uiUtil';
import PickerContext from '../context';
import Footer, { type FooterProps } from './Footer';
import PopupPanel, { type PopupPanelProps } from './PopupPanel';
import PresetPanel from './PresetPanel';
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
activeOffset?: number;
placement?: string;
// 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>;
}
export default function Popup<DateType extends object = any>(props: PopupProps<DateType>) {
const {
panelRender,
internalMode,
picker,
showNow,
// Range
range,
multiple,
activeOffset = 0,
placement,
// Presets
presets,
onPresetHover,
onPresetSubmit,
// Focus
onFocus,
onBlur,
onPanelMouseDown,
// Direction
direction,
// Change
value,
onSelect,
isInvalid,
defaultOpenValue,
onOk,
onSubmit,
} = 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 onResize: ResizeObserverProps['onResize'] = (info) => {
if (info.offsetWidth) {
setContainerWidth(info.offsetWidth);
}
};
React.useEffect(() => {
// `activeOffset` is always align with the active input element
// So we need only check container contains the `activeOffset`
if (range) {
// Offset in case container has border radius
const arrowWidth = arrowRef.current?.offsetWidth || 0;
const maxOffset = containerWidth - arrowWidth;
if (activeOffset <= maxOffset) {
setContainerOffset(0);
} else {
setContainerOffset(activeOffset + arrowWidth - containerWidth);
}
}
}, [containerWidth, activeOffset, 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();
};
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 (panelRender) {
mergedNodes = panelRender(mergedNodes);
}
// ======================== Render ========================
const containerPrefixCls = `${panelPrefixCls}-container`;
const marginLeft = 'marginLeft';
const marginRight = 'marginRight';
// Container
let renderNode = (
<div
onMouseDown={onPanelMouseDown}
tabIndex={-1}
className={classNames(
containerPrefixCls,
// Used for Today Button style, safe to remove if no need
`${prefixCls}-${internalMode}-panel-container`,
)}
style={{
[rtl ? marginRight : marginLeft]: containerOffset,
[rtl ? marginLeft : marginRight]: 'auto',
}}
// Still wish not to lose focus on mouse down
// onMouseDown={(e) => {
// // e.preventDefault();
// }}
onFocus={onFocus}
onBlur={onBlur}
>
{mergedNodes}
</div>
);
if (range) {
const realPlacement = getRealPlacement(placement, rtl);
const offsetUnit = getoffsetUnit(realPlacement, rtl);
renderNode = (
<div
onMouseDown={onPanelMouseDown}
ref={wrapperRef}
className={classNames(`${prefixCls}-range-wrapper`, `${prefixCls}-${picker}-range-wrapper`)}
>
<div
ref={arrowRef}
className={`${prefixCls}-range-arrow`}
style={{ [offsetUnit]: activeOffset }}
/>
{/* Watch for container size */}
<ResizeObserver onResize={onResize}>{renderNode}</ResizeObserver>
</div>
);
}
return renderNode;
}