Skip to content

Commit 50e649f

Browse files
authored
fix: field input focus switch trigger (#835)
* fix: it * test: add test case * docs: back of demo * chore: comment on it
1 parent fd4b023 commit 50e649f

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/PickerInput/RangePicker.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function RangePicker<DateType extends object = any>(
260260
setActiveIndex,
261261
nextActiveIndex,
262262
activeIndexList,
263-
] = useRangeActive(disabled, allowEmpty);
263+
] = useRangeActive(disabled, allowEmpty, mergedOpen);
264264

265265
const onSharedFocus = (event: React.FocusEvent<HTMLElement>, index?: number) => {
266266
triggerFocus(true);
@@ -627,6 +627,13 @@ function RangePicker<DateType extends object = any>(
627627
inherit: true,
628628
});
629629

630+
// When click input to switch the field, it will not trigger close.
631+
// Which means it will lose the part confirm and we need fill back.
632+
// ref: https://github.com/ant-design/ant-design/issues/49512
633+
if (activeIndex !== index && mergedOpen && !needConfirm && complexPicker) {
634+
triggerPartConfirm(null, true);
635+
}
636+
630637
setActiveIndex(index);
631638

632639
onSharedFocus(event, index);

src/PickerInput/hooks/useRangeActive.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type NextActive<DateType> = (nextValue: RangeValueType<DateType>) => numb
1414
export default function useRangeActive<DateType>(
1515
disabled: boolean[],
1616
empty: boolean[] = [],
17+
mergedOpen: boolean = false,
1718
): [
1819
focused: boolean,
1920
triggerFocus: (focused: boolean) => void,
@@ -57,7 +58,8 @@ export default function useRangeActive<DateType>(
5758
};
5859

5960
// ============================= Effect =============================
60-
useLockEffect(focused, () => {
61+
// Wait in case it's from the click outside to blur
62+
useLockEffect(focused || mergedOpen, () => {
6163
if (!focused) {
6264
activeListRef.current = [];
6365
}

tests/range.spec.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,15 +1933,44 @@ describe('Picker.Range', () => {
19331933
value: '2024-06-15',
19341934
},
19351935
});
1936-
1936+
19371937
fireEvent.keyDown(container.querySelectorAll('input')[1], {
19381938
key: 'Enter',
19391939
code: 'Enter',
19401940
});
1941-
19421941
});
1943-
1942+
19441943
expect(container.querySelectorAll('input')[1].value).toBe('2024-06-15');
19451944
expect(container.querySelectorAll('input')[0].value).toBe('2024-06-13');
19461945
});
1946+
1947+
it('click on the input should trigger success when with !needConfirm', () => {
1948+
const onChange = jest.fn();
1949+
const { container } = render(
1950+
<DayRangePicker showTime needConfirm={false} onChange={onChange} />,
1951+
);
1952+
1953+
// Select first field
1954+
openPicker(container, 0);
1955+
selectCell(1, 0);
1956+
1957+
// Select second field
1958+
openPicker(container, 1);
1959+
selectCell(2, 0);
1960+
1961+
// Click outside to blur
1962+
fireEvent.mouseDown(document.body);
1963+
fireEvent.mouseUp(document.body);
1964+
fireEvent.click(document.body);
1965+
1966+
act(() => {
1967+
jest.advanceTimersByTime(1000);
1968+
});
1969+
1970+
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
1971+
'1990-09-01 00:00:00',
1972+
'1990-09-02 00:00:00',
1973+
]);
1974+
expect(isOpen()).toBeFalsy();
1975+
});
19471976
});

0 commit comments

Comments
 (0)