Skip to content

feat: add getFieldsName API to form instance#787

Open
QdabuliuQ wants to merge 1 commit intoreact-component:masterfrom
QdabuliuQ:master
Open

feat: add getFieldsName API to form instance#787
QdabuliuQ wants to merge 1 commit intoreact-component:masterfrom
QdabuliuQ:master

Conversation

@QdabuliuQ
Copy link
Copy Markdown

@QdabuliuQ QdabuliuQ commented May 4, 2026

🤔 这个变动的性质是?

  • 🆕 新特性提交
  • 🐞 Bug 修复
  • 📝 站点、文档改进
  • 📽️ 演示代码改进
  • 💄 组件样式/交互改进
  • 🤖 TypeScript 定义更新
  • 📦 包体积优化
  • ⚡️ 性能优化
  • ⭐️ 功能增强
  • 🌐 国际化改进
  • 🛠 重构
  • 🎨 代码风格优化
  • ✅ 测试用例
  • 🔀 分支合并
  • ⏩ 工作流程
  • ⌨️ 无障碍改进
  • ❓ 其他改动(是关于什么的改动?)

🔗 相关 Issue

issues: 57757

💡 需求背景和解决方案

新增 getFieldsName api 获取 Form.Item的name属性值

📝 更新日志

语言 更新描述
🇺🇸 英文 Form Add getFieldsName method to get all field names
🇨🇳 中文 Form 新增 getFieldsName 方法,用于获取表单所有字段名

Summary by CodeRabbit

发布说明

  • 新功能

    • 新增 getFieldsName() 方法,支持获取表单中所有已注册字段的名称路径,包括嵌套字段和列表字段。
  • 文档

    • 添加 getFieldsName 功能文档和实际使用示例代码。
  • 测试

    • 新增全面的测试套件,覆盖多个使用场景。

@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

@QdabuliuQ is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

Walkthrough

本次 PR 新增了 getFieldsName() 方法到 FormInstance,用于获取所有已注册字段的名称路径列表。实现包括接口定义、FormStore 核心实现、FieldContext 默认值更新,以及完整的文档示例和单元测试覆盖。

Changes

getFieldsName 方法新增

Layer / File(s) Summary
接口定义
src/interface.ts
FormInstance<Values> 新增方法签名 getFieldsName(): NamePath<Values>[]
核心实现
src/hooks/useForm.ts
FormStore 实现 getFieldsName() 方法,遍历所有已注册字段实体并返回其名称路径数组;在 FormStore.getForm() 返回的内部表单实例中暴露该方法。
上下文默认值
src/FieldContext.ts
React Context 默认值添加 getFieldsName 方法,指向警告函数以处理 Field 未被 Form 包裹的情况。
文档与示例
docs/demo/getFieldsName.md, docs/examples/getFieldsName.tsx
新增文档页面和 React/TypeScript 示例组件,展示 getFieldsName() 的用法,包含普通字段和嵌套路径字段。
单元测试
tests/getFieldsName.test.tsx
完整测试套件覆盖空字段数组、字符串与数组名称路径、排除无名字段、重复字段、字段卸载动态更新、Form.List 嵌套列表等场景。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • zombieJ

Poem

🐰 一个方法轻轻来,
字段名称一览开,
表单清单显真容,
从此查询不再难,
代码如诗润心间! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地总结了主要变更:向 form 实例添加 getFieldsName API,这正是本次 PR 的核心功能。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the getFieldsName method to the FormInstance, allowing users to retrieve the name paths of all registered fields. The implementation includes updates to the FormStore, FieldContext, and TypeScript interfaces, along with new documentation and a comprehensive test suite. A review comment suggests considering whether the method should deduplicate name paths when multiple fields share the same name, as the current implementation returns an entry for every registered field entity.

Comment thread src/hooks/useForm.ts
Comment on lines +349 to +352
private getFieldsName = (): InternalNamePath[] => {
this.warningUnhooked();
return this.getFieldEntities(true).map(entity => entity.getNamePath());
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of getFieldsName returns all registered field entities, which may include duplicate name paths if multiple Field components share the same name. While this is consistent with getFieldsError, it might be more useful for consumers of getFieldsName to receive unique name paths, especially if they intend to use these paths with getFieldsValue. Consider if deduplication is desired here.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/getFieldsName.test.tsx (1)

77-97: ⚡ Quick win

Form.List 测试描述遗漏了列表容器字段本身

测试标题 'includes Form.List item fields' 暗示只验证子项路径(['list', 0]['list', 1]),但断言实际上还包含了 Form.List 内部渲染的 Fieldname={[]} + prefixName=['list'])所注册的容器路径 ['list']

