Skip to content

fix:Field switch should be locked even when the field already has values in RangePicker with showTime enabled #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ function RangePicker<DateType extends object = any>(
setActiveIndex,
nextActiveIndex,
activeIndexList,
submitIndexRef,
] = useRangeActive(disabled, allowEmpty, mergedOpen);

const onSharedFocus = (event: React.FocusEvent<HTMLElement>, index?: number) => {
Expand Down Expand Up @@ -413,7 +414,7 @@ function RangePicker<DateType extends object = any>(
if (date) {
nextValue = fillCalendarValue(date, activeIndex);
}

submitIndexRef.current = activeIndex;
// Get next focus index
const nextIndex = nextActiveIndex(nextValue);

Expand Down Expand Up @@ -641,7 +642,7 @@ function RangePicker<DateType extends object = any>(
needConfirm &&
// Not change index if is not filled
!allowEmpty[lastActiveIndex] &&
!hasSubmitValue(lastActiveIndex) &&
!hasSubmitValue(lastActiveIndex, submitIndexRef.current) &&
calendarValue[lastActiveIndex]
) {
selectorRef.current.focus({ index: lastActiveIndex });
Expand Down
5 changes: 5 additions & 0 deletions src/PickerInput/hooks/useRangeActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export default function useRangeActive<DateType>(
setActiveIndex: (index: number) => void,
nextActiveIndex: NextActive<DateType>,
activeList: number[],
submitIndexRef: React.MutableRefObject<number | null>,
] {
const [activeIndex, setActiveIndex] = React.useState(0);
const [focused, setFocused] = React.useState<boolean>(false);

const activeListRef = React.useRef<number[]>([]);

const submitIndexRef = React.useRef<number | null>(null);

const lastOperationRef = React.useRef<OperationType>(null);

const triggerFocus = (nextFocus: boolean) => {
Expand Down Expand Up @@ -62,6 +65,7 @@ export default function useRangeActive<DateType>(
useLockEffect(focused || mergedOpen, () => {
if (!focused) {
activeListRef.current = [];
submitIndexRef.current = null;
}
});

Expand All @@ -79,5 +83,6 @@ export default function useRangeActive<DateType>(
setActiveIndex,
nextActiveIndex,
activeListRef.current,
submitIndexRef,
];
}
7 changes: 3 additions & 4 deletions src/PickerInput/hooks/useRangeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export function useInnerValue<ValueType extends DateType[], DateType extends obj

// Update merged value
const [isSameMergedDates, isSameStart] = isSameDates(calendarValue(), clone);

if (!isSameMergedDates) {
setCalendarValue(clone);

Expand Down Expand Up @@ -187,7 +186,7 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
/** Trigger `onChange` directly without check `disabledDate` */
triggerSubmitChange: (value: ValueType) => boolean,
/** Tell `index` has filled value in it */
hasSubmitValue: (index: number) => boolean,
hasSubmitValue: (index: number, submitIndex: number | null) => boolean,
Copy link
Member

Choose a reason for hiding this comment

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

耦合太高了,active 的 hooks 里可以加个方法来更新 submitValueRef 记录。并且自己提供一个 hasActiveSubmitValue(index)。这里不改,否则到处飞线。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

佬 hasSubmitValue这个方法 只有在这个文件里用了 但是在这一块没法在用这个方法了 我就在这个文件里把这个方法去掉了 现在的代码扫描过不了

] {
const {
// MISC
Expand Down Expand Up @@ -334,8 +333,8 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
);

// ============================ Check =============================
function hasSubmitValue(index: number) {
return !!submitValue()[index];
function hasSubmitValue(index: number, submitIndex: number | null) {
return submitIndex === index;
}

// ============================ Return ============================
Expand Down
34 changes: 34 additions & 0 deletions tests/new-range.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,40 @@ describe('NewPicker.Range', () => {
expect(onChange).toHaveBeenCalledWith(expect.anything(), ['06:00:00', '11:00:00']);
});

it('Field switch should be locked even when the field already has the values', () => {
const onChange = jest.fn();

const { container } = render(<DayRangePicker onChange={onChange} showTime />);
openPicker(container);
selectCell(15);
fireEvent.click(document.querySelector('.rc-picker-ok button'));

selectCell(16);
fireEvent.click(document.querySelector('.rc-picker-ok button'));

expect(onChange).toHaveBeenCalledWith(expect.anything(), [
'1990-09-15 00:00:00',
'1990-09-16 00:00:00',
]);

onChange.mockReset();
openPicker(container, 0);
selectCell(1);
openPicker(container, 1);
expect(container.querySelectorAll('input')[0]).toHaveFocus();
expect(container.querySelectorAll('input')[1]).not.toHaveFocus();

fireEvent.click(document.querySelector('.rc-picker-ok button'));
openPicker(container, 1);
expect(container.querySelectorAll('input')[1]).toHaveFocus();
selectCell(2);
fireEvent.click(document.querySelector('.rc-picker-ok button'));
expect(onChange).toHaveBeenCalledWith(expect.anything(), [
'1990-09-01 00:00:00',
'1990-09-02 00:00:00',
]);
});

it('double click to confirm if needConfirm', () => {
const onChange = jest.fn();

Expand Down
Loading