Skip to content

Commit

Permalink
fix: fix panel not switch when select date with open is true (#663)
Browse files Browse the repository at this point in the history
Co-authored-by: dujiaqi <[email protected]>
  • Loading branch information
Yuiai01 and dujiaqi authored Aug 8, 2023
1 parent a67d2a0 commit 1d13a31
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/hooks/useRangeOpen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default function useRangeOpen(
] {
const [firstTimeOpen, setFirstTimeOpen] = React.useState(false);

const [afferentOpen, setAfferentOpen] = useMergedState(defaultOpen || false, {
value: open,
});

const [mergedOpen, setMergedOpen] = useMergedState(defaultOpen || false, {
value: open,
onChange: (nextOpen) => {
Expand All @@ -62,6 +66,8 @@ export default function useRangeOpen(
}
}, [mergedOpen]);

const queryNextIndex = (index: number) => (index === 0 ? 1 : 0);

const triggerOpen = useEvent((nextOpen: boolean, index: 0 | 1 | false, source: SourceType) => {
if (index === false) {
// Only when `nextOpen` is false and no need open to next index
Expand All @@ -70,7 +76,7 @@ export default function useRangeOpen(
setMergedActivePickerIndex(index);
setMergedOpen(nextOpen);

const nextIndex = index === 0 ? 1 : 0;
const nextIndex = queryNextIndex(index);

// Record next open index
if (
Expand All @@ -87,23 +93,27 @@ export default function useRangeOpen(
}
}
} else if (source === 'confirm' || (source === 'blur' && changeOnBlur)) {
if (nextActiveIndex !== null) {
const customNextActiveIndex = afferentOpen ? queryNextIndex(index) : nextActiveIndex;

if (customNextActiveIndex !== null) {
setFirstTimeOpen(false);
setMergedActivePickerIndex(nextActiveIndex);
setMergedActivePickerIndex(customNextActiveIndex);
}

setNextActiveIndex(null);

// Focus back
if (nextActiveIndex !== null && !disabled[nextActiveIndex]) {
if (customNextActiveIndex !== null && !disabled[customNextActiveIndex]) {
raf(() => {
const ref = [startInputRef, endInputRef][nextActiveIndex];
const ref = [startInputRef, endInputRef][customNextActiveIndex];
ref.current?.focus();
});
} else {
setMergedOpen(false);
}
} else {
setMergedOpen(false);
setAfferentOpen(false);
}
});

Expand Down
6 changes: 6 additions & 0 deletions tests/range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1921,4 +1921,10 @@ describe('Picker.Range', () => {
expect(document.querySelector('input').value).toEqual('');
});

it('selected date when open is true should switch panel', () => {
render(<MomentRangePicker open />);

fireEvent.click(document.querySelector('.rc-picker-cell'));
expect(document.querySelectorAll('.rc-picker-input')[1]).toHaveClass('rc-picker-input-active');
});
});

1 comment on commit 1d13a31

@vercel
Copy link

@vercel vercel bot commented on 1d13a31 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.