An open source design system that's fully customizable and agent ready
TypeScript
fix(Calendar): navigate internally when only onFocusDateChange is set (#10180)
Cindy Zhangbfc4940eccb2
diff --git a/.changeset/calendar-listener-without-focus-date.md b/.changeset/calendar-listener-without-focus-date.mdnew file mode 100644index 0000000000..cb4aa7e98e--- /dev/null+++ b/.changeset/calendar-listener-without-focus-date.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `Calendar` and `useCalendarNavigation` keep moving the visible month when `onFocusDateChange` is given without `focusDate`. The focus date is controlled only when both props are present, as the imperative `navigateTo` handle already treated it; but the Previous/Next buttons and keyboard month crossing switched on the callback alone, so a listener-only calendar reported the new month and then stayed on the old one, with no way to move. The callback still fires in every mode.++@thecodebasedotdiff --git a/packages/core/src/Calendar/Calendar.test.tsx b/packages/core/src/Calendar/Calendar.test.tsxindex 52e069eb02..458ce60712 100644--- a/packages/core/src/Calendar/Calendar.test.tsx+++ b/packages/core/src/Calendar/Calendar.test.tsx@@ -483,6 +483,35 @@ describe('Calendar', () => {expect(screen.getByText('February 2026')).toBeInTheDocument();});+ it('still moves the visible month when only onFocusDateChange is given', async () => {+ const user = userEvent.setup();+ const handleFocusChange = vi.fn();++ // A listener alone does not make the focus date controlled (that needs+ // focusDate too), so the calendar must keep moving on its own.+ render(+ <Calendar+ defaultValue="2026-01-15"+ onFocusDateChange={handleFocusChange}+ />,+ );+ expect(screen.getByText('January 2026')).toBeInTheDocument();++ await user.click(getButton('Next month'));++ expect(handleFocusChange).toHaveBeenCalledWith('2026-02-01');+ expect(screen.getByText('February 2026')).toBeInTheDocument();++ // Crossing a month with the keyboard takes the same path.+ const jan31 = screen.queryByRole('button', {name: /January 31/});+ expect(jan31).toBeNull();+ const feb28 = getButton(/February 28/);+ feb28.focus();+ await user.keyboard('{ArrowRight}');+ expect(handleFocusChange).toHaveBeenLastCalledWith('2026-03-01');+ expect(screen.getByText('March 2026')).toBeInTheDocument();+ });+it('calls onFocusDateChange when navigating', async () => {const user = userEvent.setup();const handleFocusChange = vi.fn();diff --git a/packages/core/src/Calendar/Calendar.tsx b/packages/core/src/Calendar/Calendar.tsxindex 793c86cbf3..2e8e070eb2 100644--- a/packages/core/src/Calendar/Calendar.tsx+++ b/packages/core/src/Calendar/Calendar.tsx@@ -509,9 +509,8 @@ export function Calendar({ref, ...props}: CalendarProps) {return;}const newPd = calendarAddMonths(calendar, baseMonth, delta).start;- if (onFocusDateChange) {- onFocusDateChange(plainDateToISO(newPd));- } else {+ onFocusDateChange?.(plainDateToISO(newPd));+ if (!isControlledFocus) {setInternalFocusDate(newPd);}return;@@ -586,9 +585,8 @@ export function Calendar({ref, ...props}: CalendarProps) {const newPd = isBeforeShown? targetMonth.start: calendarAddMonths(calendar, targetMonth, -(numberOfMonths - 1)).start;- if (onFocusDateChange) {- onFocusDateChange(plainDateToISO(newPd));- } else {+ onFocusDateChange?.(plainDateToISO(newPd));+ if (!isControlledFocus) {setInternalFocusDate(newPd);}},@@ -597,6 +595,7 @@ export function Calendar({ref, ...props}: CalendarProps) {calendar,canNavigateNext,canNavigatePrevious,+ isControlledFocus,isRootDateDisabled,lastVisibleMonth,maxDateForKeys,diff --git a/packages/core/src/Calendar/hooks/useCalendarNavigation.test.ts b/packages/core/src/Calendar/hooks/useCalendarNavigation.test.tsindex 9dcc9b6857..46aa9b30c4 100644--- a/packages/core/src/Calendar/hooks/useCalendarNavigation.test.ts+++ b/packages/core/src/Calendar/hooks/useCalendarNavigation.test.ts@@ -104,6 +104,24 @@ describe('useCalendarNavigation — uncontrolled navigation', () => {// Controlled mode// =============================================================================+describe('useCalendarNavigation — listener without focusDate', () => {+ it('moves itself and reports the new month', () => {+ // onFocusDateChange alone is NOT controlled (that needs focusDate too),+ // so navigation must still update the internal month.+ const onFocusDateChange = vi.fn();+ const {result} = renderHook(() =>+ useCalendarNavigation({initialValue: '2026-05-20', onFocusDateChange}),+ );++ act(() => {+ result.current.navigateMonth(1);+ });++ expect(onFocusDateChange).toHaveBeenCalledWith('2026-06-01');+ expect(result.current.baseMonth).toEqual({year: 2026, month: 6, day: 1});+ });+});+describe('useCalendarNavigation — controlled mode', () => {it('reports the new month through the callback without moving itself', () => {const onFocusDateChange = vi.fn();diff --git a/packages/core/src/Calendar/hooks/useCalendarNavigation.ts b/packages/core/src/Calendar/hooks/useCalendarNavigation.tsindex 0806559b18..959de35bbd 100644--- a/packages/core/src/Calendar/hooks/useCalendarNavigation.ts+++ b/packages/core/src/Calendar/hooks/useCalendarNavigation.ts@@ -199,13 +199,12 @@ export function useCalendarNavigation(setPendingFocus(plainDateToISO(targetDate));}- if (onFocusDateChange) {- onFocusDateChange(newISO);- } else {+ onFocusDateChange?.(newISO);+ if (!isControlledFocus) {setInternalFocusDate(newMonth);}},- [calendar, shownMonths, onFocusDateChange],+ [calendar, shownMonths, isControlledFocus, onFocusDateChange],);// Clear pending focus