Skip to content
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

feat(form): resetFields 增加 namepath 参数,用于重置指定的字段 #2953

Merged
merged 4 commits into from
Jan 22, 2025

Conversation

oasis-cloud
Copy link
Collaborator

@oasis-cloud oasis-cloud commented Jan 20, 2025

🤔 这个变动的性质是?

  • 新特性提交

🔗 相关 Issue

💡 需求背景和解决方案

增加 namepath 参数用于重置指定字段的内容

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 新功能

    • 为表单组件引入了字段重置功能,允许用户精确重置特定输入字段
    • 新增测试用例验证字段重置机制
  • 测试

    • 添加了针对表单字段重置的新测试场景
    • 测试验证了重置功能能够清除字段验证消息并恢复初始状态
  • 改进

    • 优化了 resetFields 方法,提供了更灵活的字段重置选项
    • 改进了错误状态管理机制

Copy link

coderabbitai bot commented Jan 20, 2025

Warning

Rate limit exceeded

@oasis-cloud has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 17 minutes and 59 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between c547da8 and b54f38b.

📒 Files selected for processing (7)
  • src/packages/form/__tests__/form.spec.tsx (1 hunks)
  • src/packages/form/doc.en-US.md (1 hunks)
  • src/packages/form/doc.md (1 hunks)
  • src/packages/form/doc.taro.md (1 hunks)
  • src/packages/form/doc.zh-TW.md (1 hunks)
  • src/packages/form/useform.taro.ts (2 hunks)
  • src/packages/form/useform.ts (2 hunks)

变更概览

演练

此次变更主要涉及表单组件的重置字段功能增强。在 useform.tsuseform.taro.ts 中,resetFields 方法被更新为支持可选的字段路径参数,允许精确重置特定字段。同时,在测试文件 form.spec.tsx 中新增了一个测试用例,验证了这一重置功能的正确性。

变更

文件路径 变更概要
src/packages/form/__tests__/form.spec.tsx 新增 reset username field 测试用例,验证表单字段重置功能
src/packages/form/useform.taro.ts 更新 resetFields 方法,支持按字段路径重置
src/packages/form/useform.ts 更新 resetFields 方法,支持按字段路径重置

可能相关的 PR

建议标签

size/S, 3.x

建议审阅者

  • xiaoyatong
  • irisSong

诗歌

🐰 重置表单,如清风拂面
字段飞舞,错误烟消
精准控制,灵活如意
代码如诗,兔子欢笑


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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

codecov bot commented Jan 20, 2025

Codecov Report

Attention: Patch coverage is 95.65217% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.20%. Comparing base (36220ef) to head (b54f38b).
Report is 1 commits behind head on next.

Files with missing lines Patch % Lines
src/packages/form/useform.ts 95.65% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             next    #2953   +/-   ##
=======================================
  Coverage   84.19%   84.20%           
=======================================
  Files         267      267           
  Lines       18099    18114   +15     
  Branches     2661     2667    +6     
=======================================
+ Hits        15238    15252   +14     
- Misses       2856     2857    +1     
  Partials        5        5           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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 (3)
src/packages/form/useform.ts (1)

203-225: 优化重置字段的实现

当前实现存在以下需要注意的点:

  1. 性能优化:对于选择性重置,可以先过滤出相关的 fieldEntities 再进行遍历
  2. 测试覆盖:需要补充对 path in this.initialValues 分支的测试用例

建议重构如下:

 resetFields = (namePaths?: NamePath[]) => {
   if (namePaths) {
     namePaths.forEach((path) => {
       this.errors[path] = null
-      this.fieldEntities.forEach((entity: FormFieldEntity) => {
-        const name = entity.props.name
-        if (name === path) {
+      const targetEntities = this.fieldEntities.filter(
+        (entity) => entity.props.name === path
+      )
+      targetEntities.forEach((entity) => {
           if (path in this.initialValues) {
             this.updateStore({ [path]: this.initialValues[path] })
           } else {
             delete this.store[path]
           }
           entity.onStoreChange('reset')
-        }
       })
     })
   } else {
     const nextStore = merge({}, this.initialValues)
     this.updateStore(nextStore)
     this.fieldEntities.forEach((entity: FormFieldEntity) => {
       entity.onStoreChange('reset')
     })
   }
 }
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 211-211: src/packages/form/useform.ts#L211
Added line #L211 was not covered by tests

src/packages/form/__tests__/form.spec.tsx (2)

324-335: 优化 Fragment 的使用

footer 属性中的 Fragment 是不必要的,可以直接返回 div 元素。

 footer={
-  <>
   <div
     id="reset"
     onClick={() => {
       form.resetFields(['username'])
     }}
   >
     Reset
   </div>
-  </>
 }

316-367: 补充更多测试场景

当前测试用例仅覆盖了基本的重置功能,建议添加以下测试场景:

  1. 重置不存在于 initialValues 中的字段
  2. 同时重置多个字段
  3. 重置所有字段(不传参数)

需要我帮你生成这些测试用例吗?

🧰 Tools
🪛 Biome (1.9.4)

[error] 328-339: Avoid using unnecessary Fragment.

A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment.
Unsafe fix: Remove the Fragment

(lint/complexity/noUselessFragments)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 16fb6e5 and c547da8.

📒 Files selected for processing (3)
  • src/packages/form/__tests__/form.spec.tsx (1 hunks)
  • src/packages/form/useform.taro.ts (2 hunks)
  • src/packages/form/useform.ts (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/packages/form/__tests__/form.spec.tsx

[error] 328-339: Avoid using unnecessary Fragment.

A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment.
Unsafe fix: Remove the Fragment

(lint/complexity/noUselessFragments)

🪛 GitHub Check: codecov/patch
src/packages/form/useform.ts

[warning] 211-211: src/packages/form/useform.ts#L211
Added line #L211 was not covered by tests

🔇 Additional comments (2)
src/packages/form/useform.ts (1)

177-177: 验证错误处理行为的变更

注释掉 this.errors.length = 0 是为了支持选择性重置字段功能。请确认在以下场景中错误状态的处理是否符合预期:

  1. 重置单个字段时的错误状态
  2. 重置所有字段时的错误状态
✅ Verification successful

错误处理行为变更已验证正确

代码中移除 this.errors.length = 0 是合理的,现在的实现通过 this.errors[path] = null 精确清除指定字段的错误状态,这与选择性重置字段的功能设计相符。

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查是否有其他地方清除了所有错误
rg -A 3 "this\.errors.*=.*\[\]" 

Length of output: 380


Script:

#!/bin/bash
# 检查resetFields的实现
ast-grep --pattern 'resetFields($$$) {
  $$$
}'

Length of output: 49


Script:

#!/bin/bash
# 搜索resetFields方法的实现
rg -A 10 "resetFields.*\(" src/packages/form/

Length of output: 6647

src/packages/form/useform.taro.ts (1)

Line range hint 177-225: 代码变更与 useform.ts 一致

此文件的变更与 useform.ts 完全相同,相关的优化建议也同样适用。

Copy link
Collaborator

@Alex-huxiyang Alex-huxiyang left a comment

Choose a reason for hiding this comment

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

可以 resolve 下 conflicts

@Alex-huxiyang Alex-huxiyang removed the 3.x label Jan 22, 2025
Copy link
Collaborator

@xiaoyatong xiaoyatong left a comment

Choose a reason for hiding this comment

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

fix一下 单测冲突~

entity.onStoreChange('reset')
})
resetFields = (namePaths?: NamePath[]) => {
if (namePaths) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

需要判断 namePaths 长度不为0,为0 时,应该全部重置。

@xiaoyatong
Copy link
Collaborator

补一下文档。

@xiaoyatong xiaoyatong merged commit 399ceb2 into jdf2e:next Jan 22, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants