Skip to content

Commit bc2c3a1

Browse files
committed
fix:returns wrong dates onChange when manually change date time without pressing OK button
1 parent 1ed4d1c commit bc2c3a1

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

src/PickerInput/hooks/useRangeValue.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { GenerateConfig } from '../../generate';
44
import useSyncState from '../../hooks/useSyncState';
55
import type { BaseInfo, FormatType, Locale, ReplaceListType } from '../../interface';
66
import { formatValue, isSame, isSameTimestamp } from '../../utils/dateUtil';
7-
import { fillRangeValues } from '../../utils/miscUtil';
7+
import { fillIndex } from '../../utils/miscUtil';
88
import type { RangePickerProps } from '../RangePicker';
99
import type { ReplacedPickerProps } from '../SinglePicker';
1010
import useLockEffect from './useLockEffect';
@@ -301,11 +301,8 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
301301
});
302302

303303
// ========================= Flush Submit =========================
304-
const flushSubmit = useEvent((_: number, needTriggerChange: boolean) => {
305-
const start = getCalendarValue()[0];
306-
const end = getCalendarValue()[1];
307-
308-
const nextSubmitValue = fillRangeValues(submitValue(), start, end);
304+
const flushSubmit = useEvent((index: number, needTriggerChange: boolean) => {
305+
const nextSubmitValue = fillIndex(submitValue(), index, getCalendarValue()[index]);
309306
setSubmitValue(nextSubmitValue);
310307

311308
if (needTriggerChange) {
@@ -337,7 +334,10 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
337334

338335
// ============================ Check =============================
339336
function hasSubmitValue(index: number) {
340-
return !!submitValue()[index];
337+
return (
338+
!!submitValue()[index] &&
339+
isSame(generateConfig, locale, submitValue()[index], getCalendarValue()[index], picker)
340+
);
341341
}
342342

343343
// ============================ Return ============================

tests/new-range.spec.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,40 @@ describe('NewPicker.Range', () => {
716716
expect(onChange).toHaveBeenCalledWith(expect.anything(), ['06:00:00', '11:00:00']);
717717
});
718718

719+
it('Field switch should be locked even when the field already has the values', () => {
720+
const onChange = jest.fn();
721+
722+
const { container } = render(<DayRangePicker onChange={onChange} showTime />);
723+
openPicker(container);
724+
selectCell(15);
725+
fireEvent.click(document.querySelector('ul').querySelector('li'));
726+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
727+
728+
selectCell(16);
729+
fireEvent.click(document.querySelector('ul').querySelector('li'));
730+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
731+
732+
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
733+
'1990-09-15 00:00:00',
734+
'1990-09-16 00:00:00',
735+
]);
736+
737+
onChange.mockReset();
738+
openPicker(container, 0);
739+
selectCell(1);
740+
openPicker(container, 1);
741+
expect(container.querySelectorAll('input')[0]).toHaveFocus();
742+
expect(container.querySelectorAll('input')[1]).not.toHaveFocus();
743+
744+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
745+
selectCell(2);
746+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
747+
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
748+
'1990-09-01 00:00:00',
749+
'1990-09-02 00:00:00',
750+
]);
751+
});
752+
719753
it('double click to confirm if needConfirm', () => {
720754
const onChange = jest.fn();
721755

tests/range.spec.tsx

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,35 +1017,6 @@ describe('Picker.Range', () => {
10171017
expect(document.querySelector('.rc-picker-month-panel')).toBeTruthy();
10181018
});
10191019

1020-
it('returns right dates onChange when manually change date time without pressing OK button', () => {
1021-
const onChange = jest.fn();
1022-
1023-
const { container } = render(<DayRangePicker onChange={onChange} showTime />);
1024-
openPicker(container);
1025-
selectCell(15);
1026-
fireEvent.click(document.querySelector('ul').querySelector('li'));
1027-
fireEvent.click(document.querySelector('.rc-picker-ok button'));
1028-
1029-
selectCell(16);
1030-
fireEvent.click(document.querySelector('ul').querySelector('li'));
1031-
fireEvent.click(document.querySelector('.rc-picker-ok button'));
1032-
1033-
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
1034-
'1990-09-15 00:00:00',
1035-
'1990-09-16 00:00:00',
1036-
]);
1037-
1038-
onChange.mockReset();
1039-
openPicker(container, 0);
1040-
selectCell(1);
1041-
openPicker(container, 1);
1042-
selectCell(2);
1043-
fireEvent.click(document.querySelector('.rc-picker-ok button'));
1044-
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
1045-
'1990-09-01 00:00:00',
1046-
'1990-09-02 00:00:00',
1047-
]);
1048-
});
10491020
describe('reorder onChange logic', () => {
10501021
it('datetime should reorder in onChange if start is after end in same date', () => {
10511022
const onChange = jest.fn();

0 commit comments

Comments
 (0)