An open source design system that's fully customizable and agent ready
TypeScript
fix(dateParser): in day-first locales, numeric text never takes the native month-first reading (#9726)
Cindy Zhanga3d7b46c74e9
diff --git a/.changeset/date-typing-no-month-first.md b/.changeset/date-typing-no-month-first.mdnew file mode 100644index 0000000000..b0e4c991ce--- /dev/null+++ b/.changeset/date-typing-no-month-first.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Typing a numeric date into `DateInput` or `DateTimeInput` no longer reports month-first dates on the way in day-first locales.++@duytbpdiff --git a/apps/storybook/stories/DateInput.stories.tsx b/apps/storybook/stories/DateInput.stories.tsxindex bb4bf75d56..bc793a78f1 100644--- a/apps/storybook/stories/DateInput.stories.tsx+++ b/apps/storybook/stories/DateInput.stories.tsx@@ -6,6 +6,7 @@ import {expect, userEvent, waitFor, within} from 'storybook/test';import {DateInput} from '@astryxdesign/core/DateInput';import {FormLayout} from '@astryxdesign/core/FormLayout';import {Text} from '@astryxdesign/core/Text';+import {InternationalizationProvider} from '@astryxdesign/core/i18n';import {InputGroup, InputGroupText} from '@astryxdesign/core/InputGroup';import type {ISODateString} from '@astryxdesign/core/Calendar';import {Layout, LayoutContent} from '@astryxdesign/core/Layout';@@ -158,6 +159,34 @@ export const WithValue: Story = {},};+/**+ * A day-first locale: typed numeric dates read day, then month. The line+ * under the field shows each value the field reports.+ */+export const DayFirstLocale: Story = {+ tags: ['!autodocs'],+ render: args => {+ const [value, setValue] = useState<ISODateString | undefined>(undefined);+ const [reported, setReported] = useState<string[]>([]);+ return (+ <InternationalizationProvider locale="en-GB">+ <DateInput+ {...args}+ value={value}+ onChange={next => {+ setValue(next);+ setReported(previous => [...previous, String(next)]);+ }}+ />+ <Text data-testid="reported">{reported.join(', ')}</Text>+ </InternationalizationProvider>+ );+ },+ args: {+ label: 'Start date',+ },+};+export const MondayFirstWeek: Story = {name: 'Week starts on Monday',render: args => {diff --git a/docs/specs/components/DateInput/DateInput.spec.md b/docs/specs/components/DateInput/DateInput.spec.mdindex 3efde32bd6..378bc3436e 100644--- a/docs/specs/components/DateInput/DateInput.spec.md+++ b/docs/specs/components/DateInput/DateInput.spec.md@@ -133,7 +133,7 @@ Consumer migration instructions belong in consumer docs and release notes.| FR3 | Explicit `presentation` MUST outrank deprecated `nativePicker`; the legacy values MUST retain their mappings and development-only warning from `spec:AST-043/FR3–FR4`. | Current source, docs, and Presentation tests || FR4 | Adaptive resolution MUST use the primary pointer rather than viewport width or any-pointer availability, so tablets receive a touch surface and touchscreen laptops retain the typable field. | Current source, consumer docs, and surface-selection tests || FR5 | Text, Calendar, bottom-sheet, and native commit paths MUST enforce `min`, `max`, and `dateConstraints`; a rejected value MUST NOT replace the controlled value. | Current source and all four focused suites |-| FR6 | Named committed formats MUST follow the InternationalizationProvider locale and shared Timestamp vocabulary; formatting MUST NOT rewrite in-progress typed text. Typed text MUST parse in the provider locale (`architecture:internationalization` INV1a, INV8). The touch sheet's month heading and each month pane's name are stand-alone month labels: they start with a capital where CLDR's stand-alone context transform titlecases the month, as `component:Calendar` FR1 states for Calendar's. Implementation gap: the typed parser accepts only English month names and ASCII digits, using the locale only to order day and month. | `architecture:internationalization`; current source, docs, locale tests; `spec:AST-043/FR5` |+| FR6 | Named committed formats MUST follow the InternationalizationProvider locale and shared Timestamp vocabulary; formatting MUST NOT rewrite in-progress typed text. Typed text MUST parse in the provider locale (`architecture:internationalization` INV1a, INV8); in a day-first locale, numeric text outside the locale-ordered forms is not a date, so no fixed month-first reading applies. The touch sheet's month heading and each month pane's name are stand-alone month labels: they start with a capital where CLDR's stand-alone context transform titlecases the month, as `component:Calendar` FR1 states for Calendar's. Implementation gap: the typed parser accepts only English month names and ASCII digits, using the locale only to order day and month. In a month-first locale, numeric text outside the locale-ordered forms (such as `1/5/25`) still falls back to the browser's month-first reading, and a four-digit year written first (`2026/01/12`) parses in every locale. | `architecture:internationalization`; current source, docs, locale tests; `spec:AST-043/FR5` || FR7 | Every value-change path MUST call `onChange` before `changeAction`, present the proposed value optimistically, and expose one busy state while the Action is pending. | `family:input-fields/FR6`; current source and focused tests || FR8 | The pointer field MUST open its Calendar from the input, calendar toggle, ArrowDown, or Alt+ArrowDown; Popover dismissal and focus return MUST remain owned by the shared Popover path. The open pointer Calendar stays open for a press on the field's input, clear button or label, or in the Calendar, and closes for a press elsewhere (`architecture:layer-runtime`) or for focus moving from anywhere in the field (its input, clear button or calendar toggle) to anything outside the field and the Calendar (`family:overlay-dismissal` AV4). | Current source, Popover composition, keyboard/focus tests || FR9 | A pointer-surface Calendar selection MUST commit and close its Popover; a bottom-sheet Calendar selection MUST remain editable in the open sheet until Save closes the surface. | Current source and pointer/touch tests |@@ -309,7 +309,7 @@ clear-glyph target of its own.| FR1, ORD2 | `DateInput.test.tsx` "displays formatted date when value is provided", "calls onChange on blur when input is valid", "calls onChange with undefined when input is cleared and blurred", "clears pending input when value changes externally"; `NativeDateField.test.tsx` "keeps the control's own value ISO", "fires onChange with the ISO date the control reports" | Typed, cleared, external value; native | A committed value kept beside `value`, a non-ISO change, or pending text that survives an external change fails. | `audit:DateInput/behavior` || FR2, FR3, FR4, ORD1 | `Presentation.test.tsx` cases; `DateInputTouch.test.tsx` surface-selection cases; `NativeDateField.test.tsx` nativePicker cases | Each presentation on fine and coarse pointers; deprecated mappings; tablet and touchscreen laptop | A wrong surface, a lost deprecated mapping, `nativePicker` winning, or width-based resolution fails. Evidence gap: no test asserts the development warning. | `audit:DateInput/behavior` || FR5 | `DateInput.test.tsx` min, max, and dateConstraints typed cases; `DateInputTouch.test.tsx` "disables days outside min/max and refuses to commit them", "honors custom date constraints"; `NativeDateField.test.tsx` "refuses an out-of-range date and announces it", "refuses a date dateConstraints rejects" | Typed, sheet, native; bounds and predicates | A constrained date that commits on any surface fails. Evidence gap: no test asserts constraints on a popover Calendar pick. | `audit:DateInput/behavior` |-| FR6 | `DateInput.test.tsx` format cases, including "updates when the InternationalizationProvider locale changes (#5074)" and "does not apply format to in-progress typed input"; `NativeDateField.test.tsx` format and locale cases; `DateInputTouch.test.tsx` "updates the field and open picker when the provider locale changes" | Named and function formats; locale change | A format that ignores the locale or rewrites typed text fails. Evidence gap: no test types a localized month name or non-ASCII digits (the FR6 gap). | `audit:DateInput/i18n` |+| FR6 | `DateInput.test.tsx` format cases, including "updates when the InternationalizationProvider locale changes (#5074)" and "does not apply format to in-progress typed input" and "typing a day-first date in en-GB never reports a month-first date"; `dateParser.test.ts` "in a day-first locale, never reads numeric text month-first outside the locale-ordered forms"; `NativeDateField.test.tsx` format and locale cases; `DateInputTouch.test.tsx` "updates the field and open picker when the provider locale changes" | Named and function formats; locale change | A format that ignores the locale or rewrites typed text fails. Evidence gap: no test types a localized month name or non-ASCII digits (the FR6 gap). | `audit:DateInput/i18n` || FR7, ORD3 | `DateInput.test.tsx` "runs changeAction after onChange for a typed date"; `NativeDateField.test.tsx` "runs changeAction after onChange and exposes the pending state"; `DateInputTouch.test.tsx` "runs changeAction and shows a busy state"; `BusyOwnerAnnouncement.test.tsx` DateInput cases; `DateInputTouch.test.tsx` "announces a busy start from a pick inside the open sheet, not behind it (spec:AST-086 FR8, Platform support)" | Typed, native, sheet | An Action before `onChange`, a missing busy state, or a missing busy announcement fails. | `audit:DateInput/behavior` || FR8, AR2 | `DateInput.test.tsx` "opens the calendar popover on ArrowDown (keyboard, forms-13)", "opens the calendar popover on Alt+ArrowDown (keyboard, forms-13)", "input has aria-expanded attribute", "combobox input has aria-haspopup=\"dialog\" attribute", "moves focus into the calendar on each open from the toggle"; `DateInputTouch.test.tsx` "opens from the keyboard, APG combobox style"; `DateInput.test.tsx` "press inside and outside the open calendar" cases | Pointer and touch fields | A missing open path, combobox state, or focus move fails. Evidence gap: no test asserts Escape outside IME composition on the popover. | `audit:DateInput/accessibility` || FR9 | `DateInputTouch.test.tsx` "commits the tapped day and LEAVES the sheet open", "lets a second tap correct the first, still without closing", "Save closes the sheet without touching the value" | Sheet picks and Save | A sheet that closes on a pick or a Save that changes the value fails. Evidence gap: no test asserts that a popover Calendar pick closes the popover. | `audit:DateInput/behavior` |diff --git a/packages/core/src/DateInput/DateInput.test.tsx b/packages/core/src/DateInput/DateInput.test.tsxindex 2cb47c8063..b9a6859daf 100644--- a/packages/core/src/DateInput/DateInput.test.tsx+++ b/packages/core/src/DateInput/DateInput.test.tsx@@ -421,6 +421,23 @@ describe('DateInput', () => {expect(input).toHaveAttribute('aria-expanded', 'true');});+ it('typing a day-first date in en-GB never reports a month-first date', async () => {+ const user = userEvent.setup();+ const onChange = vi.fn();+ render(+ <InternationalizationProvider locale="en-GB">+ <DateInput label="Start date" onChange={onChange} />+ </InternationalizationProvider>,+ );+ await user.type(screen.getByRole('combobox'), '12/01/2026');+ const reported = onChange.mock.calls.map(call => String(call[0]));+ expect(reported.at(-1)).toBe('2026-01-12');+ // Every value on the way reads 12 January; none reads 1 December.+ for (const value of reported) {+ expect(value).toMatch(/-01-12$/);+ }+ });+it('Enter on a busy field does not submit the form', async () => {const user = userEvent.setup();const onSubmit = vi.fn((e: React.FormEvent) => e.preventDefault());diff --git a/packages/core/src/utils/dateParser.test.ts b/packages/core/src/utils/dateParser.test.tsindex 22040462fa..efa80b92dd 100644--- a/packages/core/src/utils/dateParser.test.ts+++ b/packages/core/src/utils/dateParser.test.ts@@ -373,6 +373,50 @@ describe('parseDateInput', () => {expect(parseDateInput('1/25.2026')).toBeNull();});+ it('in a day-first locale, never reads numeric text month-first outside the locale-ordered forms', () => {+ // Native Date reads these month-first whatever the locale.+ for (const text of [+ '12/0',+ '12/01/',+ '12/01/2',+ '12/01/20',+ '12/01/202',+ '12/01/90',+ '12,01',+ '12,01,2026',+ '12;01;2026',+ ]) {+ expect(parseDateInput(text, 'en-GB')).toBeNull();+ }+ // A month-first locale keeps the browser's reading of a short year.+ expect(parseDateInput('1/5/25', 'en-US')).toEqual({+ year: 2025,+ month: 1,+ day: 5,+ });+ // A four-digit year written first parses in every locale, with any+ // separator the browser reads.+ for (const locale of ['en-GB', 'en-US']) {+ for (const text of ['2026/01/12', '2026 01 12', '2026.1.12.']) {+ expect(parseDateInput(text, locale)).toEqual({+ year: 2026,+ month: 1,+ day: 12,+ });+ }+ }+ // The locale-ordered forms still parse in the locale's order.+ expect(parseDateInput('12/01/2026', 'en-GB')).toEqual({+ year: 2026,+ month: 1,+ day: 12,+ });+ expect(parseDateInput('12/01', 'en-GB')).toMatchObject({+ month: 1,+ day: 12,+ });+ });+it('treats a single typed digit as incomplete, not a date', () => {// A user starting to type a month (e.g. "0" or "1" for January) should// not produce a date. Native Date parsing would otherwise coerce thesediff --git a/packages/core/src/utils/dateParser.ts b/packages/core/src/utils/dateParser.tsindex fd82ec67a9..665d79b49b 100644--- a/packages/core/src/utils/dateParser.ts+++ b/packages/core/src/utils/dateParser.ts@@ -32,6 +32,11 @@ export function isLocaleDayFirst(locale: Locale = 'en'): boolean {return dayIndex < monthIndex;}+// Built at run time: a Unicode property escape in a literal fails a source+// build whose Babel lacks the Unicode data.+const LETTER_FREE = new RegExp('^[^\\p{L}]+$', 'u');+const YEAR_FIRST = new RegExp('^\\d{4}[^\\p{L}\\d]', 'u');+/*** Parses user input into a PlainDate.*@@ -138,6 +143,17 @@ export function parseDateInput(if (/^\d+$/.test(trimmed)) {return null;}+ // In a day-first locale, letter-free text that no form above matched is not+ // a date. Native `Date` reads `a/b/c` month first. It also takes a year of+ // one to three digits. So "12/01/2" gives a wrong date. A four-digit year+ // written first is not ambiguous.+ if (+ isLocaleDayFirst(locale) &&+ LETTER_FREE.test(trimmed) &&+ !YEAR_FIRST.test(trimmed)+ ) {+ return null;+ }const parsed = new Date(trimmed);if (!isNaN(parsed.getTime())) {