Skip to content

Commit 1d13a31

Browse files
Yuiai01dujiaqi
andauthored
fix: fix panel not switch when select date with open is true (#663)
Co-authored-by: dujiaqi <[email protected]>
1 parent a67d2a0 commit 1d13a31

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/hooks/useRangeOpen.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export default function useRangeOpen(
4343
] {
4444
const [firstTimeOpen, setFirstTimeOpen] = React.useState(false);
4545

46+
const [afferentOpen, setAfferentOpen] = useMergedState(defaultOpen || false, {
47+
value: open,
48+
});
49+
4650
const [mergedOpen, setMergedOpen] = useMergedState(defaultOpen || false, {
4751
value: open,
4852
onChange: (nextOpen) => {
@@ -62,6 +66,8 @@ export default function useRangeOpen(
6266
}
6367
}, [mergedOpen]);
6468

69+
const queryNextIndex = (index: number) => (index === 0 ? 1 : 0);
70+
6571
const triggerOpen = useEvent((nextOpen: boolean, index: 0 | 1 | false, source: SourceType) => {
6672
if (index === false) {
6773
// Only when `nextOpen` is false and no need open to next index
@@ -70,7 +76,7 @@ export default function useRangeOpen(
7076
setMergedActivePickerIndex(index);
7177
setMergedOpen(nextOpen);
7278

73-
const nextIndex = index === 0 ? 1 : 0;
79+
const nextIndex = queryNextIndex(index);
7480

7581
// Record next open index
7682
if (
@@ -87,23 +93,27 @@ export default function useRangeOpen(
8793
}
8894
}
8995
} else if (source === 'confirm' || (source === 'blur' && changeOnBlur)) {
90-
if (nextActiveIndex !== null) {
96+
const customNextActiveIndex = afferentOpen ? queryNextIndex(index) : nextActiveIndex;
97+
98+
if (customNextActiveIndex !== null) {
9199
setFirstTimeOpen(false);
92-
setMergedActivePickerIndex(nextActiveIndex);
100+
setMergedActivePickerIndex(customNextActiveIndex);
93101
}
102+
94103
setNextActiveIndex(null);
95104

96105
// Focus back
97-
if (nextActiveIndex !== null && !disabled[nextActiveIndex]) {
106+
if (customNextActiveIndex !== null && !disabled[customNextActiveIndex]) {
98107
raf(() => {
99-
const ref = [startInputRef, endInputRef][nextActiveIndex];
108+
const ref = [startInputRef, endInputRef][customNextActiveIndex];
100109
ref.current?.focus();
101110
});
102111
} else {
103112
setMergedOpen(false);
104113
}
105114
} else {
106115
setMergedOpen(false);
116+
setAfferentOpen(false);
107117
}
108118
});
109119

tests/range.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,4 +1921,10 @@ describe('Picker.Range', () => {
19211921
expect(document.querySelector('input').value).toEqual('');
19221922
});
19231923

1924+
it('selected date when open is true should switch panel', () => {
1925+
render(<MomentRangePicker open />);
1926+
1927+
fireEvent.click(document.querySelector('.rc-picker-cell'));
1928+
expect(document.querySelectorAll('.rc-picker-input')[1]).toHaveClass('rc-picker-input-active');
1929+
});
19241930
});

0 commit comments

Comments
 (0)