Skip to content

fix: field value updates may fail multiple times #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lich-yoo
Copy link

@lich-yoo lich-yoo commented Jun 12, 2025

fix #753

// Field input
props.onChange?.('');
props.onChange?.('A');
// Test
expect(form.getFieldValue('input')).toBe('A');

// Result
expect(received).toBe(expected)
Expected: "A"
Received: ""

Summary by CodeRabbit

  • 测试
    • 新增了针对表单字段多次快速变更值的测试用例,确保多次连续变更后最终值能正确反映在表单状态中。
    • 移除了未使用的测试文件导入内容。

Copy link

vercel bot commented Jun 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
field-form ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 12, 2025 3:07am

Copy link

coderabbitai bot commented Jun 12, 2025

Walkthrough

此次更改在 src/Field.tsx 中引入了新变量 curValue,用于在触发元事件后获取最新字段值,并用其替换原先的 value 变量进行归一化和比较。测试文件新增了一个用例,验证字段值在单次事件中多次变更时,最终值能正确反映。

Changes

文件/分组 变更摘要
src/Field.tsx 使用 curValue 替换原有 value,确保归一化和比较基于最新字段值。
tests/field.test.tsx 新增“value change multiple times”用例,移除未使用的 getInput 导入。

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant MockBtnInput
  participant FormField

  User->>MockBtnInput: 点击按钮
  MockBtnInput->>FormField: onChange("")
  MockBtnInput->>FormField: onChange("A")
  FormField->>FormField: 标记 touched/dirty,触发 meta 事件
  FormField->>FormField: 获取最新 curValue
  FormField->>FormField: 归一化、比较并更新值
Loading

Assessment against linked issues

Objective Addressed Explanation
多次调用 onChange 更新字段值时,确保最终值能正确反映(#753

Possibly related PRs

  • fix: change #731:两者均修改了 src/Field.tsx 的值更新逻辑,确保字段值变更能被正确、高效处理。
  • fix: change not trigger on Date #734:两者均关注值获取和比较时机,主 PR 引入 curValue,相关 PR 修改了值的比较方式,均与字段值更新触发有关。

Suggested reviewers

  • zombieJ

Poem

兔子点点头,字段值更新溜,
多次变化不迷路,curValue 保驾护,
测试用例来守护,
代码清晰又靠谱,
🐇代码田野里,bug 不再住!

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

tests/field.test.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

src/Field.tsx

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.

The config "prettier" was referenced from the config file in "/.eslintrc.js".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch fix/field-change
  • Post Copyable Unit Tests in Comment

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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: 0

🧹 Nitpick comments (2)
src/Field.tsx (1)

608-622: 引入 curValue 合理,但可再压缩一次 getValue 调用

当前实现先在 getControlled 开头计算了 const value = this.getValue();,随后在触发器内再次执行 this.getValue() 得到 curValue
虽然两次调用都很轻量,但在高频输入场景仍是一次不必要的遍历(getFieldsValue(true) 会克隆整棵 store)。可以直接删除最前面的 value 变量,改为在外层利用 curValue,从而避免一次冗余读取,逻辑上也更统一。

- const value = this.getValue();
+ // 延后到真正渲染子节点时再取值,避免后续再次读取

其余修改(归一化及比较均改用最新值)完全解决了 #753 的时序问题,👍。

tests/field.test.tsx (1)

76-108: 测试覆盖到位,但等待方式仍有潜在隐患

await act(async () => timeout()) 依赖固定 10 ms 定时器,若后续实现改为批量更新或启用 flushSync,该时长可能不足/过多导致偶发失败。
建议改用 Testing Library 提供的 waitForfindBy 系列 API 监听实际 DOM 变动,而不是硬编码时间。

- await act(async () => {
-   await timeout();
- });
+ await waitFor(() =>
+   expect(form.current?.getFieldValue('input')).toBe('A'),
+ );

这样既能保证可靠性,也能让测试速度随实现自动收敛。
代码其余部分无问题,成功重现并验证修复效果。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6611c31 and cb183e6.

📒 Files selected for processing (2)
  • src/Field.tsx (2 hunks)
  • tests/field.test.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/field.test.tsx (3)
src/interface.ts (1)
  • FormInstance (254-276)
tests/common/InfoField.tsx (1)
  • Input (9-11)
tests/common/timeout.ts (1)
  • timeout (3-7)

@lich-yoo
Copy link
Author

@zombieJ

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.

Field onChange 概率性更新不成功
1 participant