Commit 79eb909c66ce5f17368c0d6f93945139bc9956a7
Committed by
GitHub
1 parent
92722093
feat: make fields key of form deconstructable (#1663)
Co-authored-by: liheng.wu <liheng.wu@tcl.com>
Showing
2 changed files
with
58 additions
and
1 deletions
src/components/Form/src/hooks/useFormValues.ts
@@ -11,6 +11,43 @@ interface UseFormValuesContext { | @@ -11,6 +11,43 @@ interface UseFormValuesContext { | ||
11 | getProps: ComputedRef<FormProps>; | 11 | getProps: ComputedRef<FormProps>; |
12 | formModel: Recordable; | 12 | formModel: Recordable; |
13 | } | 13 | } |
14 | + | ||
15 | +/** | ||
16 | + * @desription deconstruct array-link key. This method will mutate the target. | ||
17 | + */ | ||
18 | +function tryDeconstructArray(key: string, value: any, target: Recordable) { | ||
19 | + const pattern = /^\[(.+)\]$/; | ||
20 | + if (pattern.test(key)) { | ||
21 | + const match = key.match(pattern); | ||
22 | + if (match && match[1]) { | ||
23 | + const keys = match[1].split(','); | ||
24 | + value = Array.isArray(value) ? value : [value]; | ||
25 | + keys.forEach((k, index) => { | ||
26 | + set(target, k.trim(), value[index]); | ||
27 | + }); | ||
28 | + return true; | ||
29 | + } | ||
30 | + } | ||
31 | +} | ||
32 | + | ||
33 | +/** | ||
34 | + * @desription deconstruct object-link key. This method will mutate the target. | ||
35 | + */ | ||
36 | +function tryDeconstructObject(key: string, value: any, target: Recordable) { | ||
37 | + const pattern = /^\{(.+)\}$/; | ||
38 | + if (pattern.test(key)) { | ||
39 | + const match = key.match(pattern); | ||
40 | + if (match && match[1]) { | ||
41 | + const keys = match[1].split(','); | ||
42 | + value = isObject(value) ? value : {}; | ||
43 | + keys.forEach((k) => { | ||
44 | + set(target, k.trim(), value[k.trim()]); | ||
45 | + }); | ||
46 | + return true; | ||
47 | + } | ||
48 | + } | ||
49 | +} | ||
50 | + | ||
14 | export function useFormValues({ | 51 | export function useFormValues({ |
15 | defaultValueRef, | 52 | defaultValueRef, |
16 | getSchema, | 53 | getSchema, |
@@ -41,7 +78,10 @@ export function useFormValues({ | @@ -41,7 +78,10 @@ export function useFormValues({ | ||
41 | if (isString(value)) { | 78 | if (isString(value)) { |
42 | value = value.trim(); | 79 | value = value.trim(); |
43 | } | 80 | } |
44 | - set(res, key, value); | 81 | + if (!tryDeconstructArray(key, value, res) && !tryDeconstructObject(key, value, res)) { |
82 | + // 没有解构成功的,按原样赋值 | ||
83 | + set(res, key, value); | ||
84 | + } | ||
45 | } | 85 | } |
46 | return handleRangeTimeValue(res); | 86 | return handleRangeTimeValue(res); |
47 | } | 87 | } |
src/views/demo/form/index.vue
@@ -531,6 +531,22 @@ | @@ -531,6 +531,22 @@ | ||
531 | }, | 531 | }, |
532 | }, | 532 | }, |
533 | { | 533 | { |
534 | + field: 'divider-deconstruct', | ||
535 | + component: 'Divider', | ||
536 | + label: '字段解构', | ||
537 | + helpMessage: ['如果组件的值是 array 或者 object', '可以根据 ES6 的解构语法分别取值'], | ||
538 | + }, | ||
539 | + { | ||
540 | + field: '[startTime, endTime]', | ||
541 | + label: '时间范围', | ||
542 | + component: 'RangePicker', | ||
543 | + componentProps: { | ||
544 | + format: 'YYYY-MM-DD HH:mm:ss', | ||
545 | + placeholder: ['开始时间', '结束时间'], | ||
546 | + showTime: { format: 'HH:mm:ss' }, | ||
547 | + }, | ||
548 | + }, | ||
549 | + { | ||
534 | field: 'divider-others', | 550 | field: 'divider-others', |
535 | component: 'Divider', | 551 | component: 'Divider', |
536 | label: '其它', | 552 | label: '其它', |
@@ -602,6 +618,7 @@ | @@ -602,6 +618,7 @@ | ||
602 | keyword.value = ''; | 618 | keyword.value = ''; |
603 | }, | 619 | }, |
604 | handleSubmit: (values: any) => { | 620 | handleSubmit: (values: any) => { |
621 | + console.log('values', values); | ||
605 | createMessage.success('click search,values:' + JSON.stringify(values)); | 622 | createMessage.success('click search,values:' + JSON.stringify(values)); |
606 | }, | 623 | }, |
607 | check, | 624 | check, |