fix(fields): #3343 AddressField/GeolocationField 子输入改用 useId() 前缀 id,消除同表单重复 DOM id - #3348
Merged
Merged
Conversation
…消除同表单重复 DOM id AddressField(street/city/state/zipCode/country)与 GeolocationField (latitude/longitude)的子输入原用硬编码字面量 id:同一表单渲染两个同类型 字段即产生重复 DOM id,第二个字段的每个子 label 的 htmlFor 都解析到第一个 字段的输入。现严格照 RadioField / CheckboxesField 的 groupId 范式改为 useId() 前缀 + 子字段名,label htmlFor 逐一对齐;whitelist spread 与 id 的先后次序保持原状(行级 formItemId 落点不在本单重新设计)。 测试:两个新 *.uniqueIds.test.tsx 各渲染同表单两个实例,断言 (1) 全文档 [id] 全局唯一 (2) 第二个实例的每个子 label 经 for→getElementById 解析并 聚焦到自己的输入。破坏验证:回退硬编码 id 后 4 项断言全红。 aria-invalid 账本守卫与 dom-leak 守卫(281 项)保持全绿。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NVPjPzmmAJ2Ngtvgg5MSRa
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
✅ Console Performance Budget
📦 Bundle Size Report
Size Limits
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3343
问题
AddressField的五个子输入(street/city/state/zipCode/country)与GeolocationField的两个子输入(latitude/longitude)使用硬编码字面量 id。同一表单(或同一页面)渲染两个同类型字段即产生重复 DOM id,每个子Label的htmlFor都解析到文档中第一个匹配元素——第二个字段的子 label 点击/朗读全部落在第一个字段的输入上。修法(按 PM 裁决)
RadioField/CheckboxesField的groupId范式:useId()前缀 + 子字段名(`${groupId}-street`等),labelhtmlFor逐一对齐。formItemId的落点:whitelist spread(含id)与本组件id的先后次序保持原状(spread 在前、组件 id 在后,组件 id 仍覆盖注入值)。观察记录见下。aria-invalid={!!error}与toDomPropsspread 位置均未改动。验证
pnpm exec vitest run packages/fields/src/widgets/AddressField.uniqueIds.test.tsx packages/fields/src/widgets/GeolocationField.uniqueIds.test.tsxpnpm exec vitest run packages/fields/src/__tests__/widget-aria-invalid-registry-e2e.test.tsx packages/fields/src/__tests__/widget-dom-leak-e2e.test.tsxpnpm exec vitest run packages/fields --maxWorkers=2turbo run build --filter=@object-ui/fields(含tsc)破坏验证实录
将两个 widget 回退为硬编码 id 后重跑新测试——4 项断言全红,随后恢复修复:
测试断言走的正是浏览器 label 激活的机制本身:
for→getElementById→ 落在自己实例内 →focus()生效;回退即红,防止任何人改回硬编码字面量。观察记录(不在本单处理)
issue 正文第 2 点提到:这些组件级 id 会覆盖 form renderer 经
FormControlSlot 注入的行级formItemId,行级FormLabel的htmlFor因此指不到任何控件。这是复合 widget(多个子输入共享一个行级 id 注入)的普遍结构问题,PM 裁决明确不在本单重新设计落点;本 PR 保持 spread/id 次序原状,该问题维持现状,留待后续专项。变更
packages/fields/src/widgets/AddressField.tsx— 5 个子输入 id / htmlFor 改useId()前缀packages/fields/src/widgets/GeolocationField.tsx— 2 个子输入 id / htmlFor 改useId()前缀packages/fields/src/widgets/AddressField.uniqueIds.test.tsx— 新增packages/fields/src/widgets/GeolocationField.uniqueIds.test.tsx— 新增.changeset/address-geo-unique-ids.md—@object-ui/fieldspatch🤖 Generated with Claude Code
https://claude.ai/code/session_01NVPjPzmmAJ2Ngtvgg5MSRa
Generated by Claude Code