Skip to content

Commit

Permalink
fix(DatePicker): fix uncontrolled default date on calendar (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Feb 1, 2023
1 parent ddbc325 commit c530b25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
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');
});
});

1 comment on commit c530b25

@vercel
Copy link

@vercel vercel bot commented on c530b25 Feb 1, 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.