Skip to content

Commit e6e3f72

Browse files
committed
CalendarContextProvider - fix the return update source (#2648)
* CalendarContextProvider - fix the return update source from events to avoid breaking change * fix tests
1 parent c93414a commit e6e3f72

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/expandableCalendar/Context/Provider.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,30 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
8080
}
8181
}, [date]);
8282

83+
const getUpdateSource = useCallback((updateSource: UpdateSources) => {
84+
// NOTE: this comes to avoid breaking those how listen to the update source in onDateChanged and onMonthChange - remove on V2
85+
if (updateSource === UpdateSources.ARROW_PRESS || updateSource === UpdateSources.WEEK_ARROW_PRESS) {
86+
return UpdateSources.PAGE_SCROLL;
87+
}
88+
return updateSource;
89+
}, []);
90+
8391
const _setDate = useCallback((date: string, updateSource: UpdateSources) => {
8492
prevDate.current = currDate.current;
8593
currDate.current = date;
94+
8695
setCurrentDate(date);
8796
if (!includes(disableAutoDaySelection, updateSource as string)) {
8897
setSelectedDate(date);
8998
}
9099
setUpdateSource(updateSource);
91100

92-
onDateChanged?.(date, updateSource);
93-
101+
const _updateSource = getUpdateSource(updateSource);
102+
onDateChanged?.(date, _updateSource);
94103
if (!sameMonth(new XDate(date), new XDate(prevDate.current))) {
95-
onMonthChange?.(xdateToData(new XDate(date)), updateSource);
104+
onMonthChange?.(xdateToData(new XDate(date)), _updateSource);
96105
}
97-
}, [onDateChanged, onMonthChange]);
106+
}, [onDateChanged, onMonthChange, getUpdateSource]);
98107

