Skip to content
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

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

Merged
merged 19 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions src/PickerInput/RangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ function RangePicker<DateType extends object = any>(
setActiveIndex,
nextActiveIndex,
activeIndexList,
updateSubmitIndex,
hasActiveSubmitValue,
] = useRangeActive(disabled, allowEmpty, mergedOpen);

const onSharedFocus = (event: React.FocusEvent<HTMLElement>, index?: number) => {
Expand Down Expand Up @@ -325,8 +327,6 @@ function RangePicker<DateType extends object = any>(
flushSubmit,
/** Trigger `onChange` directly without check `disabledDate` */
triggerSubmitChange,
/** Tell `index` has filled value in it */
hasSubmitValue,
Copy link
Member

Choose a reason for hiding this comment

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

这个既然没用了,是不是可以从 hooks 里删了?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

] = useRangeValue<RangeValueType<DateType>, DateType>(
filledProps,
mergedValue,
Expand Down Expand Up @@ -413,7 +413,7 @@ function RangePicker<DateType extends object = any>(
if (date) {
nextValue = fillCalendarValue(date, activeIndex);
}

updateSubmitIndex(activeIndex);
// Get next focus index
const nextIndex = nextActiveIndex(nextValue);

Expand Down Expand Up @@ -641,7 +641,7 @@ function RangePicker<DateType extends object = any>(
needConfirm &&
// Not change index if is not filled
!allowEmpty[lastActiveIndex] &&
!hasSubmitValue(lastActiveIndex) &&
!hasActiveSubmitValue(lastActiveIndex) &&
calendarValue[lastActiveIndex]
) {
selectorRef.current.focus({ index: lastActiveIndex });
Expand Down
15 changes: 14 additions & 1 deletion src/PickerInput/hooks/useRangeActive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ export default function useRangeActive<DateType>(
setActiveIndex: (index: number) => void,
nextActiveIndex: NextActive<DateType>,
activeList: number[],
updateSubmitIndex: (index: number | null) => void,
hasActiveSubmitValue: (index: number) => boolean,
] {
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 updateSubmitIndex = (index: number | null) => {
submitIndexRef.current = index;
};

const hasActiveSubmitValue = (index: number) => {
return submitIndexRef.current === index;
};

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

Expand All @@ -79,5 +90,7 @@ export default function useRangeActive<DateType>(
setActiveIndex,
nextActiveIndex,
activeListRef.current,
updateSubmitIndex,
hasActiveSubmitValue,
];
}
10 changes: 1 addition & 9 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 @@ -186,8 +185,6 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
flushSubmit: (index: number, needTriggerChange: boolean) => void,
/** Trigger `onChange` directly without check `disabledDate` */
triggerSubmitChange: (value: ValueType) => boolean,
/** Tell `index` has filled value in it */
hasSubmitValue: (index: number) => boolean,
] {
const {
// MISC
Expand Down Expand Up @@ -333,11 +330,6 @@ export default function useRangeValue<ValueType extends DateType[], DateType ext
2,
);

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

// ============================ Return ============================
return [flushSubmit, triggerSubmit, hasSubmitValue];
return [flushSubmit, triggerSubmit];
}
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