Commit 1b30834eb36b7e1730b01ce6af602444937d00ef
Committed by
GitHub
1 parent
c5f97b9d
feat: 解构字段设置value (#2648)
Showing
1 changed file
with
53 additions
and
9 deletions
src/components/Form/src/hooks/useFormEvents.ts
... | ... | @@ -14,7 +14,7 @@ import { |
14 | 14 | import { deepMerge } from '/@/utils'; |
15 | 15 | import { dateItemType, handleInputNumberValue, defaultValueComponents } from '../helper'; |
16 | 16 | import { dateUtil } from '/@/utils/dateUtil'; |
17 | -import { cloneDeep, uniqBy } from 'lodash-es'; | |
17 | +import { cloneDeep, set, uniqBy } from 'lodash-es'; | |
18 | 18 | import { error } from '/@/utils/log'; |
19 | 19 | |
20 | 20 | interface UseFormActionContext { |
... | ... | @@ -27,6 +27,47 @@ interface UseFormActionContext { |
27 | 27 | schemaRef: Ref<FormSchema[]>; |
28 | 28 | handleFormValues: Fn; |
29 | 29 | } |
30 | + | |
31 | +function tryConstructArray(field: string, values: Recordable = {}): any[] | undefined { | |
32 | + const pattern = /^\[(.+)\]$/; | |
33 | + if (pattern.test(field)) { | |
34 | + const match = field.match(pattern); | |
35 | + if (match && match[1]) { | |
36 | + const keys = match[1].split(','); | |
37 | + if (!keys.length) { | |
38 | + return undefined; | |
39 | + } | |
40 | + | |
41 | + const result = []; | |
42 | + keys.forEach((k, index) => { | |
43 | + set(result, index, values[k.trim()]); | |
44 | + }); | |
45 | + | |
46 | + return result.length ? result : undefined; | |
47 | + } | |
48 | + } | |
49 | +} | |
50 | + | |
51 | +function tryConstructObject(field: string, values: Recordable = {}): Recordable | undefined { | |
52 | + const pattern = /^\{(.+)\}$/; | |
53 | + if (pattern.test(field)) { | |
54 | + const match = field.match(pattern); | |
55 | + if (match && match[1]) { | |
56 | + const keys = match[1].split(','); | |
57 | + if (!keys.length) { | |
58 | + return; | |
59 | + } | |
60 | + | |
61 | + const result = {}; | |
62 | + keys.forEach((k) => { | |
63 | + set(result, k.trim(), values[k.trim()]); | |
64 | + }); | |
65 | + | |
66 | + return Object.values(result).filter(Boolean).length ? result : undefined; | |
67 | + } | |
68 | + } | |
69 | +} | |
70 | + | |
30 | 71 | export function useFormEvents({ |
31 | 72 | emit, |
32 | 73 | getProps, |
... | ... | @@ -69,7 +110,7 @@ export function useFormEvents({ |
69 | 110 | const nestKeyArray = fields.filter((item) => String(item).indexOf(delimiter) >= 0); |
70 | 111 | |
71 | 112 | const validKeys: string[] = []; |
72 | - Object.keys(values).forEach((key) => { | |
113 | + fields.forEach((key) => { | |
73 | 114 | const schema = unref(getSchema).find((item) => item.field === key); |
74 | 115 | let value = values[key]; |
75 | 116 | |
... | ... | @@ -81,24 +122,28 @@ export function useFormEvents({ |
81 | 122 | if (typeof componentProps === 'function') { |
82 | 123 | _props = _props({ formModel: unref(formModel) }); |
83 | 124 | } |
125 | + | |
126 | + const constructValue = tryConstructArray(key, values) || tryConstructObject(key, values); | |
127 | + | |
84 | 128 | // 0| '' is allow |
85 | - if (hasKey && fields.includes(key)) { | |
129 | + if (hasKey || !!constructValue) { | |
130 | + const fieldValue = constructValue || value; | |
86 | 131 | // time type |
87 | 132 | if (itemIsDateType(key)) { |
88 | - if (Array.isArray(value)) { | |
133 | + if (Array.isArray(fieldValue)) { | |
89 | 134 | const arr: any[] = []; |
90 | - for (const ele of value) { | |
135 | + for (const ele of fieldValue) { | |
91 | 136 | arr.push(ele ? dateUtil(ele) : null); |
92 | 137 | } |
93 | 138 | unref(formModel)[key] = arr; |
94 | 139 | } else { |
95 | - unref(formModel)[key] = value ? (_props?.valueFormat ? value : dateUtil(value)) : null; | |
140 | + unref(formModel)[key] = fieldValue ? (_props?.valueFormat ? fieldValue : dateUtil(fieldValue)) : null; | |
96 | 141 | } |
97 | 142 | } else { |
98 | - unref(formModel)[key] = value; | |
143 | + unref(formModel)[key] = fieldValue; | |
99 | 144 | } |
100 | 145 | if (_props?.onChange) { |
101 | - _props?.onChange(value); | |
146 | + _props?.onChange(fieldValue); | |
102 | 147 | } |
103 | 148 | validKeys.push(key); |
104 | 149 | } else { |
... | ... | @@ -335,4 +380,3 @@ export function useFormEvents({ |
335 | 380 | setFieldsValue, |
336 | 381 | scrollToField, |
337 | 382 | }; |
338 | -} | ... | ... |