An open source design system that's fully customizable and agent ready
TypeScript
fix(DateTimeInput): derive the touch draft time and month clamp during render (#7349)
Cindy Zhangf2ffe0811eee
diff --git a/.changeset/touch-datetime-no-stale-frame.md b/.changeset/touch-datetime-no-stale-frame.mdnew file mode 100644index 0000000000..ffc44063f5--- /dev/null+++ b/.changeset/touch-datetime-no-stale-frame.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] DateTimeInput's touch picker no longer paints a stale frame. Clearing the value from outside clears the time display in the same update, instead of showing the previous time for one frame, and narrowing `max` (or raising `min`) while the sheet is open keeps the visible month in range in the same update, instead of showing the out-of-range month and then jumping.++@cixzhangdiff --git a/packages/core/src/DateTimeInput/DateTimeInputTouch.test.tsx b/packages/core/src/DateTimeInput/DateTimeInputTouch.test.tsxindex 14f97c114d..1f5236e118 100644--- a/packages/core/src/DateTimeInput/DateTimeInputTouch.test.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInputTouch.test.tsx@@ -25,7 +25,7 @@ import {beforeEach,afterEach,} from 'vitest';-import {useState} from 'react';+import {Profiler, useState} from 'react';import * as stylex from '@stylexjs/stylex';import {readFileSync} from 'node:fs';import {render, screen, fireEvent, within} from '@testing-library/react';@@ -986,3 +986,62 @@ describe('DateTimeInput touch surface', () => {expect(field).toHaveAttribute('aria-expanded', 'false');});});++describe('DateTimeInput touch surface: no stale commit', () => {+ // Records what each commit painted, so a value that is corrected one render+ // later (by an effect) shows up as a stale intermediate frame.+ function paintedFrames(read: () => string) {+ const frames: string[] = [];+ const onRender = () => frames.push(read());+ return {frames, onRender};+ }++ it('clears the time display in the same commit as an external clear', () => {+ const {frames, onRender} = paintedFrames(+ () => (timeField() as HTMLInputElement).value,+ );+ const ui = (value: ISODateTimeString | undefined) => (+ <Profiler id="field" onRender={onRender}>+ <DateTimeInput label="Meeting" value={value} onChange={() => {}} />+ </Profiler>+ );+ const {rerender} = render(ui('2026-03-15T10:00' as ISODateTimeString));+ expect(timeField()).toHaveValue('10:00 AM');++ frames.length = 0;+ rerender(ui(undefined));++ expect(frames.length).toBeGreaterThan(0);+ expect(frames).not.toContain('10:00 AM');+ expect(timeField()).toHaveValue('');+ });++ it('shows an in-range month in the same commit that narrows max', () => {+ withMonthLayout(() => {+ const visibleMonth = () =>+ ['March', 'April', 'May', 'June']+ .map(m => `${m} 2026`)+ .find(label => screen.queryByText(label) != null) ?? '';+ const {frames, onRender} = paintedFrames(visibleMonth);+ const ui = (max: ISODateTimeString) => (+ <Profiler id="field" onRender={onRender}>+ <DateTimeInput label="Meeting" max={max} onChange={() => {}} />+ </Profiler>+ );+ const {rerender} = render(ui('2026-12-31T23:59' as ISODateTimeString));+ openDateSheet();+ const next = screen.getByRole('button', {name: 'Next month'});+ fireEvent.click(next);+ fireEvent.click(next);+ fireEvent.click(next);+ expect(screen.getByText('June 2026')).toBeInTheDocument();++ frames.length = 0;+ rerender(ui('2026-04-30T23:59' as ISODateTimeString));++ expect(frames.length).toBeGreaterThan(0);+ expect(frames).not.toContain('June 2026');+ expect(screen.getByText('April 2026')).toBeInTheDocument();+ });+ });+});diff --git a/packages/core/src/DateTimeInput/TouchDateTimeField.tsx b/packages/core/src/DateTimeInput/TouchDateTimeField.tsxindex 349f24681e..54bc6b1aea 100644--- a/packages/core/src/DateTimeInput/TouchDateTimeField.tsx+++ b/packages/core/src/DateTimeInput/TouchDateTimeField.tsx@@ -590,15 +590,25 @@ export function TouchDateTimeField({);const weekStartsOn = normalizeDayOfWeek(weekStartsOnProp);- useEffect(() => {+ // The draft follows the controlled time, and an external clear resets it.+ // Adjusted during render when the value changes, so no commit paints the+ // previous draft.+ const [draftSource, setDraftSource] = useState({+ time: normalizedValueTime,+ value: optimisticValue,+ });+ // Object.is, matching the dependency comparison of the effect this replaces.+ if (+ !Object.is(draftSource.time, normalizedValueTime) ||+ !Object.is(draftSource.value, optimisticValue)+ ) {+ setDraftSource({time: normalizedValueTime, value: optimisticValue});if (normalizedValueTime !== undefined) {- // eslint-disable-next-line @eslint-react/set-state-in-effect -- mirrors an externally controlled time into the sheet-local draftsetDraftTime(normalizedValueTime);} else if (optimisticValue === undefined) {- // eslint-disable-next-line @eslint-react/set-state-in-effect -- external clear resets the sheet-local draft toosetDraftTime(undefined);}- }, [normalizedValueTime, optimisticValue]);+ }const [anchorMonthIndex] = useState(() =>monthIndexOf(selectedDate ?? plainDateToday()),@@ -614,19 +624,23 @@ export function TouchDateTimeField({const [monthIndex, setMonthIndex] = useState(() =>clampIndex(anchorMonthIndex, minMonthIndex, maxMonthIndex),);+ // min/max can narrow underneath the mounted sheet. Keep the visible month in+ // range during render, so no commit shows an out-of-range month; the month+ // scroller is keyed on the range and remounts at the clamped month.+ const clampedMonthIndex = clampIndex(+ monthIndex,+ minMonthIndex,+ maxMonthIndex,+ );+ // Object.is, matching the dependency comparison of the effect this replaces+ // (a NaN index would otherwise never compare equal and re-render forever).+ if (!Object.is(clampedMonthIndex, monthIndex)) {+ setMonthIndex(clampedMonthIndex);+ }const {year, month} = fromMonthIndex(monthIndex);const canStepBack = monthIndex > minMonthIndex;const canStepForward = monthIndex < maxMonthIndex;- useEffect(() => {- const clamped = clampIndex(monthIndex, minMonthIndex, maxMonthIndex);- if (clamped !== monthIndex) {- // eslint-disable-next-line @eslint-react/set-state-in-effect -- min/max can change underneath the mounted sheet; keep the visible month in range- setMonthIndex(clamped);- scrollerHandleRef.current?.scrollToMonth(clamped, 'auto');- }- }, [monthIndex, minMonthIndex, maxMonthIndex]);-const dayNames = useMemo(() =>Array.from({length: 7}, (_, offset) =>