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): 同步 2.x 的功能 #2976

Open
wants to merge 2 commits into
base: feat_v3.x
Choose a base branch
from

Conversation

oasis-cloud
Copy link
Collaborator

@oasis-cloud oasis-cloud commented Feb 10, 2025

🤔 这个变动的性质是?

  • 新特性提交

Summary by CodeRabbit

  • 新功能

    • 添加了新表单演示示例,支持根据登录方式动态切换输入项,同时升级了重置、验证及切换控件的交互行为。
    • 引入了更多自定义选项,包括禁用表单和自定义验证触发时机,提升了表单配置灵活性。
  • 文档

    • 完善了文档说明,新增 useWatch 演示与更清晰的 API 描述。
  • 样式

    • 优化了表单及表单项的布局响应,增强了禁用状态和 RTL 布局支持。

Copy link

coderabbitai bot commented Feb 10, 2025

Warning

Rate limit exceeded

@github-actions[bot] has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 5 minutes and 38 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 a2265f5 and 5cef618.

📒 Files selected for processing (3)
  • src/packages/form/demo.taro.tsx (4 hunks)
  • src/packages/form/demos/taro/demo7.tsx (1 hunks)
  • src/packages/form/demos/taro/demo8.tsx (1 hunks)

Walkthrough

此次提交对表单组件及其相关工具、文档、样例和测试做了大量改动。新增了对重置和动态监听字段的测试用例,同时调整了 Demo 示例的组件结构和布局。在核心逻辑中,增加了 FormProps 新属性(disabled、validateTrigger)并引入 useWatch hook,用于状态管理和动态响应。文档、样式和类型定义也相应更新,以反映这些改动。

Changes

文件 变更摘要
src/packages/form/tests/*.tsx 新增/修改 Form 组件测试:添加重置 username 测试、useWatch 测试,更新必填标签选择器;新增 merge/clone/recursive/isPlainObject 单元测试。
src/packages/form/(demo.taro.tsx, demo.tsx, demos/h5/*.tsx, demos/taro/*.tsx) 调整 Demo 示例:新增 Demo8 组件、更新布局(将 ScrollView/View 替换为 div/h2)、修改 Switch 项属性(添加 valuePropName="checked")、新增数量字段校验规则。
src/packages/form/(doc.en-US.md, doc.md, doc.taro.md, doc.zh-TW.md) 更新文档:新增 useWatch 章节与示例,完善 Form 组件 Props(validateTrigger、disabled)及 FormInstance 方法(setFieldValue、resetFields)的说明。
src/packages/form/(form.scss, formitem/formitem.scss) CSS 调整:删除部分旧样式、增加禁用状态、更新标签布局及 RTL 相关样式。
src/packages/form/(form.taro.tsx, form.tsx, index.taro.ts, index.ts, types.ts, formitem/(formitem.taro.tsx, formitem.tsx)) 核心组件及类型更新:在 FormProps 中新增 disabled 和 validateTrigger,更新 Context 传递内容,调整 FormInstance 接口和辅助方法(getClassNameWithDirection 等)。
src/packages/form/(useform.taro.ts, useform.ts), src/utils/(merge.ts, to-array.ts) 状态管理与工具扩展:新增 useWatch hook,扩展 FormStore 功能(registerWatch, notifyWatch, setFieldValue),增强 merge、clone 和 toArray 功能。

Sequence Diagram(s)

sequenceDiagram
  participant User as 用户
  participant Component as 表单组件
  participant FormInstance as Form 实例
  participant FormStore as FormStore
  participant Watcher as 注册的 Watch 回调

  User->>Component: 触发字段更新
  Component->>FormInstance: 调用 setFieldValue(...)
  FormInstance->>FormStore: 更新状态并调用 notifyWatch(...)
  FormStore->>Watcher: 通知所有注册的回调
  Watcher-->>Component: 更新组件状态,重新渲染
Loading

Possibly related PRs

Suggested reviewers

  • xiaoyatong
  • irisSong

Poem

嗨,我是一只快乐的小兔子,
跳跃在代码的草地上,
看新功能如鲜花绽放,
useWatch 的魔法闪耀光芒,
表单变得灵动又聪明,
踏着节奏,代码欢畅~
(_/)(๑˃̵ᴗ˂̵)


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.

@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) 3.x Target branch 3.x labels Feb 10, 2025
Copy link

codecov bot commented Feb 10, 2025

Codecov Report

Attention: Patch coverage is 90.62500% with 24 lines in your changes missing coverage. Please review.

Project coverage is 85.77%. Comparing base (c2b21ba) to head (5cef618).
Report is 8 commits behind head on feat_v3.x.

Files with missing lines Patch % Lines
src/packages/form/useform.ts 91.20% 11 Missing ⚠️
src/packages/formitem/formitem.tsx 85.18% 8 Missing ⚠️
src/utils/merge.ts 95.08% 3 Missing ⚠️
src/utils/to-array.ts 66.66% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #2976      +/-   ##
=============================================
+ Coverage      85.74%   85.77%   +0.03%     
=============================================
  Files            277      279       +2     
  Lines          18110    18272     +162     
  Branches        2745     2787      +42     
=============================================
+ Hits           15528    15673     +145     
- Misses          2577     2594      +17     
  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: 6

🧹 Nitpick comments (21)
src/utils/merge.ts (2)

3-7: 建议提高单测覆盖率。
当前对 main 函数的测试覆盖率不足,尤其是针对 clone 参数及多项合并的场景,可考虑在单测中添加更多调用场景,确保逻辑正确。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 6-7: src/utils/merge.ts#L6-L7
Added lines #L6 - L7 were not covered by tests


61-61: 增加递归合并的测试覆盖。
_merge 函数的递归合并路径 (line 61) 在自动化测试中缺少覆盖,建议添加嵌套对象合并的单测,确保该分支逻辑长期稳定。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 61-61: src/utils/merge.ts#L61
Added line #L61 was not covered by tests

src/packages/formitem/formitem.tsx (2)

109-120: defaultValue 警告逻辑。
在非生产环境下用 console.warn 提示用户应通过 initialValue 设置默认值,有助于减少用法混淆。此处逻辑清晰,可在文档中强调这一点。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 114-119: src/packages/formitem/formitem.tsx#L114-L119
Added lines #L114 - L119 were not covered by tests


143-161: 验证触发点的合并处理。
使用数组形式统一管理多个触发点,dispatch 校验请求。逻辑合理,但需注意在高频事件场景下避免频繁触发校验。

src/packages/formitem/formitem.taro.tsx (3)

110-120: defaultValue 警告机制。
与 Web 版本类似,对 defaultValue 的警告清楚明了,有效地引导用户正确用法。


145-161: 批量校验事件处理。
toArray 将 validateTrigger 数组化后逐个监听,dispatch 校验请求;在多触发点场景下能保证灵活度,但要注意对性能的把控。


223-224: 星号使用 Text 标签。
以 Text 显示必填星号更符合 Taro 环境组件习惯,便于后续样式覆盖与多端统一。

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

205-229: resetFields 方法
支持针对特定字段或所有字段进行重置,灵活度很高。注意执行部分删除操作后要保证其他字段的数据完整性不受影响。

🧰 Tools
🪛 Biome (1.9.4)

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


210-211: 可选链建议
如果担心 entity.props 可能为 undefined,建议使用可选链以避免潜在报错:

- const name = entity.props.name
+ const name = entity.props?.name
🧰 Tools
🪛 Biome (1.9.4)

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/packages/form/useform.ts (3)

177-194: validateFields 方法
对特定字段或全部字段执行批量校验,符合常见表单流程。可结合需求进一步优化校验效率,如增量校验或异步分批处理。


210-211: 可选链建议
若存在 entity.props 为 undefined 的可能,使用可选链可以避免潜在错误:

- const name = entity.props.name
+ const name = entity.props?.name
🧰 Tools
🪛 Biome (1.9.4)

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


124-128: 测试覆盖率不足
以下行尚未有相应的测试用例(124-128,142-144,213,290-291),建议进行单元测试补充,以提升可靠性。

Also applies to: 142-144, 213-213, 290-291

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 124-128: src/packages/form/useform.ts#L124-L128
Added lines #L124 - L128 were not covered by tests

src/utils/to-array.ts (1)

1-7: toArray 函数
功能简明:可将非数组或空值统一转化为数组。根据测试报告,3-4 行缺少测试覆盖,可考虑增加测试用例以确保健壮性。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 3-4: src/utils/to-array.ts#L3-L4
Added lines #L3 - L4 were not covered by tests

src/packages/form/types.ts (1)

3-7: 类型定义增强了表单验证功能!

新增的验证规则属性使表单验证更加灵活和完整。建议为 message 属性添加更详细的类型说明。

 export interface FormItemRuleWithoutValidator {
   [key: string]: any

   regex?: RegExp
   required?: boolean
-  message?: string
+  message?: string | ((value: any) => string)
 }
src/packages/form/demo.tsx (2)

2-2: 建议优化导入路径!

相对路径的使用可能会在项目结构调整时造成维护困难。建议使用别名导入。

-import { useTranslate } from '../../sites/assets/locale'
+import { useTranslate } from '@/sites/assets/locale'

10-10: 新功能展示完整!

新增的 useWatch 功能演示和对应的翻译文案集成得当。建议考虑添加更多注释说明 Demo8 的用途。

+// Demo8: 展示 useWatch 在动态表单中的应用
 import Demo8 from './demos/h5/demo8'

Also applies to: 22-23, 32-33, 53-54

src/packages/form/demos/h5/demo8.tsx (2)

4-4: 建议完善类型定义

建议将 FieldType 类型定义为 required 字段,因为表单初始值已经提供了这些字段。

-type FieldType = { account?: string; loginMethod?: 'mobile' | 'email' }
+type FieldType = { account: string; loginMethod: 'mobile' | 'email' }

16-37: 优化 Fragment 的使用

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

      footer={
-        <>
          <div
            style={{
              width: '100%',
            }}
          >
            <div
              style={{
                fontSize: '12px',
                textAlign: 'center',
                marginBottom: '20px',
              }}
            >
              你将使用{loginMethod === 'mobile' ? '手机号' : '电子邮箱'}
              {account}登录
            </div>
            <Button block type="primary" size="large" nativeType="submit">
              提交
            </Button>
          </div>
-        </>
      }
🧰 Tools
🪛 Biome (1.9.4)

[error] 16-39: 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)

src/packages/form/demos/taro/demo8.tsx (2)

4-4: 建议完善类型定义

建议将 FieldType 类型定义为 required 字段,因为表单初始值已经提供了这些字段。

-type FieldType = { account?: string; loginMethod?: 'mobile' | 'email' }
+type FieldType = { account: string; loginMethod: 'mobile' | 'email' }

16-37: 优化 Fragment 的使用

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

      footer={
-        <>
          <div
            style={{
              width: '100%',
            }}
          >
            <div
              style={{
                fontSize: '12px',
                textAlign: 'center',
                marginBottom: '20px',
              }}
            >
              你将使用{loginMethod === 'mobile' ? '手机号' : '电子邮箱'}
              {account}登录
            </div>
            <Button block type="primary" size="large" nativeType="submit">
              提交
            </Button>
          </div>
-        </>
      }
🧰 Tools
🪛 Biome (1.9.4)

[error] 16-39: 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)

src/packages/form/doc.en-US.md (1)

143-143: 调整 Form.useWatch 描述中的标点符号
注意到描述中标点符号存在松散問題,建議檢查並調整該處標點(例如替換逗號或調整其它符號),以符合文檔風格要求。

🧰 Tools
🪛 LanguageTool

[uncategorized] ~143-~143: Loose punctuation mark.
Context: ...idation | Promise | Form.useWatch(), this method will watch specified inputs...

(UNLIKELY_OPENING_PUNCTUATION)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a9a6292 and a2265f5.

📒 Files selected for processing (28)
  • src/packages/form/__tests__/form.spec.tsx (3 hunks)
  • src/packages/form/__tests__/merge.spec.ts (1 hunks)
  • src/packages/form/demo.taro.tsx (3 hunks)
  • src/packages/form/demo.tsx (4 hunks)
  • src/packages/form/demos/h5/demo1.tsx (1 hunks)
  • src/packages/form/demos/h5/demo7.tsx (1 hunks)
  • src/packages/form/demos/h5/demo8.tsx (1 hunks)
  • src/packages/form/demos/taro/demo1.tsx (1 hunks)
  • src/packages/form/demos/taro/demo4.tsx (2 hunks)
  • src/packages/form/demos/taro/demo7.tsx (2 hunks)
  • src/packages/form/demos/taro/demo8.tsx (1 hunks)
  • src/packages/form/doc.en-US.md (3 hunks)
  • src/packages/form/doc.md (3 hunks)
  • src/packages/form/doc.taro.md (3 hunks)
  • src/packages/form/doc.zh-TW.md (4 hunks)
  • src/packages/form/form.scss (0 hunks)
  • src/packages/form/form.taro.tsx (6 hunks)
  • src/packages/form/form.tsx (4 hunks)
  • src/packages/form/index.taro.ts (2 hunks)
  • src/packages/form/index.ts (2 hunks)
  • src/packages/form/types.ts (2 hunks)
  • src/packages/form/useform.taro.ts (10 hunks)
  • src/packages/form/useform.ts (9 hunks)
  • src/packages/formitem/formitem.scss (5 hunks)
  • src/packages/formitem/formitem.taro.tsx (8 hunks)
  • src/packages/formitem/formitem.tsx (8 hunks)
  • src/utils/merge.ts (1 hunks)
  • src/utils/to-array.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • src/packages/form/form.scss
✅ Files skipped from review due to trivial changes (1)
  • src/packages/form/demos/taro/demo4.tsx
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/utils/to-array.ts

[warning] 3-4: src/utils/to-array.ts#L3-L4
Added lines #L3 - L4 were not covered by tests

src/packages/formitem/formitem.tsx

[warning] 114-119: src/packages/formitem/formitem.tsx#L114-L119
Added lines #L114 - L119 were not covered by tests


[warning] 199-199: src/packages/formitem/formitem.tsx#L199
Added line #L199 was not covered by tests


[warning] 283-283: src/packages/formitem/formitem.tsx#L283
Added line #L283 was not covered by tests

src/utils/merge.ts

[warning] 6-7: src/utils/merge.ts#L6-L7
Added lines #L6 - L7 were not covered by tests


[warning] 61-61: src/utils/merge.ts#L61
Added line #L61 was not covered by tests

src/packages/form/useform.ts

[warning] 124-128: src/packages/form/useform.ts#L124-L128
Added lines #L124 - L128 were not covered by tests


[warning] 142-144: src/packages/form/useform.ts#L142-L144
Added lines #L142 - L144 were not covered by tests


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


[warning] 290-291: src/packages/form/useform.ts#L290-L291
Added lines #L290 - L291 were not covered by tests

🪛 Biome (1.9.4)
src/packages/form/demos/taro/demo8.tsx

[error] 16-39: 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)

src/packages/form/__tests__/form.spec.tsx

[error] 329-340: 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)


[error] 386-407: 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)

src/packages/form/demos/h5/demo8.tsx

[error] 16-39: 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)

src/packages/formitem/formitem.taro.tsx

[error] 244-245: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/packages/formitem/formitem.tsx

[error] 243-244: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/packages/form/useform.taro.ts

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

src/packages/form/useform.ts

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🪛 LanguageTool
src/packages/form/doc.en-US.md

[uncategorized] ~143-~143: Loose punctuation mark.
Context: ...idation | Promise | Form.useWatch(), this method will watch specified inputs...

(UNLIKELY_OPENING_PUNCTUATION)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (69)
src/utils/merge.ts (2)

25-43: 警惕复杂类型的克隆限制。
clone 函数在深拷贝数组与普通对象时没有处理如 Date、Map、Set 等特殊对象,可能导致克隆结果与预期不符。若需要支持更多类型,建议进行扩展。


68-89: 整体实现可读性较好。
_merge 函数通过 isClone、isRecursive 标记区分不同合并策略,逻辑清晰,且对原型链攻击等有基本防护,值得肯定。

src/packages/formitem/formitem.tsx (5)

7-7: 引入 toArray 函数。
此工具函数可将 validateTrigger 转为数组,非常实用,保证后续逻辑可靠性无虞。


78-90: 在 componentDidMount 中设置 initialValues。
对 store 缺少对应字段时才设置 initialValue,思路合理。建议确保在真正需要时才执行该逻辑,防止对已有数据造成覆盖。


91-96: 表单注册与事件监听。
通过 registerField 与 registerUpdate 动态管理组件实例和更新时机,避免频繁重渲染,提升性能,值得赞赏。


195-201: 根据 labelPosition 添加类名。
通过 getClassNameWithDirection 动态拼接类名,保证不同布局条件下的样式适配,方案简单易维护。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 199-199: src/packages/formitem/formitem.tsx#L199
Added line #L199 was not covered by tests


283-285: 进一步完善测试覆盖率。
noStyle 分支下的渲染逻辑(行 283)在测试中未完全覆盖,建议补充相应的测试用例,验证自定义样式场景是否符合预期。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 283-283: src/packages/formitem/formitem.tsx#L283
Added line #L283 was not covered by tests

src/packages/formitem/formitem.taro.tsx (7)

2-2: 引入 Taro 组件。
使用 Text 和 View 代替原生标签有助于在小程序环境中保持更好兼容性。实现较为直观,符合 Taro 风格。


79-90: 初始化表单数据逻辑。
若初始值不存在于 store 中才写入,能够避免覆盖用户已有数据。适用场景广,但仍需警惕大规模初始化时的性能开销。


92-97: registerField & registerUpdate。
组件挂载时注册自身,卸载时取消注册,流程完备,有效避免多余的事件回调、潜在内存泄漏等问题。


196-201: 动态样式函数命名可读性。
getClassNameWithDirection 动态拼接 labelPosition 后缀,逻辑清晰,可快速识别布局方向,建议保留或在文档注明。


212-213: align 属性新增支持。
为 Cell 组件增加 align,可使表单项的排版更灵活,进一步提升 UI 适配能力。


219-219: 错误信息管理。
item 数组统一管理字段错误,结合小程序端的 UI 呈现,此种做法可维护性高。


283-290: noStyle 分支的测试覆盖率不足。
Taro 环境下同样需要验证 noStyle 场景,建议增加对应用例,确保禁用默认布局时的渲染逻辑能正常工作。

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

1-1: 导入与类型定义看起来正常
本段代码引入了 React 的 Hooks,以及定义了新的 WatchCallback 类型。未发现明显问题,整体实现清晰。

Also applies to: 3-3, 7-7, 9-9, 14-14


49-49: 空行变更
无实质性修改。


77-79: updateStore 方法
这里直接用 nextStore 覆盖现有 store,需确保 nextStore 已包含所有必要数据,避免数据被意外覆盖或丢失。


87-92: setInitialValues 方法
在初始化时合并 initialValues 并及时调用 notifyWatch,有利于保持数据与监听方同步,逻辑完善。


100-102: setFieldsValue 方法
使用 recursive 深度合并后赋值到 store,并在更新逻辑中调用 notifyWatch,能保证所有监听都能及时收到最新字段值。

Also applies to: 120-121


123-129: setFieldValue 方法
为单个字段提供独立的更新和通知,进一步提高了使用者针对特定字段的可控性与灵活性。


274-296: watchList 相关逻辑
通过 registerWatch 与 notifyWatch 完成订阅与通知机制,在表单场景下非常实用;若业务场景中订阅数量庞大,需留意性能问题。

src/packages/form/useform.ts (8)

1-1: 导入与类型声明
新增的 Hooks 引入及 WatchCallback 类型定义清晰易读,与 taro 版本基本保持一致。

Also applies to: 3-3, 7-7, 9-9, 14-14


77-79: updateStore 方法
直接替换 store 也许在初期简化开发,不过务必确保 nextStore 已成功合并所有旧字段,避免误覆盖。


87-92: setInitialValues 方法
将 initialValues 与现有 store 合并后同步给监听,实现了初始值与实时数据的完整对接。


100-102: setFieldsValue 方法
深度合并新值后更新到 store 并触发监听,方便对多字段的批量更新。

Also applies to: 120-121


123-129: setFieldValue 方法
单独更新指定字段时立即触发 notifyWatch,与 setFieldsValue 一致,提升了字段级的可控性。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 124-128: src/packages/form/useform.ts#L124-L128
Added lines #L124 - L128 were not covered by tests


138-175: validateEntities 方法
支持多规则数组校验并在缺少 name 属性时发出警告,功能更完备。建议进一步测试不同场景的错误处理。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 142-144: src/packages/form/useform.ts#L142-L144
Added lines #L142 - L144 were not covered by tests


205-229: resetFields 方法
提供对多字段或所有字段的重置,满足通用需求。需谨慎确保与初始值对应关系正确,以免不必要的数据丢失。

🧰 Tools
🪛 Biome (1.9.4)

[error] 210-211: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🪛 GitHub Check: codecov/patch

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


274-296: watchList 机制
通过 watchList 数组维护订阅并在 store 更新后自动广播,对实时性要求较高的场景十分友好。注意在大规模表单场景的性能表现。

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 290-291: src/packages/form/useform.ts#L290-L291
Added lines #L290 - L291 were not covered by tests

src/packages/form/index.ts (1)

5-5: 代码变更符合最佳实践!

引入 useWatch hook 并将其添加到 InnerForm 组件中的实现方式非常规范,遵循了 React 组件设计模式。

Also applies to: 20-20, 27-27

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

5-5: 保持了H5和Taro版本的一致性!

代码变更与H5版本保持同步,确保了跨平台实现的一致性。

Also applies to: 20-20, 27-27

src/packages/form/types.ts (1)

24-24: 类型安全性得到提升!

setFieldValuesetFieldsValue 的类型定义更加严格,这有助于在开发时及早发现类型错误。

Also applies to: 26-26

src/packages/form/demo.taro.tsx (3)

12-12: 新增 Demo8 组件导入

新增的 Demo8 组件导入与现有的导入结构保持一致。


24-24: 新增 useWatch 国际化翻译

为 useWatch 功能添加了中英文翻译支持。

Also applies to: 34-34


41-58: 优化组件结构

将 ScrollView 和 View 组件替换为标准的 HTML div 和 h2 元素,使代码结构更加简洁。同时保持了功能的完整性。

src/packages/form/form.tsx (3)

14-14: 新增表单属性

为 FormProps 接口新增了以下属性:

  • disabled: 控制表单是否禁用
  • validateTrigger: 控制表单验证触发时机

这些属性的添加增强了表单的可配置性。

Also applies to: 16-16


27-27: 设置默认属性值

为新增的属性设置了合理的默认值:

  • disabled 默认为 false
  • validateTrigger 默认为 'onChange'

Also applies to: 29-29


105-109: 优化 Context 提供的值

Context.Provider 现在提供了更完整的上下文信息,包括表单实例、标签位置、禁用状态和验证触发时机。这样的改动使得子组件能够更方便地访问这些配置。

src/packages/form/form.taro.tsx (3)

2-2: 使用 Taro 表单组件

将标准 HTML form 元素替换为 Taro 的 TForm 组件,使其更好地适配小程序环境。

Also applies to: 87-87, 115-115


15-15: 同步表单属性更新

与 form.tsx 保持一致,新增了相同的表单属性和默认值:

  • disabled 属性及其默认值
  • validateTrigger 属性及其默认值

Also applies to: 17-17, 28-28, 30-30


106-110: Context 提供者更新

更新了 Context.Provider 的值,与 form.tsx 保持一致,确保了跨平台的一致性。

src/packages/form/demos/h5/demo7.tsx (1)

61-61: 优化 Switch 组件的值处理

为 Switch 组件的 Form.Item 添加了 valuePropName="checked" 属性,使其能够正确处理开关状态。这是一个必要的改进,因为 Switch 组件使用 checked 属性而不是 value 属性来控制其状态。

src/packages/form/demos/taro/demo7.tsx (2)

40-53: 表单底部布局优化

将 Taro 的 View 组件替换为标准的 HTML div 元素,提高了跨平台兼容性。


61-63: Switch 组件值绑定优化

添加 valuePropName="checked" 属性,确保 Switch 组件的值正确绑定到表单状态。

src/packages/form/__tests__/merge.spec.ts (4)

3-104: merge 函数测试用例完善

测试用例覆盖了以下关键场景:

  • 基本对象合并
  • 嵌套对象合并
  • 目标对象克隆
  • 非普通对象处理
  • 原型污染防护

测试覆盖率全面,代码结构清晰。


106-144: clone 函数测试用例完善

测试用例验证了:

  • 对象克隆
  • 数组克隆
  • 非普通对象处理

测试设计合理,确保了深度克隆的正确性。


146-215: recursive 函数测试用例完善

测试用例验证了:

  • 递归合并
  • 递归克隆
  • 非普通对象处理
  • 原型污染防护

测试覆盖了关键功能点。


217-231: isPlainObject 函数测试用例完善

测试用例验证了各种类型的对象判断,包括普通对象和非普通对象的识别。

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

317-368: 表单重置功能测试完善

新增测试用例验证表单字段重置功能:

  • 验证字段值变更
  • 验证错误信息清除
  • 验证重置后的状态

测试设计合理,覆盖了重要场景。

🧰 Tools
🪛 Biome (1.9.4)

[error] 329-340: 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)


370-438: useWatch 钩子功能测试完善

新增测试用例验证表单字段监听功能:

  • 验证登录方式切换
  • 验证动态表单项渲染
  • 验证状态更新

测试用例设计完整,确保了动态表单行为的正确性。

🧰 Tools
🪛 Biome (1.9.4)

[error] 386-407: 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)

src/packages/formitem/formitem.scss (4)

5-8: 表单项禁用状态样式优化

新增 nut-form-item-disabled 类,通过透明度和指针事件控制禁用状态的视觉效果。


23-29: 必填标记样式优化

新增 nut-form-item-label-required 类,优化了必填标记的位置和样式。


127-161: 表单布局样式增强

新增多个布局相关的类:

  • nut-form-item-label-right: 标签右对齐
  • nut-form-item-label-left: 标签左对齐
  • nut-form-item-label-top: 标签顶部对齐
  • nut-form-item-body-top: 内容区域样式

提供了更灵活的布局选项。


163-192: RTL 支持优化

增强了从右到左(RTL)布局支持:

  • 优化了标签和内容的对齐方式
  • 调整了内边距和外边距
  • 完善了必填标记的位置

确保了在 RTL 布局下的正确显示。

src/packages/form/doc.zh-TW.md (4)

61-65: 新增 useWatch 展示部分
此處新增了 useWatch 示範區塊以及對應的 demo 代碼(demo8.tsx),請確認該範例與文檔描述一致,並與其他語言版本保持同步。


90-91: 新增 validateTrigger 與 disabled 屬性
在 Props 表中新增了 validateTrigger(統一設定字段觸發驗證的時機)和 disabled(是否禁用)屬性,描述清晰,請確認這些說明與實際代碼實現一致。


137-139: 更新 FormInstance 方法說明
文檔中對 setFieldsValue 的說明進行了補充,並新增了 setFieldValue 方法,同時更新了 resetFields 的簽名說明,建議再次檢查描述是否準確反映了用法,特別是關於對象直接傳入 form store 的提醒。


142-142: 更新 Form.useWatch 方法描述
新增了對 Form.useWatch 方法的描述,說明其監聽指定輸入並返回值的用途,請確認描述與實際功能相符,並與其他版本保持一致。

src/packages/form/doc.md (4)

61-67: 新增 useWatch 示範區塊
該區塊新增了 useWatch 示範及 demo 代碼(demo8.tsx),請確認示例代碼正確運作並能幫助用戶理解該功能。


89-90: 新增的 validateTrigger 與 disabled 屬性
在 Props 表中更新新增了 validateTrigger 與 disabled 屬性,描述清晰,可與代碼實現保持一致,請檢查與其他語言版本的文檔描述是否一致。


137-140: 更新 FormInstance 方法說明
文檔中對 setFieldsValue 進行了補充說明,並新增了 setFieldValue 方法,同時調整了 resetFields 的簽名,建議確保這些更新能準確指導用戶使用該 API。


143-143: 更新 Form.useWatch 說明
針對 Form.useWatch 方法的說明進行了補充,確保用戶能清楚理解其用途及返回值,請再次確認描述與組件實際功能對應。

src/packages/form/doc.taro.md (4)

61-67: 新增 Taro 版 useWatch 示範區塊
在 Taro 文檔中新增了 useWatch 部分,並引用了 taro/demo8.tsx 的代碼,請確認該範例在 Taro 環境下能正常運作且描述準確。


89-90: 新增的 validateTrigger 與 disabled 屬性(Taro 版)
Props 表中新增了 validateTrigger 與 disabled 屬性,描述與其他版本基本一致,請再次核對是否與 Taro 端組件實現匹配。


137-140: 更新 FormInstance 部分的新方法說明
新增了 setFieldValue 方法並更新了 resetFields 的簽名,同時對 setFieldsValue 的說明進行了補充,請確認這些描述能夠準確指導 Taro 端用戶。


143-143: 更新 Form.useWatch 描述(Taro 版)
對 Form.useWatch 方法的描述進行了更新,建議再次檢查該描述是否能夠清晰說明方法用途,並與其他文檔保持一致。

src/packages/form/doc.en-US.md (3)

61-67: Added useWatch Demo Section
A new useWatch demo section has been added with a reference to h5/demo8.tsx. Please ensure that the demo code is correct and clearly illustrates the useWatch functionality as described.


91-92: Newly Added disabled and validateTrigger Properties
The Props table now includes the disabled and validateTrigger properties. Please verify that their descriptions match the component implementation and are consistent with the documentation in other language versions.


138-141: Updates to FormInstance Method Descriptions
The documentation has been updated to include a new setFieldValue method and revised descriptions for setFieldsValue and resetFields. Ensure that these updates clearly convey the intended usage to users.

src/packages/formitem/formitem.tsx Show resolved Hide resolved
src/packages/formitem/formitem.taro.tsx Show resolved Hide resolved
src/packages/form/demos/h5/demo8.tsx Show resolved Hide resolved
src/packages/form/demos/taro/demo8.tsx Show resolved Hide resolved
src/packages/form/demos/h5/demo1.tsx Show resolved Hide resolved
src/packages/form/demos/taro/demo1.tsx Show resolved Hide resolved
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.

标签使用需要过一下,如 div 改为 view~

src/packages/form/demo.taro.tsx Outdated Show resolved Hide resolved
src/packages/form/demos/taro/demo4.tsx Show resolved Hide resolved
@github-actions github-actions bot added action:review This PR needs more reviews (less than 2 approvals) and removed action:review This PR needs more reviews (less than 2 approvals) labels Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants