Skip to content

Commit adfa327

Browse files
committed
chore: Move submit button into panel
1 parent a1b04a4 commit adfa327

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

assets/index.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@
129129
}
130130
}
131131

132+
&-now {
133+
text-align: left;
134+
overflow: hidden;
135+
}
136+
137+
&-ok {
138+
float: right;
139+
}
140+
132141
// ================== Year & Month Panel ==================
133142
&-year-panel,
134143
&-month-panel {

src/Picker.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface PickerSharedProps<DateType> extends React.AriaAttributes {
8181
// WAI-ARIA
8282
role?: string;
8383
name?: string;
84+
85+
/** @private Internal usage. Do not use in your production env */
86+
components?: {
87+
button: React.ComponentType;
88+
};
8489
}
8590

8691
type OmitPanelProps<Props> = Omit<
@@ -158,6 +163,9 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
158163

159164
const inputRef = React.useRef<HTMLInputElement>(null);
160165

166+
const needConfirmButton: boolean =
167+
(picker === 'date' && !!showTime) || picker === 'time';
168+
161169
// ============================= State =============================
162170
const formatList = toArray(
163171
getDefaultFormat(format, picker, showTime, use12Hours),
@@ -266,7 +274,7 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
266274

267275
// ============================= Input =============================
268276
const [inputProps, { focused, typing }] = usePickerInput({
269-
blurToCancel: !!(picker === 'date' && showTime),
277+
blurToCancel: needConfirmButton,
270278
open: mergedOpen,
271279
triggerOpen,
272280
forwardKeyDown,
@@ -375,8 +383,11 @@ function InnerPicker<DateType>(props: PickerProps<DateType>) {
375383
}
376384

377385
// ============================ Return =============================
378-
const onContextSelect = (date: DateType, type: 'key' | 'mouse') => {
379-
if (type !== 'key' && (picker !== 'date' || !showTime)) {
386+
const onContextSelect = (
387+
date: DateType,
388+
type: 'key' | 'mouse' | 'submit',
389+
) => {
390+
if (type === 'submit' || (type !== 'key' && !needConfirmButton)) {
380391
// triggerChange will also update selected values
381392
triggerChange(date);
382393
triggerOpen(false, true);

src/PickerPanel.tsx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ export interface PickerPanelSharedProps<DateType> {
6969
onChange?: (value: DateType) => void;
7070
onPanelChange?: OnPanelChange<DateType>;
7171
onMouseDown?: React.MouseEventHandler<HTMLDivElement>;
72+
onOk?: () => void;
7273

7374
/** @private This is internal usage. Do not use in your production env */
7475
hideHeader?: boolean;
7576
/** @private This is internal usage. Do not use in your production env */
7677
onPickerValueChange?: (date: DateType) => void;
78+
79+
/** @private Internal usage. Do not use in your production env */
80+
components?: {
81+
button: React.ComponentType;
82+
};
7783
}
7884

7985
export interface PickerPanelBaseProps<DateType>
@@ -135,6 +141,8 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
135141
onPanelChange,
136142
onMouseDown,
137143
onPickerValueChange,
144+
onOk,
145+
components,
138146
} = props as MergedPickerPanelProps<DateType>;
139147

140148
if (process.env.NODE_ENV !== 'production') {
@@ -229,20 +237,20 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
229237

230238
const triggerSelect = (
231239
date: DateType,
232-
type: 'key' | 'mouse',
240+
type: 'key' | 'mouse' | 'submit',
233241
forceTriggerSelect: boolean = false,
234242
) => {
235243
if (mergedMode === picker || forceTriggerSelect) {
236244
setInnerValue(date);
237245

238-
if (onContextSelect) {
239-
onContextSelect(date, type);
240-
}
241-
242246
if (onSelect) {
243247
onSelect(date);
244248
}
245249

250+
if (onContextSelect) {
251+
onContextSelect(date, type);
252+
}
253+
246254
if (onChange && !isEqual(generateConfig, date, mergedValue)) {
247255
onChange(date);
248256
}
@@ -418,17 +426,35 @@ function PickerPanel<DateType>(props: PickerPanelProps<DateType>) {
418426
let nowNode: React.ReactNode;
419427
let todayNode: React.ReactNode;
420428

421-
if (showTime && !inRange) {
429+
if ((mergedMode === 'date' && showTime) || mergedMode === 'time') {
430+
const Button = components ? components.button : 'button';
431+
422432
nowNode = (
423433
<div className={`${prefixCls}-now`}>
424434
<a
425435
className={`${prefixCls}-now-btn`}
426436
onClick={() => {
427-
triggerSelect(generateConfig.getNow(), 'mouse', true);
437+
triggerSelect(generateConfig.getNow(), 'submit', true);
428438
}}
429439
>
430440
{locale.now}
431441
</a>
442+
443+
<div className={`${prefixCls}-ok`}>
444+
<Button
445+
disabled={!value}
446+
onClick={() => {
447+
if (value) {
448+
triggerSelect(value, 'submit', true);
449+
if (onOk) {
450+
onOk();
451+
}
452+
}
453+
}}
454+
>
455+
{locale.ok}
456+
</Button>
457+
</div>
432458
</div>
433459
);
434460
} else if (showToday && mergedMode === 'date' && picker === 'date') {

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export type NullableDateType<DateType> = DateType | null | undefined;
5353

5454
export type OnSelect<DateType> = (
5555
value: DateType,
56-
type: 'key' | 'mouse',
56+
type: 'key' | 'mouse' | 'submit',
5757
) => void;
5858

5959
export interface PanelSharedProps<DateType> {

0 commit comments

Comments
 (0)