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(DatePicker): fix uncontrolled default date on calendar #3045

Merged
merged 1 commit into from
Feb 1, 2023
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
13 changes: 8 additions & 5 deletions src/Calendar/useCalendarDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ const useCalendarDate = (value: Date | null | undefined, defaultDate: Date | und
[calendarDate]
);

const resetCalendarDate = useCallback(() => {
setValue(value ?? defaultDate ?? new Date());
}, [defaultDate, value]);
const resetCalendarDate = useCallback(
(nextValue = value) => {
setValue(nextValue ?? defaultDate ?? new Date());
},
[defaultDate, value]
);

useUpdateEffect(() => {
if (value?.valueOf() !== valueRef.current?.valueOf()) {
setCalendarDate(value ?? new Date());
setCalendarDate(value ?? defaultDate ?? new Date());
valueRef.current = value;
}
}, [value]);
}, [value, defaultDate]);

return { calendarDate, setCalendarDate, resetCalendarDate };
};
Expand Down
2 changes: 1 addition & 1 deletion src/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ const DatePicker: RsRefForwardingComponent<'div', DatePickerProps> = React.forwa
const handleClean = useCallback(
(event: React.SyntheticEvent) => {
updateValue(event, null);
resetCalendarDate();
resetCalendarDate(null);
},
[resetCalendarDate, updateValue]
);
Expand Down
40 changes: 40 additions & 0 deletions src/DatePicker/test/DatePickerSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,44 @@ describe('DatePicker', () => {

expect(headerTitle).to.have.text(format(addMonths(today, 1), 'MMM yyyy'));
});

it('Should the calendar date be reset when the controlled value is cleared', () => {
const ref = React.createRef<PickerHandle>();

const App = () => {
const [value, setValue] = React.useState<Date | null>();
return (
<DatePicker
ref={ref as any}
value={value}
onChange={setValue}
format="yyyy-MM-dd HH:mm:ss"
calendarDefaultDate={new Date('2022-02-02 00:00:00')}
ranges={[{ label: 'This day', value: new Date('2023-01-01 10:20:30') }]}
/>
);
};

render(<App />);

fireEvent.click(ref.current?.target as HTMLElement);

const headerDateElement = ref.current?.overlay?.querySelector('.rs-calendar-header-title-date');
const headerTimeElement = ref.current?.overlay?.querySelector('.rs-calendar-header-title-time');

expect(headerDateElement).to.have.text('Feb 2022');
expect(headerTimeElement).to.have.text('00:00:00');

fireEvent.click(screen.getByRole('button', { name: 'This day' }));

expect(headerDateElement).to.have.text('Jan 2023');
expect(headerTimeElement).to.have.text('10:20:30');
expect(ref.current?.target).to.have.text('2023-01-01 10:20:30');

fireEvent.click(screen.getByRole('button', { name: 'Clear' }));
fireEvent.click(ref.current?.target as HTMLElement);

expect(headerDateElement).to.have.text('Feb 2022');
expect(headerTimeElement).to.have.text('00:00:00');
});
});