建议更新描述以反映完整行为,便于后续维护者理解 getFieldsName() 会同时返回列表容器字段:

✏️ 建议的描述改法
-  it('includes Form.List item fields', () => {
+  it('includes both Form.List container field and its item fields', () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/getFieldsName.test.tsx` around lines 77 - 97, Update the test
description to accurately reflect that getFieldsName() returns both the
Form.List item paths and the List container path: change the it() title from
'includes Form.List item fields' to something like 'includes Form.List item
fields and the List container field' so the test intent matches the assertion
involving Form.List, List, and getFieldsName().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/getFieldsName.test.tsx`:
- Around line 42-55: Update the ambiguous test title in the spec that currently
reads "includes one entry per Field with the same name" to explicitly state the
non-deduplication behavior so readers won't misinterpret it as "deduplicates by
name"; locate the test case using the it(...) block that references
formRef.current!.getFieldsName() in tests/getFieldsName.test.tsx and replace the
description with something clearer like "returns one entry per Field instance
even when names are identical" or "does not deduplicate fields with the same
name" to accurately reflect the behavior.

---

Nitpick comments:
In `@tests/getFieldsName.test.tsx`:
- Around line 77-97: Update the test description to accurately reflect that
getFieldsName() returns both the Form.List item paths and the List container
path: change the it() title from 'includes Form.List item fields' to something
like 'includes Form.List item fields and the List container field' so the test
intent matches the assertion involving Form.List, List, and getFieldsName().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c129126-f098-40b4-9f81-d242557e7d00

📥 Commits

Reviewing files that changed from the base of the PR and between f08c1cc and 96bf86d.

📒 Files selected for processing (6)
  • docs/demo/getFieldsName.md
  • docs/examples/getFieldsName.tsx
  • src/FieldContext.ts
  • src/hooks/useForm.ts
  • src/interface.ts
  • tests/getFieldsName.test.tsx

Comment on lines +42 to +55
it('includes one entry per Field with the same name', () => {
const formRef = React.createRef<FormInstance>();
render(
<Form ref={formRef}>
<Field name="x">
<Input />
</Field>
<Field name="x">
<Input />
</Field>
</Form>,
);
expect(formRef.current!.getFieldsName()).toEqual([['x'], ['x']]);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

测试描述存在歧义,建议澄清

'includes one entry per Field with the same name' 可被误读为"同名字段只返回一条"(即去重),而实际行为恰好相反——不做去重,每个注册的 Field 实例均贡献一条记录。建议将描述改为明确表达非去重语义,例如:

✏️ 建议的描述改法
-  it('includes one entry per Field with the same name', () => {
+  it('does not deduplicate Fields sharing the same name', () => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('includes one entry per Field with the same name', () => {
const formRef = React.createRef<FormInstance>();
render(
<Form ref={formRef}>
<Field name="x">
<Input />
</Field>
<Field name="x">
<Input />
</Field>
</Form>,
);
expect(formRef.current!.getFieldsName()).toEqual([['x'], ['x']]);
});
it('does not deduplicate Fields sharing the same name', () => {
const formRef = React.createRef<FormInstance>();
render(
<Form ref={formRef}>
<Field name="x">
<Input />
</Field>
<Field name="x">
<Input />
</Field>
</Form>,
);
expect(formRef.current!.getFieldsName()).toEqual([['x'], ['x']]);
});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/getFieldsName.test.tsx` around lines 42 - 55, Update the ambiguous test
title in the spec that currently reads "includes one entry per Field with the
same name" to explicitly state the non-deduplication behavior so readers won't
misinterpret it as "deduplicates by name"; locate the test case using the
it(...) block that references formRef.current!.getFieldsName() in
tests/getFieldsName.test.tsx and replace the description with something clearer
like "returns one entry per Field instance even when names are identical" or
"does not deduplicate fields with the same name" to accurately reflect the
behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant