Skip to content

Commit ba61d2a

Browse files
committed
fix(DateInput, DateTimeInput): the month/year wheels move the date like UIDatePicker's
Turning the month or year wheel behind the calendar title moves a chosen date to the new month, keeping its day (clamped to the month's length and the bounds; DateTimeInput keeps the time); with no date chosen the wheels only move the calendar. The month wheel loops without changing the year, and a year that takes the month out of range turns the month wheel onto the nearest reachable month on UIDatePicker's column turn.
1 parent 856179f commit ba61d2a

8 files changed

Lines changed: 357 additions & 19 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] The month and year wheels behind the calendar title in the DateInput and DateTimeInput bottom sheets now behave like the iOS date picker's:
6+
7+
- Turning a wheel moves a chosen date to the new month and keeps its day, so October 15 becomes May 15. A day the month lacks becomes its last day, the result stays within `min` and `max`, and DateTimeInput keeps the time.
8+
- With no date chosen, the wheels only move the calendar.
9+
- The month wheel loops from December to January without changing the year.
10+
- A year that takes the month out of range turns the month wheel onto the nearest reachable month instead of jumping.
11+
12+
@vjeux

‎packages/core/src/DateInput/DateInputTouch.test.tsx‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
monthIndexOf,
5050
fromMonthIndex,
5151
clampIndex,
52+
dateInMonth,
5253
paneWindow,
5354
rowsIn,
5455
DEFAULT_MONTH_REACH,
@@ -1327,6 +1328,85 @@ describe('DateInput — month/year wheels', () => {
13271328
expect(title()).toHaveTextContent('March 2025');
13281329
});
13291330

1331+
/**
1332+
* UIDatePicker's inline calendar: turning the month or year wheel changes
1333+
* the date, keeping its day (datePicker.calendar.monthYearWheels
1334+
* .changesDate), clamped to the month and the bounds.
1335+
*/
1336+
it('moves the chosen date with the wheels, keeping its day', () => {
1337+
const onChange = vi.fn();
1338+
renderAndOpen(
1339+
<Controlled initial="2026-03-21" {...ONE_YEAR} onChange={onChange} />,
1340+
);
1341+
openWheels();
1342+
fireEvent.click(
1343+
within(screen.getByRole('listbox', {name: 'Month'})).getByText('May'),
1344+
);
1345+
expect(onChange).toHaveBeenLastCalledWith('2026-05-21');
1346+
expect(field()).toHaveValue('May 21, 2026');
1347+
});
1348+
1349+
it('clamps a day the new month lacks to its last day', () => {
1350+
const onChange = vi.fn();
1351+
renderAndOpen(
1352+
<Controlled initial="2026-03-31" {...ONE_YEAR} onChange={onChange} />,
1353+
);
1354+
openWheels();
1355+
fireEvent.click(
1356+
within(screen.getByRole('listbox', {name: 'Month'})).getByText('April'),
1357+
);
1358+
expect(onChange).toHaveBeenLastCalledWith('2026-04-30');
1359+
});
1360+
1361+
it('loops the month wheel without changing the year', () => {
1362+
const onChange = vi.fn();
1363+
renderAndOpen(
1364+
<Controlled initial="2026-12-10" {...ONE_YEAR} onChange={onChange} />,
1365+
);
1366+
openWheels();
1367+
fireEvent.keyDown(screen.getByRole('listbox', {name: 'Month'}), {
1368+
key: 'ArrowDown',
1369+
});
1370+
expect(title()).toHaveTextContent('January 2026');
1371+
expect(onChange).toHaveBeenLastCalledWith('2026-01-10');
1372+
});
1373+
1374+
it('brings a year change past the range back onto it, date and month wheel too', async () => {
1375+
const onChange = vi.fn();
1376+
renderAndOpen(
1377+
<Controlled
1378+
initial="2026-03-21"
1379+
min="2025-06-01"
1380+
max="2026-12-31"
1381+
onChange={onChange}
1382+
/>,
1383+
);
1384+
openWheels();
1385+
fireEvent.click(
1386+
within(screen.getByRole('listbox', {name: 'Year'})).getByText('2025'),
1387+
);
1388+
// March 2025 is before the range: June 2025, the first reachable month.
1389+
expect(title()).toHaveTextContent('June 2025');
1390+
expect(onChange).toHaveBeenLastCalledWith('2025-06-21');
1391+
const months = screen.getByRole('listbox', {name: 'Month'});
1392+
await waitFor(() =>
1393+
expect(
1394+
document.getElementById(months.getAttribute('aria-activedescendant')!),
1395+
).toHaveTextContent('June'),
1396+
);
1397+
});
1398+
1399+
it('only moves the calendar when no date is chosen', () => {
1400+
const onChange = vi.fn();
1401+
renderAndOpen(<Controlled {...ONE_YEAR} onChange={onChange} />);
1402+
openWheels();
1403+
fireEvent.click(
1404+
within(screen.getByRole('listbox', {name: 'Month'})).getByText('May'),
1405+
);
1406+
expect(title()).toHaveTextContent('May 2026');
1407+
expect(onChange).not.toHaveBeenCalled();
1408+
});
1409+
13301410
it('is a single tab stop driven by the arrow keys', () => {
13311411
renderAndOpen();
13321412
openWheels();
@@ -1787,6 +1867,29 @@ describe('monthGeometry', () => {
17871867
);
17881868
});
17891869

1870+
it('moves a date to another month keeping its day, within the month and the bounds', () => {
1871+
const d = (year: number, month: number, day: number) => ({
1872+
year,
1873+
month,
1874+
day,
1875+
});
1876+
expect(dateInMonth(d(2026, 10, 15), toMonthIndex(2026, 5))).toEqual(
1877+
d(2026, 5, 15),
1878+
);
1879+
expect(dateInMonth(d(2026, 1, 31), toMonthIndex(2026, 2))).toEqual(
1880+
d(2026, 2, 28),
1881+
);
1882+
expect(dateInMonth(d(2024, 1, 31), toMonthIndex(2024, 2))).toEqual(
1883+
d(2024, 2, 29),
1884+
);
1885+
expect(
1886+
dateInMonth(d(2026, 6, 10), toMonthIndex(2026, 5), d(2026, 5, 20)),
1887+
).toEqual(d(2026, 5, 20));
1888+
expect(
1889+
dateInMonth(d(2026, 6, 25), toMonthIndex(2026, 7), null, d(2026, 7, 4)),
1890+
).toEqual(d(2026, 7, 4));
1891+
});
1892+
17901893
it('clamps to the reachable range', () => {
17911894
expect(clampIndex(5, 10, 20)).toBe(10);
17921895
expect(clampIndex(25, 10, 20)).toBe(20);

‎packages/core/src/DateInput/MonthYearWheels.tsx‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
* @output Exports MonthYearWheels — the month + year wheel pair
99
* @position Internal component; consumed by TouchDateField.tsx
1010
*
11-
* The shortcut out of continuous scrolling. Scrolling is the right gesture for
12-
* "a month or two either way" and the wrong one for "December 2019", so the
13-
* header title opens this instead: two wheels, one flick each.
11+
* The month and year wheels behind the calendar's title, as UIDatePicker's
12+
* inline calendar has them (motion/uikit/datePicker.ts
13+
* `calendar.monthYearWheels`): the month wheel loops (December is followed
14+
* by January, the year unchanged), and a year that lacks the month shown in
15+
* the range turns the month wheel onto the nearest reachable month, on the
16+
* picker's own column turn, instead of making it jump.
1417
*
15-
* It occupies exactly the same box as the month scroller, so opening it never
18+
* It occupies exactly the same box as the month pager, so opening it never
1619
* changes the picker's height.
1720
*
1821
* SYNC: When modified, update:
@@ -22,15 +25,22 @@
2225
* - /packages/core/src/DateInput/DateInputTouch.test.tsx
2326
*/
2427

25-
import {useMemo} from 'react';
28+
import {useMemo, useRef} from 'react';
2629
import * as stylex from '@stylexjs/stylex';
2730
import {useLocale} from '../i18n';
2831
import {DATE_FORMAT_MONTH_ONLY, plainDateFormat} from '../utils';
2932
import {dateInputTouchGeometry} from './tokens.stylex';
30-
import {Wheel, type WheelOption} from './Wheel';
33+
import {datePicker} from '../motion/uikit/datePicker';
34+
import {Wheel, type WheelHandle, type WheelOption} from './Wheel';
3135
import {WheelGroup} from './WheelGroup';
3236
import {fromMonthIndex, toMonthIndex} from './monthGeometry';
3337

38+
/** UIDatePicker's turn of a column it moves by itself. */
39+
const COLUMN_TURN = {
40+
duration: datePicker.wheels.columnTurn.durationMs / 1000,
41+
curve: datePicker.wheels.columnTurn.curve,
42+
};
43+
3444
const styles = stylex.create({
3545
// The picker (UIPickerView's 216 px) centred in the month pane.
3646
wheels: {
@@ -71,6 +81,7 @@ export function MonthYearWheels({
7181
}: MonthYearWheelsProps) {
7282
const locale = useLocale();
7383
const {year, month} = fromMonthIndex(monthIndex);
84+
const monthWheelRef = useRef<WheelHandle | null>(null);
7485

7586
// Day 15 of a fixed year: no timezone can push it into an adjacent month
7687
// the way day 1 or day 31 can.
@@ -127,9 +138,11 @@ export function MonthYearWheels({
127138
<div {...stylex.props(styles.wheels)}>
128139
<WheelGroup>
129140
<Wheel
141+
ref={monthWheelRef}
130142
label={monthLabel}
131143
options={monthOptions}
132144
value={month}
145+
isLooping={datePicker.calendar.monthYearWheels.monthLoops}
133146
isActive={isActive}
134147
onChange={nextMonth => onChange(toMonthIndex(year, nextMonth))}
135148
/>
@@ -139,12 +152,21 @@ export function MonthYearWheels({
139152
value={year}
140153
isActive={isActive}
141154
onChange={nextYear => {
142-
// Jan 31 -> Feb has no equivalent here (months carry no day), but a
143-
// year change can still land outside the range; clamp to it.
155+
// A year change can land outside the range: clamp to it, and turn
156+
// the month wheel onto the clamped month rather than letting it
157+
// jump there (UIKit turns a column past a bound back onto it).
144158
const candidate = toMonthIndex(nextYear, month);
145-
onChange(
146-
Math.min(Math.max(candidate, minMonthIndex), maxMonthIndex),
159+
const clamped = Math.min(
160+
Math.max(candidate, minMonthIndex),
161+
maxMonthIndex,
147162
);
163+
if (clamped !== candidate) {
164+
void monthWheelRef.current?.spinTo(
165+
fromMonthIndex(clamped).month - 1,
166+
COLUMN_TURN,
167+
);
168+
}
169+
onChange(clamped);
148170
}}
149171
/>
150172
</WheelGroup>

‎packages/core/src/DateInput/TouchDateField.tsx‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ import {
104104
themeProps,
105105
formatSharedDate,
106106
plainDateFromISO,
107+
plainDateIsEqual,
108+
plainDateToISO,
107109
plainDateToday,
108110
plainDateFormat,
109111
DATE_FORMAT_MONTH_YEAR,
@@ -119,6 +121,7 @@ import {MonthYearWheels} from './MonthYearWheels';
119121
import {
120122
DEFAULT_MONTH_REACH,
121123
clampIndex,
124+
dateInMonth,
122125
fromMonthIndex,
123126
monthIndexOf,
124127
} from './monthGeometry';
@@ -895,10 +898,29 @@ export function TouchDateField({
895898
// A wheel commit steers the scroller immediately, even though it is behind
896899
// the wheels: it keeps its layout box while hidden, so by the time the
897900
// wheels close it is already resting on the new month.
898-
const handleWheelChange = useCallback((next: number) => {
899-
setMonthIndex(next);
900-
scrollerHandleRef.current?.scrollToMonth(next, 'auto');
901-
}, []);
901+
// Turning the month or year wheel moves the calendar, and moves a chosen
902+
// date with it, keeping its day (UIDatePicker's inline calendar:
903+
// datePicker.calendar.monthYearWheels.changesDate), clamped to the month
904+
// and the bounds. A date a custom constraint refuses stays where it was.
905+
const handleWheelChange = useCallback(
906+
(next: number) => {
907+
setMonthIndex(next);
908+
scrollerHandleRef.current?.scrollToMonth(next, 'auto');
909+
if (selectedDate == null) {
910+
return;
911+
}
912+
const moved = dateInMonth(
913+
selectedDate,
914+
next,
915+
min != null ? plainDateFromISO(min) : null,
916+
max != null ? plainDateFromISO(max) : null,
917+
);
918+
if (!plainDateIsEqual(moved, selectedDate) && !isDateDisabled(moved)) {
919+
fireChange(plainDateToISO(moved));
920+
}
921+
},
922+
[selectedDate, min, max, isDateDisabled, fireChange],
923+
);
902924

903925
/**
904926
* The calendar reports the month it has scrolled to — but only while it is
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file MonthYearWheels.interaction.browser.spec.ts
5+
* @input The built "Bottom Sheet Calendar" DateInput story (October 15, 2026
6+
* in a 2026 range), real touch input and the interaction harness
7+
* @output Interaction proofs for the month/year wheels behind the calendar
8+
* title moving the date like UIDatePicker's: a drag of the month wheel moves
9+
* the chosen date to the next month keeping its day, and the month wheel
10+
* loops past December to January of the same year
11+
* @position Interaction tests of MonthYearWheels.tsx and TouchDateField.tsx
12+
* (`*.interaction.browser.spec.ts`, run by `pnpm test:interaction`)
13+
*/
14+
15+
import {expect, test, type Locator, type Page} from '@playwright/test';
16+
import {
17+
drag,
18+
openStory,
19+
touchDriver,
20+
waitForRest,
21+
withStorybook,
22+
type TouchDriver,
23+
} from '@astryxdesign/interaction-harness';
24+
25+
const storybook = withStorybook(test);
26+
27+
async function openWheels(page: Page) {
28+
await openStory(page, storybook(), 'core-dateinput--bottom-sheet-calendar');
29+
const driver = await touchDriver(page);
30+
const tapCenter = async (locator: Locator) => {
31+
const box = await locator.boundingBox();
32+
if (box == null) {
33+
throw new Error('no box');
34+
}
35+
await driver.tap({x: box.x + box.width / 2, y: box.y + box.height / 2});
36+
};
37+
await tapCenter(page.getByRole('button', {name: /calendar/i}).first());
38+
const sheet = page
39+
.getByRole('dialog')
40+
.locator('.astryx-bottom-sheet')
41+
.first();
42+
await waitForRest(sheet);
43+
await tapCenter(page.locator('[data-title="month-year"]'));
44+
const months = page.getByRole('listbox', {name: 'Month'});
45+
await expect(months).toBeVisible();
46+
await waitForRest(page.locator('[data-panel="wheels"]').first());
47+
return {driver, months, field: page.locator('input').first()};
48+
}
49+
50+
/** A slow drag of `rows` rows up (later months), resting before the lift. */
51+
async function turnRows(driver: TouchDriver, column: Locator, rows: number) {
52+
const box = await column.boundingBox();
53+
if (box == null) {
54+
throw new Error('no column');
55+
}
56+
const from = {x: box.x + box.width / 2, y: box.y + box.height / 2};
57+
// 32 px rows plus the pan's hysteresis.
58+
await driver.play(
59+
drag(
60+
from,
61+
{x: from.x, y: from.y - rows * 32 - 10},
62+
{durationMs: 250 + rows * 100, restMs: 150},
63+
),
64+
);
65+
}
66+
67+
async function restingLabel(column: Locator): Promise<string | null> {
68+
return column.evaluate(element => {
69+
const id = element.getAttribute('aria-activedescendant');
70+
return id == null
71+
? null
72+
: (document.getElementById(id)?.textContent ?? null);
73+
});
74+
}
75+
76+
test('a turn of the month wheel moves the chosen date, keeping its day', async ({
77+
page,
78+
}) => {
79+
const {driver, months, field} = await openWheels(page);
80+
expect(await restingLabel(months)).toBe('October');
81+
await turnRows(driver, months, 1);
82+
await expect.poll(async () => restingLabel(months)).toBe('November');
83+
await expect(field).toHaveValue('November 15, 2026');
84+
});
85+
86+
test('the month wheel loops past December to January of the same year', async ({
87+
page,
88+
}) => {
89+
const {driver, months, field} = await openWheels(page);
90+
await turnRows(driver, months, 3);
91+
await expect.poll(async () => restingLabel(months)).toBe('January');
92+
await expect(field).toHaveValue('January 15, 2026');
93+
});

0 commit comments

Comments
 (0)