Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, {
Copy link
Member

Choose a reason for hiding this comment

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

你没发现这样子就和下面 mergedOpen 一样了么 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

对的,这里我想让 defaultOpen 首次参与 open 的计算,后续不会影响到 afferentOpen 这个值,跟 mergedOpen 不同的是 只有一处主动调用了 set 方法,后续状态变更时我这边测出来 afferentOpen 的值和 mergedOpen 的值是不一致的 😂

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);
Copy link
Member

Choose a reason for hiding this comment

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

如果受控关闭这里应该不会走到。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

对,受控关闭的话是不会走到这里,这里是考虑到在 defaultOpen 为 true 的情况下,选择完日期点击外部内容触发关闭后会走到这里,如果不设置为 false 的话后续 defaultOpen 因为会一直为 true,此时的状态应该是不对的。

}
});

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');
});
});