Skip to content

Commit dd6bb5a

Browse files
authored
fix:Field switch should be locked even when the field already has values in RangePicker with showTime enabled (#897)
* rangPicker禁用时 不在对禁用的日期进行校验 * 测试用例编写 * 测试用例 * fix: After the RangePicker sets disabledDate, the date cannot be modified. * Add test case * fix: returns right dates onChange when manually change date time without pressing OK button * fix:returns wrong dates onChange when manually change date time without pressing OK button * fix:returns wrong dates onChange when manually change date time without pressing OK button * fix:modify test cases * fix:adjust hasSubmitValue check for submitIndexRef * fix:add hasActiveSubmitValue check for submitIndexRef * fix:delete hasSubmitValue check
1 parent f58068e commit dd6bb5a

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

src/PickerInput/RangePicker.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ function RangePicker<DateType extends object = any>(
260260
setActiveIndex,
261261
nextActiveIndex,
262262
activeIndexList,
263+
updateSubmitIndex,
264+
hasActiveSubmitValue,
263265
] = useRangeActive(disabled, allowEmpty, mergedOpen);
264266

265267
const onSharedFocus = (event: React.FocusEvent<HTMLElement>, index?: number) => {
@@ -325,8 +327,6 @@ function RangePicker<DateType extends object = any>(
325327
flushSubmit,
326328
/** Trigger `onChange` directly without check `disabledDate` */
327329
triggerSubmitChange,
328-
/** Tell `index` has filled value in it */
329-
hasSubmitValue,
330330
] = useRangeValue<RangeValueType<DateType>, DateType>(
331331
filledProps,
332332
mergedValue,
@@ -413,7 +413,7 @@ function RangePicker<DateType extends object = any>(
413413
if (date) {
414414
nextValue = fillCalendarValue(date, activeIndex);
415415
}
416-
416+
updateSubmitIndex(activeIndex);
417417
// Get next focus index
418418
const nextIndex = nextActiveIndex(nextValue);
419419

@@ -641,7 +641,7 @@ function RangePicker<DateType extends object = any>(
641641
needConfirm &&
642642
// Not change index if is not filled
643643
!allowEmpty[lastActiveIndex] &&
644-
!hasSubmitValue(lastActiveIndex) &&
644+
!hasActiveSubmitValue(lastActiveIndex) &&
645645
calendarValue[lastActiveIndex]
646646
) {
647647
selectorRef.current.focus({ index: lastActiveIndex });

src/PickerInput/hooks/useRangeActive.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ export default function useRangeActive<DateType>(
2323
setActiveIndex: (index: number) => void,
2424
nextActiveIndex: NextActive<DateType>,
2525
activeList: number[],
26+
updateSubmitIndex: (index: number | null) => void,
27+
hasActiveSubmitValue: (index: number) => boolean,
2628
] {
2729
const [activeIndex, setActiveIndex] = React.useState(0);
2830
const [focused, setFocused] = React.useState<boolean>(false);
2931

3032
const activeListRef = React.useRef<number[]>([]);
31-
33+
const submitIndexRef = React.useRef<number | null>(null);
3234
const lastOperationRef = React.useRef<OperationType>(null);
3335

36+
const updateSubmitIndex = (index: number | null) => {
37+
submitIndexRef.current = index;
38+
};
39+
40+
const hasActiveSubmitValue = (index: number) => {
41+
return submitIndexRef.current === index;
42+
};
43+
3444
const triggerFocus = (nextFocus: boolean) => {
3545
setFocused(nextFocus);
3646
};
@@ -62,6 +72,7 @@ export default function useRangeActive<DateType>(
6272
useLockEffect(focused || mergedOpen, () => {
6373
if (!focused) {
6474
activeListRef.current = [];
75+
updateSubmitIndex(null);
6576
}
6677
});
6778

@@ -79,5 +90,7 @@ export default function useRangeActive<DateType>(
7990
setActiveIndex,
8091
nextActiveIndex,
8192
activeListRef.current,
93+
updateSubmitIndex,
94+
hasActiveSubmitValue,
8295
];
8396
}

src/PickerInput/hooks/useRangeValue.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ export function useInnerValue<ValueType extends DateType[], DateType extends obj
142142

143143
// Update merged value
144144
const [isSameMergedDates, isSameStart] = isSameDates(calendarValue(), clone);
145-
146145
if (!isSameMergedDates) {
147146
setCalendarValue(clone);
148147

@@ -186,8 +185,6 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
186185
flushSubmit: (index: number, needTriggerChange: boolean) => void,
187186
/** Trigger `onChange` directly without check `disabledDate` */
188187
triggerSubmitChange: (value: ValueType) => boolean,
189-
/** Tell `index` has filled value in it */
190-
hasSubmitValue: (index: number) => boolean,
191188
] {
192189
const {
193190
// MISC
@@ -333,11 +330,6 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
333330
2,
334331
);
335332

336-
// ============================ Check =============================
337-
function hasSubmitValue(index: number) {
338-
return !!submitValue()[index];
339-
}
340-
341333
// ============================ Return ============================
342-
return [flushSubmit, triggerSubmit, hasSubmitValue];
334+
return [flushSubmit, triggerSubmit];
343335
}

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('.rc-picker-ok button'));
726+
727+
selectCell(16);
728+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
729+
730+
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
731+
'1990-09-15 00:00:00',
732+
'1990-09-16 00:00:00',
733+
]);
734+
735+
onChange.mockReset();
736+
openPicker(container, 0);
737+
selectCell(1);
738+
openPicker(container, 1);
739+
expect(container.querySelectorAll('input')[0]).toHaveFocus();
740+
expect(container.querySelectorAll('input')[1]).not.toHaveFocus();
741+
742+
fireEvent.click(document.querySelector('.rc-picker-ok button'));
743+
openPicker(container, 1);
744+
expect(container.querySelectorAll('input')[1]).toHaveFocus();
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

0 commit comments

Comments
 (0)