-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import { deepClone, getPropByPath, isDef, isPromise } from '../common/util' | |
import { useChildren } from '../composables/useChildren' | ||
import { useToast } from '../wd-toast' | ||
import { type FormRules, FORM_KEY, type ErrorMessage, formProps, type FormExpose } from './types' | ||
import { onMounted } from 'vue' | ||
|
||
const { show: showToast } = useToast('wd-form-toast') | ||
const props = defineProps(formProps) | ||
|
@@ -180,7 +181,25 @@ function reset() { | |
clearMessage() | ||
} | ||
|
||
defineExpose<FormExpose>({ validate, reset }) | ||
/** | ||
* 重置表单项为初始值,并清空验证提示 | ||
*/ | ||
let initialValue: any = undefined | ||
function resetFields() { | ||
let model = props.model | ||
children.forEach((field) => { | ||
model[field.prop] = deepClone(initialValue[field.prop]) | ||
}) | ||
|
||
reset() | ||
} | ||
Comment on lines
+187
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要增强类型安全性和错误处理 当前实现存在以下问题:
建议按照以下方式重构: -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])
+ })
+ }
+})
|
||
|
||
onMounted(() => { | ||
// 保存初始值 | ||
initialValue = deepClone(props.model) | ||
}) | ||
Comment on lines
+197
to
+200
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 建议优化初始值的处理机制 当前实现在组件挂载时只保存一次初始值,这可能导致以下问题:
建议改进如下: +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
+})
|
||
|
||
defineExpose<FormExpose>({ validate, reset, resetFields }) | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要替换版本号占位符
$LOWEST_VERSION$
需要替换为实际的版本号。📝 Committable suggestion