Skip to content

Commit b69cf2d

Browse files
committed
fix(ui): fix logic of date-picker popup window
1 parent b5a30c6 commit b69cf2d

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

packages/ui/src/components/_date-input/DateInput.tsx

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface DDateInputProps extends Omit<React.HTMLAttributes<HTMLDivElemen
3030
date: [Date | null, Date | null];
3131
isFocus: [boolean, boolean];
3232
changeDate: (date: Date | [Date, Date]) => void;
33+
enter: () => void;
3334
renderPopup: DCloneHTMLElement;
3435
}) => JSX.Element | null;
3536
dRef:
@@ -200,6 +201,25 @@ function DateInput(props: DDateInputProps, ref: React.ForwardedRef<DDateInputRef
200201
forceUpdate();
201202
};
202203

204+
const handleEnter = () => {
205+
const index = isFocus[0] ? 0 : 1;
206+
const value = isFocus[0] ? valueLeft : valueRight;
207+
if (dayjs(dataRef.current.inputValue[index], dFormat, true).isValid()) {
208+
if (dRange) {
209+
if (isNull(isFocus[0] ? valueRight : valueLeft)) {
210+
dataRef.current.focusAnother = true;
211+
} else {
212+
onVisibleChange(false);
213+
}
214+
} else {
215+
onVisibleChange(false);
216+
}
217+
} else {
218+
dataRef.current.inputValue[index] = isNull(value) ? '' : dayjs(value).format(dFormat);
219+
}
220+
forceUpdate();
221+
};
222+
203223
const clearable = dClearable && !isNull(_value) && !dVisible && !dDisabled;
204224

205225
const maxZIndex = useMaxIndex(dVisible);
@@ -324,20 +344,7 @@ function DateInput(props: DDateInputProps, ref: React.ForwardedRef<DDateInputRef
324344
}}
325345
onKeyDown={(e) => {
326346
if (e.code === 'Enter') {
327-
if (dayjs(dataRef.current.inputValue[index], dFormat, true).isValid()) {
328-
if (dRange) {
329-
if (isNull(isLeft ? valueRight : valueLeft)) {
330-
dataRef.current.focusAnother = true;
331-
} else {
332-
onVisibleChange(false);
333-
}
334-
} else {
335-
onVisibleChange(false);
336-
}
337-
} else {
338-
dataRef.current.inputValue[index] = isNull(value) ? '' : dayjs(value).format(dFormat);
339-
}
340-
forceUpdate();
347+
handleEnter();
341348
}
342349
}}
343350
onFocus={() => {
@@ -490,6 +497,7 @@ function DateInput(props: DDateInputProps, ref: React.ForwardedRef<DDateInputRef
490497
changeValue(date);
491498
}
492499
},
500+
enter: handleEnter,
493501
renderPopup: (el) =>
494502
cloneHTMLElement(el, {
495503
ref: popupRef,

packages/ui/src/components/date-picker/DatePicker.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DFormControl } from '../form';
44
import type { DTimePickerProps } from '../time-picker';
55
import type { DPanelPrivateProps as DTimePickerPanelPrivateProps } from '../time-picker/Panel';
66

7-
import { isBoolean, isUndefined } from 'lodash';
7+
import { isArray, isBoolean, isUndefined } from 'lodash';
88
import React, { useRef } from 'react';
99

1010
import { CalendarOutlined } from '@react-devui/icons';
@@ -139,7 +139,7 @@ function DatePicker(props: DDatePickerProps, ref: React.ForwardedRef<DDateInputR
139139
afterVisibleChange={afterVisibleChange}
140140
onClear={onClear}
141141
>
142-
{({ date, isFocus, changeDate, renderPopup }) => {
142+
{({ date, isFocus, changeDate, enter, renderPopup }) => {
143143
const index = isFocus[0] ? 0 : 1;
144144
const position = isFocus[0] ? 'start' : 'end';
145145

@@ -152,9 +152,8 @@ function DatePicker(props: DDatePickerProps, ref: React.ForwardedRef<DDateInputR
152152
dConfigDate={dConfigDate ? (...args) => dConfigDate(...args, position, date) : undefined}
153153
onDateChange={(date) => {
154154
changeDate(date);
155-
156155
if (!dShowTime) {
157-
changeVisible(false);
156+
enter();
158157
}
159158
}}
160159
></DPanel>
@@ -182,10 +181,13 @@ function DatePicker(props: DDatePickerProps, ref: React.ForwardedRef<DDateInputR
182181
const handleClick = () => {
183182
const d = dPresetDate[name]();
184183
changeDate(d);
184+
if (dRange && isArray(d)) {
185+
changeVisible(false);
186+
} else {
187+
enter();
188+
}
185189
updatePanelRef.current?.(d[index]);
186190
updateTimePickerPanelRef.current?.(d[index]);
187-
188-
changeVisible(false);
189191
};
190192

191193
return (
@@ -205,10 +207,9 @@ function DatePicker(props: DDatePickerProps, ref: React.ForwardedRef<DDateInputR
205207
onClick={() => {
206208
const now = new Date();
207209
changeDate(now);
210+
enter();
208211
updatePanelRef.current?.(now);
209212
updateTimePickerPanelRef.current?.(now);
210-
211-
changeVisible(false);
212213
}}
213214
dType="link"
214215
>

packages/ui/src/components/time-picker/TimePicker.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function TimePicker(props: DTimePickerProps, ref: React.ForwardedRef<DTimePicker
129129
afterVisibleChange={afterVisibleChange}
130130
onClear={onClear}
131131
>
132-
{({ date, isFocus, changeDate, renderPopup }) =>
132+
{({ date, isFocus, changeDate, enter, renderPopup }) =>
133133
renderPopup(
134134
<div className={getClassName(dPopupClassName, `${dPrefix}time-picker__popup`)}>
135135
<DPanel
@@ -145,9 +145,8 @@ function TimePicker(props: DTimePickerProps, ref: React.ForwardedRef<DTimePicker
145145
onClick={() => {
146146
const now = new Date();
147147
changeDate(now);
148+
enter();
148149
updatePanelRef.current?.(now);
149-
150-
changeVisible(false);
151150
}}
152151
dType="link"
153152
>

0 commit comments

Comments
 (0)