An open source design system that's fully customizable and agent ready
TypeScript
fix(core): clamp Calendar numberOfMonths to 1|2 (drop console.warn per review) (#2704) (#3248)
Durvesh Pilankarf00b8446d74c
diff --git a/.changeset/calendar-number-of-months-clamp.md b/.changeset/calendar-number-of-months-clamp.mdnew file mode 100644index 0000000000..86977cf468--- /dev/null+++ b/.changeset/calendar-number-of-months-clamp.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Calendar / DateInput / DateTimeInput: defensively clamp `numberOfMonths` to its `1 | 2` type at runtime. Out-of-range values (e.g. `0` rendered nothing, `1000` locked the page up in `Array.from({length})`) fall back to a single month. DateInput and DateTimeInput inherit the guard since they forward the prop to `<Calendar>` (#2704)+@durvesh1992diff --git a/packages/core/src/Calendar/Calendar.test.tsx b/packages/core/src/Calendar/Calendar.test.tsxindex 557baa24e7..445bfe9d67 100644--- a/packages/core/src/Calendar/Calendar.test.tsx+++ b/packages/core/src/Calendar/Calendar.test.tsx@@ -215,6 +215,33 @@ describe('Calendar', () => {expect(screen.getByText(/February 2026.*March 2026/)).toBeInTheDocument();});+ it('clamps out-of-range numberOfMonths to a single month', () => {+ // 1000 would otherwise try to render 1000 month grids and lock the page up.+ render(+ <Calendar+ numberOfMonths={1000 as unknown as 1 | 2}+ focusDate="2026-01-01"+ />,+ );++ // Only the focused month renders — no second month, no range separator.+ expect(screen.getByText('January 2026')).toBeInTheDocument();+ expect(screen.queryByText(/February 2026/)).not.toBeInTheDocument();+ });++ it('clamps numberOfMonths={0} to a single month', () => {+ render(+ <Calendar+ numberOfMonths={0 as unknown as 1 | 2}+ focusDate="2026-01-01"+ />,+ );++ // 0 previously rendered no months at all; now falls back to one.+ expect(screen.getByText('January 2026')).toBeInTheDocument();+ expect(screen.getAllByRole('gridcell').length).toBeGreaterThan(0);+ });+// ─── Display Options ─────────────────────────────────────────it('shows week numbers when hasWeekNumbers is true', () => {diff --git a/packages/core/src/Calendar/Calendar.tsx b/packages/core/src/Calendar/Calendar.tsxindex 09cd23fbd8..086a35e3d9 100644--- a/packages/core/src/Calendar/Calendar.tsx+++ b/packages/core/src/Calendar/Calendar.tsx@@ -203,7 +203,7 @@ export function Calendar({ref, ...props}: CalendarProps) {value,defaultValue,onChange,- numberOfMonths = 1,+ numberOfMonths: numberOfMonthsProp = 1,min,max,dateConstraints,@@ -223,6 +223,11 @@ export function Calendar({ref, ...props}: CalendarProps) {// DayOfWeek so all downstream date math keeps working with an index.const weekStartsOn = normalizeDayOfWeek(weekStartsOnProp);+ // `numberOfMonths` is typed `1 | 2`; defensively clamp anything else that+ // slips through at runtime to 1 so `Array.from({length})` can't render an+ // absurd number of month grids (e.g. `numberOfMonths={1000}`).+ const numberOfMonths = numberOfMonthsProp === 2 ? 2 : 1;+// Today's date (memoized)const today = useMemo(() => plainDateToday(), []);