Commit 4c67d8c38807929f8ebc3d9cd03a93992e50debe
Committed by
GitHub
1 parent
0ab2a541
feat: 使用useForm时调用setFieldsValue后,组件的onChange未主动触发 (#2142) (#2503)
Co-authored-by: maxiaojing <maxiaojing@hashdata.cn>
Showing
2 changed files
with
17 additions
and
10 deletions
src/components/Form/src/hooks/useFormEvents.ts
... | ... | @@ -76,6 +76,11 @@ export function useFormEvents({ |
76 | 76 | const hasKey = Reflect.has(values, key); |
77 | 77 | |
78 | 78 | value = handleInputNumberValue(schema?.component, value); |
79 | + const { componentProps } = schema || {}; | |
80 | + let _props = componentProps as any; | |
81 | + if (typeof componentProps === 'function') { | |
82 | + _props = _props({ formModel: unref(formModel) }); | |
83 | + } | |
79 | 84 | // 0| '' is allow |
80 | 85 | if (hasKey && fields.includes(key)) { |
81 | 86 | // time type |
... | ... | @@ -85,17 +90,15 @@ export function useFormEvents({ |
85 | 90 | for (const ele of value) { |
86 | 91 | arr.push(ele ? dateUtil(ele) : null); |
87 | 92 | } |
88 | - formModel[key] = arr; | |
93 | + unref(formModel)[key] = arr; | |
89 | 94 | } else { |
90 | - const { componentProps } = schema || {}; | |
91 | - let _props = componentProps as any; | |
92 | - if (typeof componentProps === 'function') { | |
93 | - _props = _props({ formModel }); | |
94 | - } | |
95 | - formModel[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; | |
95 | + unref(formModel)[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; | |
96 | 96 | } |
97 | 97 | } else { |
98 | - formModel[key] = value; | |
98 | + unref(formModel)[key] = value; | |
99 | + } | |
100 | + if (_props?.onChange) { | |
101 | + _props?.onChange(value); | |
99 | 102 | } |
100 | 103 | validKeys.push(key); |
101 | 104 | } else { |
... | ... | @@ -103,13 +106,13 @@ export function useFormEvents({ |
103 | 106 | try { |
104 | 107 | const value = nestKey.split('.').reduce((out, item) => out[item], values); |
105 | 108 | if (isDef(value)) { |
106 | - formModel[nestKey] = value; | |
109 | + unref(formModel)[nestKey] = unref(value); | |
107 | 110 | validKeys.push(nestKey); |
108 | 111 | } |
109 | 112 | } catch (e) { |
110 | 113 | // key not exist |
111 | 114 | if (isDef(defaultValueRef.value[nestKey])) { |
112 | - formModel[nestKey] = cloneDeep(defaultValueRef.value[nestKey]); | |
115 | + unref(formModel)[nestKey] = cloneDeep(unref(defaultValueRef.value[nestKey])); | |
113 | 116 | } |
114 | 117 | } |
115 | 118 | }); | ... | ... |
src/views/demo/form/RuleForm.vue
... | ... | @@ -98,6 +98,9 @@ |
98 | 98 | key: '2', |
99 | 99 | }, |
100 | 100 | ], |
101 | + onChange: (value) => { | |
102 | + console.log(value, '123'); | |
103 | + }, | |
101 | 104 | }, |
102 | 105 | rules: [ |
103 | 106 | { |
... | ... | @@ -235,6 +238,7 @@ |
235 | 238 | function setFormValues() { |
236 | 239 | setFieldsValue({ |
237 | 240 | field1: 1111, |
241 | + field4: ['1'], | |
238 | 242 | field5: ['1'], |
239 | 243 | field7: '1', |
240 | 244 | field33: '2020-12-12', | ... | ... |