|
1 | | -<template> |
2 | | - <div> |
3 | | - <component |
4 | | - :is="group.component" |
5 | | - v-for="group in config" |
6 | | - :key="group._key" |
7 | | - v-bind="group.props" |
8 | | - > |
9 | | - <component |
10 | | - v-for="comp in group.fields" |
11 | | - v-bind="comp.props" |
12 | | - v-model="formValues[comp.name]" |
13 | | - @command="handleCommand" |
14 | | - :is="comp.component" |
15 | | - :key="comp.name" |
16 | | - :tip="comp.tip" |
17 | | - :tooltip="comp.tooltip" |
18 | | - :name="comp.name" |
19 | | - :hide="comp.hide" |
20 | | - :rules="comp.rules" |
21 | | - :label="comp.label" |
22 | | - :items="comp.items" |
23 | | - :props="comp.props" |
24 | | - :extend="comp.extend" |
25 | | - :metadata="metadata" |
26 | | - :formValues="formValues" |
27 | | - > |
28 | | - </component> |
29 | | - </component> |
30 | | - </div> |
31 | | -</template> |
32 | | - |
33 | | -<script> |
34 | | -import deepmerge from 'deepmerge'; |
35 | | -
|
36 | | -const options = { |
37 | | - name: 'form-builder', |
38 | | -
|
39 | | - props: { |
40 | | - // instance of form-compostion |
41 | | - form: { |
42 | | - type: Object, |
43 | | - required: true |
44 | | - }, |
45 | | - shares: { |
46 | | - type: Object, |
47 | | - default() { |
48 | | - return {}; |
49 | | - } |
50 | | - }, |
51 | | - config: { |
52 | | - type: Array, |
53 | | - required: true |
54 | | - }, |
55 | | - metadata: { |
56 | | - type: Object, |
57 | | - default() { |
58 | | - return {}; |
59 | | - } |
60 | | - } |
61 | | - }, |
62 | | -
|
63 | | - setup(props) { |
64 | | - const { formValues, updateFormValues } = props.form; |
65 | | -
|
66 | | - return { |
67 | | - // from form composition |
68 | | - formValues, |
69 | | - updateFormValues |
70 | | - }; |
71 | | - }, |
72 | | -
|
73 | | - data() { |
74 | | - return {}; |
75 | | - }, |
76 | | -
|
77 | | - computed: {}, |
78 | | -
|
79 | | - created() { |
80 | | - this.prepare(); |
81 | | - }, |
82 | | -
|
83 | | - methods: { |
84 | | - prepare() { |
85 | | - const { config, shares, formValues } = this; |
86 | | - const initialValues = {}; |
87 | | -
|
88 | | - config.forEach((group, index) => { |
89 | | - group._key = `group_${index}`; |
90 | | - group.fields = group.fields || []; |
91 | | - group.component = group.component || 'div'; |
92 | | -
|
93 | | - group.fields = group.fields.map(comp => { |
94 | | - if (!comp.name) { |
95 | | - throw new Error( |
96 | | - `FormBuilder: name is required, config - ${JSON.stringify(comp)}` |
97 | | - ); |
98 | | - } |
99 | | -
|
100 | | - if (comp.defaultValue != null) { |
101 | | - if (formValues[comp.name] == null) { |
102 | | - initialValues[comp.name] = comp.defaultValue; |
103 | | - } |
104 | | - } |
105 | | -
|
106 | | - return deepmerge.all([{}, shares, comp]); |
107 | | - }); |
108 | | - }); |
109 | | -
|
110 | | - this.updateFormValues(initialValues); |
111 | | - }, |
112 | | -
|
113 | | - handleCommand(...args) { |
114 | | - this.$emit('command', ...args); |
115 | | - } |
116 | | - } |
117 | | -}; |
118 | | -
|
119 | | -export default options; |
120 | | -</script> |
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <component |
| 4 | + :is="group.component" |
| 5 | + v-for="group in config" |
| 6 | + :key="group._key" |
| 7 | + v-bind="group.props" |
| 8 | + > |
| 9 | + <component |
| 10 | + v-for="comp in group.fields" |
| 11 | + v-bind="comp.props" |
| 12 | + v-model="formValues[comp.name]" |
| 13 | + @command="handleCommand" |
| 14 | + :is="comp.component" |
| 15 | + :key="comp.name" |
| 16 | + :tip="comp.tip" |
| 17 | + :tooltip="comp.tooltip" |
| 18 | + :name="comp.name" |
| 19 | + :hide="comp.hide" |
| 20 | + :rules="comp.rules" |
| 21 | + :label="comp.label" |
| 22 | + :items="comp.items" |
| 23 | + :props="comp.props" |
| 24 | + :extend="comp.extend" |
| 25 | + :metadata="metadata" |
| 26 | + :formValues="formValues" |
| 27 | + > |
| 28 | + </component> |
| 29 | + </component> |
| 30 | + </div> |
| 31 | +</template> |
| 32 | + |
| 33 | +<script> |
| 34 | +import deepmerge from 'deepmerge'; |
| 35 | +
|
| 36 | +const options = { |
| 37 | + name: 'form-builder', |
| 38 | +
|
| 39 | + props: { |
| 40 | + // instance of form-compostion |
| 41 | + form: { |
| 42 | + type: Object, |
| 43 | + required: true |
| 44 | + }, |
| 45 | + shares: { |
| 46 | + type: Object, |
| 47 | + default() { |
| 48 | + return {}; |
| 49 | + } |
| 50 | + }, |
| 51 | + config: { |
| 52 | + type: Array, |
| 53 | + required: true |
| 54 | + }, |
| 55 | + metadata: { |
| 56 | + type: Object, |
| 57 | + default() { |
| 58 | + return {}; |
| 59 | + } |
| 60 | + } |
| 61 | + }, |
| 62 | +
|
| 63 | + setup(props) { |
| 64 | + const { formValues, updateFormValues } = props.form; |
| 65 | +
|
| 66 | + return { |
| 67 | + // from form composition |
| 68 | + formValues, |
| 69 | + updateFormValues |
| 70 | + }; |
| 71 | + }, |
| 72 | +
|
| 73 | + data() { |
| 74 | + return {}; |
| 75 | + }, |
| 76 | +
|
| 77 | + computed: {}, |
| 78 | +
|
| 79 | + created() { |
| 80 | + this.prepare(); |
| 81 | + }, |
| 82 | +
|
| 83 | + methods: { |
| 84 | + prepare() { |
| 85 | + const { config, shares, formValues } = this; |
| 86 | + const initialValues = {}; |
| 87 | +
|
| 88 | + config.forEach((group, index) => { |
| 89 | + group._key = `group_${index}`; |
| 90 | + group.fields = group.fields || []; |
| 91 | + group.component = group.component || 'div'; |
| 92 | +
|
| 93 | + group.fields = group.fields.map(comp => { |
| 94 | + if (!comp.name) { |
| 95 | + throw new Error( |
| 96 | + `FormBuilder: name is required, config - ${JSON.stringify(comp)}` |
| 97 | + ); |
| 98 | + } |
| 99 | +
|
| 100 | + if (comp.defaultValue != null) { |
| 101 | + if (formValues[comp.name] == null) { |
| 102 | + initialValues[comp.name] = comp.defaultValue; |
| 103 | + } |
| 104 | + } |
| 105 | +
|
| 106 | + return deepmerge.all([{}, shares, comp]); |
| 107 | + }); |
| 108 | + }); |
| 109 | +
|
| 110 | + this.updateFormValues(initialValues); |
| 111 | + }, |
| 112 | +
|
| 113 | + handleCommand(...args) { |
| 114 | + this.$emit('command', ...args); |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | +
|
| 119 | +export default options; |
| 120 | +</script> |
0 commit comments