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

未关闭 适合新手
#10,833 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@mangeshraut712 已经在做这个了。

开始于 2026年10月23日。

  • #2 来自 @mangeshraut712 —— 已关闭,未合并
  • #10842 来自 @mangeshraut712 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript

调研方向

从 packages/react-aria/src/datepicker/useDateSegment.ts 中 issue 所述的 amPmFormatter 和 dayPeriod 查找处开始。检查 AM/PM 按键检查如何处理 day period 缺失的情况,以及它们是否会在仅包含日期的 segment 上运行。使用 locale fr-CM 复现;当 formatter 省略 dayPeriod 时,DateField、DatePicker 和 TimeField 均能正常渲染且不崩溃,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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
主要语言
TypeScript
星标
16k
派生
1.6k
平均合并
3 天 19 小时
30 天内合并 PR
46

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

adobe/react-spectrum 的其他 Issue

查看 adobe/react-spectrum 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。