useDateSegment crashes for locales whose 12-hour format has no dayPeriod part (e.g. fr-CM)

Aperta Adatta ai principianti
#10,833 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@mangeshraut712 ci sta già lavorando.

Dal 23/10/2026.

  • #2 di @mangeshraut712 — chiusa senza merge
  • #10842 di @mangeshraut712 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react, typescript

Direzione di ricerca

Inizia in packages/react-aria/src/datepicker/useDateSegment.ts, nei lookup di amPmFormatter e dayPeriod descritti nell’issue. Verifica come i controlli dei tasti AM/PM gestiscono l’assenza di un periodo del giorno e se vengono eseguiti per i segmenti che contengono solo una data. Riproduci il problema con la locale fr-CM; il lavoro è concluso quando DateField, DatePicker e TimeField vengono renderizzati senza arresti anomali se il formatter omette dayPeriod.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Provide a general summary of the issue here

DatePicker, DateField and TimeField throw during render when the locale is fr-CM. useDateSegment assumes a 12-hour formatter always outputs a dayPeriod part, but for some locales it doesn't.

🤔 Expected Behavior?

The field renders normally. A locale with no AM/PM marker should at worst make AM/PM typing unavailable, not crash, and a date-only field shouldn't need AM/PM at all.

😯 Current Behavior
TypeError: Cannot read properties of undefined (reading 'value')
    at useDateSegment

packages/react-aria/src/datepicker/useDateSegment.ts (lines 151 and 157 on main):

let amPmFormatter = useDateFormatter({hour: 'numeric', hour12: true});
let am = useMemo(() => {
  let date = new Date();
  date.setHours(0);
  return amPmFormatter.formatToParts(date).find(part => part.type === 'dayPeriod')!.value;
}, [amPmFormatter]);

For fr-CM, ICU returns no dayPeriod part (Node 20, ICU 76):

new Intl.DateTimeFormat('fr-CM', {hour: 'numeric', hour12: true}).formatToParts(new Date(2000, 0, 1, 0))
// [{type: 'hour', value: '0'}]

new Intl.DateTimeFormat('fr', {hour: 'numeric', hour12: true}).formatToParts(new Date(2000, 0, 1, 0))
// [{type: 'hour', value: '0'}, {type: 'literal', value: ' '}, {type: 'dayPeriod', value: 'AM'}]

These memos run for every segment, so a date-only field with no dayPeriod segment crashes too.

💁 Possible Solution

Guard the lookup:

return amPmFormatter.formatToParts(date).find(part => part.type === 'dayPeriod')?.value ?? '';

The startsWith(am, key) / startsWith(pm, key) checks would also need to skip an empty string. Otherwise every key would match, since every string starts with ''. Computing am/pm only when segment.type === 'dayPeriod' would also avoid the work for every other segment.

🔦 Context

Visitors whose browser locale is fr-CM couldn't open a birthdate picker; the field crashed on render. Because the default locale comes from navigator.language, apps hit this without configuring anything. We're working around it by wrapping DateInput in an I18nProvider that falls back from fr-CM to fr when the locale has no day period.

🖥️ Steps to Reproduce
import {I18nProvider} from 'react-aria';
import {DateField, DateInput, DateSegment, Label} from 'react-aria-components';

<I18nProvider locale="fr-CM">
  <DateField>
    <Label>Birthdate</Label>
    <DateInput>{segment => <DateSegment segment={segment} />}</DateInput>
  </DateField>
</I18nProvider>

Or set the browser language to fr-CM and render any DateField without an I18nProvider.

Version

react-aria 3.51.0 / react-aria-components 1.20.0; still present on main.

What browsers are you seeing the problem on?

All

What operating system are you using?

MacOS 26

🧢 Your Company/Team

Planning Center

🕷 Tracking Issue
Lingua principale
TypeScript
Stelle
16k
Fork
1.6k
Merge medio
3g 9h
PR unite (30g)
45

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di adobe/react-spectrum

Tutte le issue di adobe/react-spectrum

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.