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: ✨ 重置表单项为初始值,并移除校验结果 #719

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jiaoxueyan
Copy link
Contributor

@jiaoxueyan jiaoxueyan commented Nov 18, 2024

Closes: #701

🤔 这个 PR 的性质是?(至少选择一个)

  • 日常 bug 修复
  • 新特性提交
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • CI/CD 改进
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 代码重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

#701

💡 需求背景和解决方案

  1. form组建增加resetFields事件,重置表单项为初始值,并移除校验结果。

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

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能

    • 在表单组件中添加了重置字段的功能,用户可以通过新按钮快速清空输入。
    • 文档更新,详细说明了表单的验证错误显示、验证规则、动态表单功能和复杂表单示例。
  • 文档

    • 增强了 wd-form 组件的文档,提供更清晰的功能和用法说明。

Copy link

vercel bot commented Nov 18, 2024

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

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 19, 2024 1:40am

Copy link

coderabbitai bot commented Nov 18, 2024

Walkthrough

本次变更涉及对wd-form组件的文档和功能的增强。文档中增加了关于验证错误显示方法、验证规则、动态表单能力和复杂表单示例的详细说明。功能方面,在Vue组件中新增了一个重置按钮,允许用户快速清空输入字段,同时在wd-form组件中添加了resetFields方法,以重置表单项到初始值并清除验证信息。

Changes

文件路径 变更摘要
docs/component/form.md 增加了验证错误显示方法、验证规则、动态表单能力和复杂表单示例的详细说明。
src/pages/form/Index.vue 添加了重置按钮<wd-button>,并定义了resetFields1方法以清空表单输入。
src/uni_modules/wot-design-uni/components/wd-form/types.ts FormExpose类型中添加了resetFields方法,用于重置表单项和清除验证信息。
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue 添加了resetFields方法并修改了defineExpose以包含该方法,增强了表单的重置功能。

Assessment against linked issues

Objective Addressed Explanation
添加重置表单功能 (701)

Possibly related PRs

Suggested reviewers

  • Moonofweisheng

🐰 在草地上跳跃,重置表单真好,
输入清空,烦恼全抛,
文档更新,功能更妙,
小兔子欢呼,快乐无比,
一起享受,编程的乐趣! 🌼✨


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

netlify bot commented Nov 18, 2024

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d3466ca
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/673bec52a4a91f0008e6813b
😎 Deploy Preview https://deploy-preview-719--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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: 3

🧹 Outside diff range and nitpick comments (2)
src/uni_modules/wot-design-uni/components/wd-form/types.ts (1)

83-86: 类型定义符合需求,建议补充文档说明

resetFields 方法的类型定义清晰且符合重置表单的需求。不过建议在注释中详细说明 resetresetFields 两个方法的具体区别,以便开发者选择合适的方法使用。

建议将注释修改为:

  /**
-  * 重置表单项为初始值,并清空验证提示
+  * 重置表单项为初始值,并清空验证提示。
+  * 注意:与 reset 方法的区别是,reset 只清空验证提示,而 resetFields 会同时重置值为初始值
   */
src/pages/form/Index.vue (1)

198-200: 建议增加空值检查以提高类型安全性

当前实现使用了非空断言操作符(!),这可能在运行时导致潜在的空值错误。

建议修改为:

function resetFields1() {
-  form1.value!.resetFields()
+  if (form1.value) {
+    form1.value.resetFields()
+  }
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 314c2e8 and 3d2db6c.

📒 Files selected for processing (4)
  • docs/component/form.md (1 hunks)
  • src/pages/form/Index.vue (3 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/types.ts (1 hunks)
  • src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (2 hunks)
🔇 Additional comments (5)
src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue (2)

26-26: 导入声明看起来没有问题!

onMounted 的导入符合 Vue 3 组合式 API 的最佳实践。


202-202: 方法暴露的更新符合预期!

resetFields 方法的暴露正确使用了 FormExpose 类型,并与现有的 validatereset 方法保持一致。

src/pages/form/Index.vue (2)

28-28: 重置按钮实现符合需求

重置按钮的实现完全符合issue #701中提出的需求,并且保持了与提交按钮一致的样式。


226-228: 样式实现合理

重置按钮的样式设置合理,与整体表单布局协调。

docs/component/form.md (1)

945-945: 新增的重置功能文档完善且清晰

resetFields 事件的文档说明符合需求,描述准确清晰。该功能满足了 Issue #701 中提出的重置表单项的需求。

Comment on lines +187 to +195
let initialValue: any = undefined
function resetFields() {
let model = props.model
children.forEach((field) => {
model[field.prop] = deepClone(initialValue[field.prop])
})

reset()
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

需要增强类型安全性和错误处理

当前实现存在以下问题:

  1. 直接访问和修改 model[field.prop] 缺少类型检查
  2. 没有对 field.prop 进行空值检查
  3. 直接修改 props 可能违反 Vue 的单向数据流原则

建议按照以下方式重构:

-let model = props.model
-children.forEach((field) => {
-  model[field.prop] = deepClone(initialValue[field.prop])
-})
+const model = props.model
+children.forEach((field) => {
+  if (field.prop && initialValue && initialValue[field.prop] !== undefined) {
+    // 建议通过 emit 事件通知父组件更新,而不是直接修改
+    emit('update:model', {
+      ...model,
+      [field.prop]: deepClone(initialValue[field.prop])
+    })
+  }
+})

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +197 to +200
onMounted(() => {
// 保存初始值
initialValue = deepClone(props.model)
})
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

建议优化初始值的处理机制

当前实现在组件挂载时只保存一次初始值,这可能导致以下问题:

  1. 如果 props.model 后续发生变化,初始值不会更新
  2. 组件卸载时没有清理 initialValue

建议改进如下:

+import { ref, watch } from 'vue'
-let initialValue: any = undefined
+const initialValue = ref<any>(undefined)

+watch(() => props.model, (newModel) => {
+  initialValue.value = deepClone(newModel)
+}, { immediate: true, deep: true })

-onMounted(() => {
-  // 保存初始值
-  initialValue = deepClone(props.model)
-})

+onUnmounted(() => {
+  initialValue.value = undefined
+})

Committable suggestion skipped: line range outside the PR's diff.

@@ -942,6 +942,7 @@ function handleIconClick() {
| -------- | ------------------------------------------------------------------------------ | --------------- | -------- |
| validate | 验证表单,支持传入一个 prop 来验证单个表单项,不传入 prop 时,会验证所有表单项 | `prop?: string` | 0.2.0 |
| reset | 重置校验结果 | - | 0.2.0 |
| resetFields | 重置表单项为初始值,并移除校验结果 | - | $LOWEST_VERSION$ |
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

需要替换版本号占位符

$LOWEST_VERSION$ 需要替换为实际的版本号。

-| resetFields | 重置表单项为初始值,并移除校验结果                                              | -               | $LOWEST_VERSION$    |
+| resetFields | 重置表单项为初始值,并移除校验结果                                              | -               | 1.4.0    |
📝 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
| resetFields | 重置表单项为初始值,并移除校验结果 | - | $LOWEST_VERSION$ |
| resetFields | 重置表单项为初始值,并移除校验结果 | - | 1.4.0 |

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.

form表单是否可以加一个重置表单功能
1 participant