Skip to content

Commit 6227cef

Browse files
committed
Fix and test turning goToRangeStartOnSelect off
1 parent f56c7ae commit 6227cef

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/Calendar.spec.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { act, render } from '@testing-library/react';
44
import { getMonthStart } from '@wojtekmaj/date-utils';
55

66
import Calendar from './Calendar';
7+
import { LooseValue } from './shared/types';
78

89
const { format } = new Intl.DateTimeFormat('en-US', {
910
day: 'numeric',
@@ -707,6 +708,85 @@ describe('Calendar', () => {
707708

708709
expect(onActiveStartDateChange).not.toHaveBeenCalled();
709710
});
711+
712+
it('returns to start of range when range is completed', () => {
713+
const onActiveStartDateChange = vi.fn();
714+
715+
const initialRange: LooseValue = [new Date(2017, 0, 10), null];
716+
717+
const { container } = render(
718+
<Calendar
719+
view="month"
720+
selectRange
721+
onActiveStartDateChange={onActiveStartDateChange}
722+
defaultValue={initialRange}
723+
/>,
724+
);
725+
726+
const nextMonthButton = container.querySelector(
727+
'button.react-calendar__navigation__next-button',
728+
) as HTMLButtonElement;
729+
730+
act(() => {
731+
nextMonthButton.click();
732+
});
733+
// Disregard the call on the above button click
734+
onActiveStartDateChange.mockClear();
735+
736+
// First day not in the previous month
737+
const firstDayTile = container.querySelector(
738+
'.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])',
739+
) as HTMLButtonElement;
740+
741+
act(() => {
742+
firstDayTile.click();
743+
});
744+
745+
expect(onActiveStartDateChange).toHaveBeenCalledWith(
746+
expect.objectContaining({
747+
action: 'onChange',
748+
activeStartDate: new Date(2017, 0, 1),
749+
view: 'month',
750+
}),
751+
);
752+
});
753+
754+
it('does not change activeStartDate on range completion when goToRangeStartOnSelect is set to false', () => {
755+
const onActiveStartDateChange = vi.fn();
756+
757+
const initialRange: LooseValue = new Date(2017, 0, 10);
758+
759+
const { container } = render(
760+
<Calendar
761+
view="month"
762+
selectRange
763+
onActiveStartDateChange={onActiveStartDateChange}
764+
defaultValue={initialRange}
765+
goToRangeStartOnSelect={false}
766+
/>,
767+
);
768+
769+
const nextMonthButton = container.querySelector(
770+
'button.react-calendar__navigation__next-button',
771+
) as HTMLButtonElement;
772+
773+
act(() => {
774+
nextMonthButton.click();
775+
});
776+
// Disregard the call on the above button click
777+
onActiveStartDateChange.mockClear();
778+
779+
// First day not in the previous month
780+
const firstDayTile = container.querySelector(
781+
'.react-calendar__tile:not([react-calendar__month-view__days__day--weekend])',
782+
) as HTMLButtonElement;
783+
784+
act(() => {
785+
firstDayTile.click();
786+
});
787+
788+
expect(onActiveStartDateChange).not.toHaveBeenCalled();
789+
});
710790
});
711791

712792
describe('handles view change properly', () => {

src/Calendar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
496496
this.setState(nextState, () => {
497497
const args = {
498498
action: nextState.action,
499-
activeStartDate: nextState.activeStartDate || this.activeStartDate,
499+
activeStartDate: nextState.activeStartDate || prevArgs.activeStartDate,
500500
value: ('value' in nextState && nextState.value) || this.value,
501501
view: ('view' in nextState && nextState.view) || this.view,
502502
};
@@ -691,7 +691,7 @@ export default class Calendar extends Component<CalendarProps, CalendarState> {
691691
...(this.props as CalendarPropsWithDefaults),
692692
value: nextValue,
693693
})
694-
: null;
694+
: this.activeStartDate;
695695

696696
event.persist();
697697

0 commit comments

Comments
 (0)