|
1 | 1 | <script>
|
2 | 2 | import { loadFields } from './parser';
|
3 |
| - import { initChild, getChild, setVal } from './utils'; |
| 3 | + import { initChild, getChild, setVal, deepClone } from './utils'; |
4 | 4 | const option = { native: true };
|
5 | 5 | const components = {
|
6 | 6 | title: { component: 'h1', option },
|
|
70 | 70 | };
|
71 | 71 | },
|
72 | 72 | created() {
|
73 |
| - loadFields(this, JSON.parse(JSON.stringify(this.schema))); |
74 |
| - this.default = { ...this.value }; |
75 |
| - this.data = { ...this.value }; |
| 73 | + loadFields(this, deepClone(this.schema)); |
| 74 | + this.default = deepClone(this.value); |
| 75 | + this.data = deepClone(this.value); |
76 | 76 | },
|
77 | 77 | render(createElement) {
|
78 | 78 | const nodes = [];
|
|
100 | 100 | function createForm(fields, sub) {
|
101 | 101 | let node;
|
102 | 102 | if(sub) {
|
103 |
| - node = setVal(formNode, sub, []); |
| 103 | + node = setVal(formNode, sub.pop(), {}); |
104 | 104 | } else {
|
105 | 105 | node = formNode.root;
|
106 | 106 | }
|
|
114 | 114 | return createForm.call(this, field, sub ? [ ...sub, key ] : [ key ]);
|
115 | 115 | }
|
116 | 116 | const fieldName = field.name;
|
117 |
| - const fieldValue = getChild(this.value, field.name.split('.')); |
| 117 | +
|
| 118 | + const fieldValue = getChild(this.data, fieldName.split('.')); |
118 | 119 | if (!field.value) {
|
119 | 120 | field.value = fieldValue;
|
120 | 121 | }
|
|
134 | 135 | on: {
|
135 | 136 | input: (event) => {
|
136 | 137 | const value = event && event.target ? event.target.value : event;
|
137 |
| - const ns = field.name.split('.'); |
| 138 | + const ns = fieldName.split('.'); |
138 | 139 | const n = ns.pop();
|
139 | 140 | const ret = (ns.length > 0 ? initChild(this.data, ns) : this.data);
|
140 | 141 | this.$set(ret, n, value);
|
|
216 | 217 | } else {
|
217 | 218 | formControlsNodes.forEach((node) => formNodes.push(node));
|
218 | 219 | }
|
219 |
| - if(sub) { |
220 |
| - node.push(formNodes[0]); |
221 |
| - } else { |
222 |
| - node[key] = formNodes[0]; |
223 |
| - } |
| 220 | + node[key] = formNodes[0]; |
224 | 221 | });
|
225 | 222 | }
|
226 | 223 | }
|
227 | 224 | createForm.call(this, this.fields);
|
228 | 225 |
|
229 | 226 | function createNode(fields, sub) {
|
230 |
| - if(sub) { |
231 |
| - allFormNodes.push(createElement('div', { |
232 |
| - class: 'sub-' + sub.length, |
233 |
| - }, formNode[sub[sub.length - 1]])); |
234 |
| - } |
| 227 | + const nodes = []; |
| 228 | + const subName = sub && sub.pop(); |
235 | 229 | Object.keys(fields).forEach((key) => {
|
236 | 230 | if(key === '$sub') return;
|
237 | 231 | const field = fields[key];
|
238 | 232 | if(field.$sub) {
|
239 |
| - createNode.call(this, field, sub ? [ ...sub, key ] : [ key ]); |
240 |
| - } else if(formNode.root[key]) { |
241 |
| - allFormNodes.push(formNode.root[key]); |
| 233 | + const node = createNode.call(this, field, sub ? [ ...sub, key ] : [ key ]); |
| 234 | + nodes.push(createElement('div', { |
| 235 | + class: 'sub', |
| 236 | + }, node)); |
| 237 | + } else if(subName) { |
| 238 | + nodes.push(getChild(formNode, subName.split('.'))[key]); |
| 239 | + } else { |
| 240 | + nodes.push(formNode.root[key]); |
242 | 241 | }
|
243 | 242 | });
|
| 243 | + return nodes; |
244 | 244 | }
|
245 |
| - createNode.call(this, this.fields); |
| 245 | + const formNodes = createNode.call(this, this.fields); |
| 246 | + allFormNodes.push(formNodes); |
246 | 247 |
|
247 | 248 | const labelOptions = this.elementOptions(components.label);
|
248 | 249 | const button = this.$slots.hasOwnProperty('default')
|
|
342 | 343 | /**
|
343 | 344 | * Reset the value of all elements of the parent form.
|
344 | 345 | */
|
345 |
| - reset() { |
346 |
| - for (const key in this.default) { |
| 346 | + reset(e) { |
| 347 | + for (const key in this.data) { |
347 | 348 | const ns = key.split('.');
|
348 | 349 | const n = ns.pop();
|
349 | 350 | const ret = (ns.length > 0 ? initChild(this.data, ns) : this.data);
|
|
0 commit comments