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组件validate函数的返回值类型 #3203

Open
wants to merge 1 commit into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/packages/__VUE/form/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DOMWrapper, mount } from '@vue/test-utils'
import { nextTick, ref } from 'vue'
import { Form, FormItem, Button, Textarea, Switch, Checkbox, Rate, InputNumber, Range, Uploader } from '@nutui/nutui'
import { Form, FormItem, Button, Textarea, Switch, Checkbox, Rate, InputNumber, Range, Uploader, FormInstance } from '@nutui/nutui'

test('base Form', () => {
const wrapper = mount(() => {
Expand Down Expand Up @@ -47,9 +47,9 @@ test('base Dynamic Form', async () => {
value: ''
})
})
const formRef = ref()
const formRef = ref<FormInstance>()
const submit = () => {
formRef.value.validate().then(({ valid }: any) => {
formRef.value!.validate().then(({ valid }) => {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

关于使用非空断言操作符的建议

submitreset 函数中使用非空断言操作符 (!) 可以解决类型检查的问题,但可能会隐藏潜在的运行时错误。建议考虑以下几点:

  1. 优点:简化了类型检查,避免了 TypeScript 的错误提示。
  2. 缺点:如果 formRef.value 在运行时确实为 null,可能会导致意外的错误。

建议使用更安全的方式处理可能为 null 的情况:

if (formRef.value) {
  formRef.value.validate().then(({ valid }) => {
    // ...
  })
}

这样可以在运行时避免潜在的 null 引用错误,同时保持代码的类型安全性。

Also applies to: 61-61

if (valid) {
// console.log('success', dynamicForm);
} else {
Expand All @@ -58,7 +58,7 @@ test('base Dynamic Form', async () => {
})
}
const reset = () => {
formRef.value.reset()
formRef.value!.reset()
}
const remove = () => {
val.value.tels.pop()
Expand Down
2 changes: 1 addition & 1 deletion src/packages/__VUE/form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const checkRule = async (item: FormRule): Promise<FormErrorMessage | boolean> =>
* @param customProp 指定校验,用于用户自定义场景时触发,例如 blur、change 事件
* @returns
*/
const validate = (customProp = '') => {
const validate = (customProp = ''): Promise<{ valid: boolean, errors: any[] }> => {
return new Promise((resolve, reject) => {
try {
const task = getTaskFromChildren()
Expand Down