99108
const _setDisabled = useCallback((disabled: boolean) => {
100109
if (showTodayButton) {

src/expandableCalendar/__tests__/index.spec.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ describe('ExpandableCalendar', () => {
185185
jest.runAllTimers();
186186
const expectedDate = today.clone().setDate(1).addMonths(direction === Direction.RIGHT ? 1 : -1);
187187
driver.pressOnHeaderArrow({left: direction === Direction.LEFT});
188-
expect(onDateChanged).toHaveBeenCalledWith(toMarkingFormat(expectedDate), UpdateSources.ARROW_PRESS);
189-
expect(onMonthChange).toHaveBeenCalledWith(xdateToData(expectedDate), UpdateSources.ARROW_PRESS);
188+
expect(onDateChanged).toHaveBeenCalledWith(toMarkingFormat(expectedDate), UpdateSources.PAGE_SCROLL);
189+
expect(onMonthChange).toHaveBeenCalledWith(xdateToData(expectedDate), UpdateSources.PAGE_SCROLL);
190190
});
191191

192192
it(`should call onDateChanged and onMonthChanged for first day in initial month when changing to initial month`, () => {
@@ -197,8 +197,8 @@ describe('ExpandableCalendar', () => {
197197
driver.pressOnHeaderArrow({left: true});
198198
jest.runAllTimers();
199199
const expectedDate = today.clone().setDate(1);
200-
expect(onDateChanged).toHaveBeenNthCalledWith(2, toMarkingFormat(expectedDate), UpdateSources.ARROW_PRESS);
201-
expect(onMonthChange).toHaveBeenNthCalledWith(2, xdateToData(expectedDate), UpdateSources.ARROW_PRESS);
200+
expect(onDateChanged).toHaveBeenNthCalledWith(2, toMarkingFormat(expectedDate), UpdateSources.PAGE_SCROLL);
201+
expect(onMonthChange).toHaveBeenNthCalledWith(2, xdateToData(expectedDate), UpdateSources.PAGE_SCROLL);
202202
});
203203

204204
it(`should navigate 6 months ahead and back successfully`, () => {
@@ -209,13 +209,13 @@ describe('ExpandableCalendar', () => {
209209
});
210210
jest.runAllTimers();
211211
const expectedFutureDate = today.clone().setDate(1).addMonths(6);
212-
expect(onDateChanged).toHaveBeenNthCalledWith(6, toMarkingFormat(expectedFutureDate), UpdateSources.ARROW_PRESS);
213-
expect(onMonthChange).toHaveBeenNthCalledWith(6, xdateToData(expectedFutureDate), UpdateSources.ARROW_PRESS);
212+
expect(onDateChanged).toHaveBeenNthCalledWith(6, toMarkingFormat(expectedFutureDate), UpdateSources.PAGE_SCROLL);
213+
expect(onMonthChange).toHaveBeenNthCalledWith(6, xdateToData(expectedFutureDate), UpdateSources.PAGE_SCROLL);
214214
times(6, () => driver.pressOnHeaderArrow({left: true}));
215215
jest.runAllTimers();
216216
const expectedDate = today.clone().setDate(1);
217-
expect(onDateChanged).toHaveBeenNthCalledWith(12, toMarkingFormat(expectedDate), UpdateSources.ARROW_PRESS);
218-
expect(onMonthChange).toHaveBeenNthCalledWith(12, xdateToData(expectedDate), UpdateSources.ARROW_PRESS);
217+
expect(onDateChanged).toHaveBeenNthCalledWith(12, toMarkingFormat(expectedDate), UpdateSources.PAGE_SCROLL);
218+
expect(onMonthChange).toHaveBeenNthCalledWith(12, xdateToData(expectedDate), UpdateSources.PAGE_SCROLL);
219219
});
220220
});
221221
});
@@ -247,14 +247,14 @@ describe('ExpandableCalendar', () => {
247247
const currentDay = today.getDay();
248248
const expectedDate = today.clone().addDays(direction === Direction.LEFT ? -(currentDay + 7) : (7 - currentDay));
249249
driver.pressOnHeaderArrow({left: direction === Direction.LEFT});
250-
expect(onDateChanged).toHaveBeenCalledWith(toMarkingFormat(expectedDate), UpdateSources.WEEK_ARROW_PRESS);
250+
expect(onDateChanged).toHaveBeenCalledWith(toMarkingFormat(expectedDate), UpdateSources.PAGE_SCROLL);
251251
});
252252

253253
it(`should call onDateChanged for first day of initial week when changing to initial week`, () => {
254254
driver.pressOnHeaderArrow({left: false});
255255
driver.pressOnHeaderArrow({left: true});
256256
const expectedDate = today.clone().addDays(-(today.getDay()));
257-
expect(onDateChanged).toHaveBeenNthCalledWith(2, toMarkingFormat(expectedDate), UpdateSources.WEEK_ARROW_PRESS);
257+
expect(onDateChanged).toHaveBeenNthCalledWith(2, toMarkingFormat(expectedDate), UpdateSources.PAGE_SCROLL);
258258
});
259259

260260
it('should fetch next weeks when in last week of the list', () => {
@@ -270,7 +270,7 @@ describe('ExpandableCalendar', () => {
270270
const diff = Math.ceil(((endOfMonth.getUTCDate() + 1) - today.getUTCDate()) / 7) + ((today.getUTCDay() > endOfMonth.getUTCDay()) ? 1 : 0);
271271
const expectedDate = today.clone().setDate(today.getDate() + 7 * diff - today.getDay());
272272
times(diff, () => driver.pressOnHeaderArrow({left: false}));
273-
expect(onMonthChange).toHaveBeenCalledWith(xdateToData(expectedDate), UpdateSources.WEEK_ARROW_PRESS);
273+
expect(onMonthChange).toHaveBeenCalledWith(xdateToData(expectedDate), UpdateSources.PAGE_SCROLL);
274274
});
275275
});
276276
});

0 commit comments

Comments
 (0)