From 3d2db6ce982595a3dda50761e3bfcee87cabc6c5 Mon Sep 17 00:00:00 2001 From: jiaoxueyan <2016036930@qq.com> Date: Mon, 18 Nov 2024 20:38:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20=E9=87=8D=E7=BD=AE=E8=A1=A8?= =?UTF-8?q?=E5=8D=95=E9=A1=B9=E4=B8=BA=E5=88=9D=E5=A7=8B=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E7=A7=BB=E9=99=A4=E6=A0=A1=E9=AA=8C=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #701 --- docs/component/form.md | 1 + src/pages/form/Index.vue | 8 +++++++ .../components/wd-form/types.ts | 4 ++++ .../components/wd-form/wd-form.vue | 21 ++++++++++++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/component/form.md b/docs/component/form.md index d93efc8c1..f2616d1a7 100644 --- a/docs/component/form.md +++ b/docs/component/form.md @@ -942,6 +942,7 @@ function handleIconClick() { | -------- | ------------------------------------------------------------------------------ | --------------- | -------- | | validate | 验证表单,支持传入一个 prop 来验证单个表单项,不传入 prop 时,会验证所有表单项 | `prop?: string` | 0.2.0 | | reset | 重置校验结果 | - | 0.2.0 | +| resetFields | 重置表单项为初始值,并移除校验结果 | - | $LOWEST_VERSION$ | ## 外部样式类 diff --git a/src/pages/form/Index.vue b/src/pages/form/Index.vue index fa18a6344..b34c60ce5 100644 --- a/src/pages/form/Index.vue +++ b/src/pages/form/Index.vue @@ -25,6 +25,7 @@ 提交 + 重置 @@ -194,6 +195,10 @@ function handleSubmit2() { }) } +function resetFields1() { + form1.value!.resetFields() +} + function handleClick1() { uni.navigateTo({ url: '/pages/form/demo1' }) } @@ -218,5 +223,8 @@ function handleClick4() { } .footer { padding: 16px; + .reset { + margin-top: 10px; + } } diff --git a/src/uni_modules/wot-design-uni/components/wd-form/types.ts b/src/uni_modules/wot-design-uni/components/wd-form/types.ts index 176b14620..c634d862c 100644 --- a/src/uni_modules/wot-design-uni/components/wd-form/types.ts +++ b/src/uni_modules/wot-design-uni/components/wd-form/types.ts @@ -80,6 +80,10 @@ export type FormExpose = { * 重置表单项的验证提示 */ reset: () => void + /** + * 重置表单项为初始值,并清空验证提示 + */ + resetFields: () => void } export type FormInstance = ComponentPublicInstance diff --git a/src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue b/src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue index 3c509d13b..36c0c8f79 100644 --- a/src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue +++ b/src/uni_modules/wot-design-uni/components/wd-form/wd-form.vue @@ -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({ validate, reset }) +/** + * 重置表单项为初始值,并清空验证提示 + */ +let initialValue: any = undefined +function resetFields() { + let model = props.model + children.forEach((field) => { + model[field.prop] = deepClone(initialValue[field.prop]) + }) + + reset() +} + +onMounted(() => { + // 保存初始值 + initialValue = deepClone(props.model) +}) + +defineExpose({ validate, reset, resetFields })