From 4c67d8c38807929f8ebc3d9cd03a93992e50debe Mon Sep 17 00:00:00 2001 From: vivianma83 <7566436+vivianma83@users.noreply.github.com> Date: Wed, 18 Jan 2023 11:15:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8useForm=E6=97=B6?= =?UTF-8?q?=E8=B0=83=E7=94=A8setFieldsValue=E5=90=8E=EF=BC=8C=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=9A=84onChange=E6=9C=AA=E4=B8=BB=E5=8A=A8=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=20(#2142)=20(#2503)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: maxiaojing --- .../Form/src/hooks/useFormEvents.ts | 23 +++++++++++-------- src/views/demo/form/RuleForm.vue | 4 ++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index 768089d0c..66b833719 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -76,6 +76,11 @@ export function useFormEvents({ const hasKey = Reflect.has(values, key); value = handleInputNumberValue(schema?.component, value); + const { componentProps } = schema || {}; + let _props = componentProps as any; + if (typeof componentProps === 'function') { + _props = _props({ formModel: unref(formModel) }); + } // 0| '' is allow if (hasKey && fields.includes(key)) { // time type @@ -85,17 +90,15 @@ export function useFormEvents({ for (const ele of value) { arr.push(ele ? dateUtil(ele) : null); } - formModel[key] = arr; + unref(formModel)[key] = arr; } else { - const { componentProps } = schema || {}; - let _props = componentProps as any; - if (typeof componentProps === 'function') { - _props = _props({ formModel }); - } - formModel[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; + unref(formModel)[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; } } else { - formModel[key] = value; + unref(formModel)[key] = value; + } + if (_props?.onChange) { + _props?.onChange(value); } validKeys.push(key); } else { @@ -103,13 +106,13 @@ export function useFormEvents({ try { const value = nestKey.split('.').reduce((out, item) => out[item], values); if (isDef(value)) { - formModel[nestKey] = value; + unref(formModel)[nestKey] = unref(value); validKeys.push(nestKey); } } catch (e) { // key not exist if (isDef(defaultValueRef.value[nestKey])) { - formModel[nestKey] = cloneDeep(defaultValueRef.value[nestKey]); + unref(formModel)[nestKey] = cloneDeep(unref(defaultValueRef.value[nestKey])); } } }); diff --git a/src/views/demo/form/RuleForm.vue b/src/views/demo/form/RuleForm.vue index 8124dae75..183c0471e 100644 --- a/src/views/demo/form/RuleForm.vue +++ b/src/views/demo/form/RuleForm.vue @@ -98,6 +98,9 @@ key: '2', }, ], + onChange: (value) => { + console.log(value, '123'); + }, }, rules: [ { @@ -235,6 +238,7 @@ function setFormValues() { setFieldsValue({ field1: 1111, + field4: ['1'], field5: ['1'], field7: '1', field33: '2020-12-12',