Skip to content

Commit f5ad8b5

Browse files
committed
feat: sub object render as sub div
1 parent c77c58a commit f5ad8b5

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

example/components/Subscription.vue

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<el-card class="form">
55
<json-editor ref="JsonEditor" :schema="schema" v-model="model">
66
<el-button type="primary" @click="submit">Subscribe</el-button>
7-
<el-button type="reset">Reset</el-button>
7+
<el-button type="reset" @click="reset">Reset</el-button>
88
</json-editor>
99
</el-card>
1010
</el-col>
@@ -112,13 +112,11 @@
112112
},
113113
methods: {
114114
submit(_e) {
115-
// this.$refs.JsonEditor.form() returns the ElementUI's form instance
116-
117115
this.$refs.JsonEditor.form().validate((valid) => {
118116
if (valid) {
119117
// this.model contains the valid data according your JSON Schema.
120118
// You can submit your model to the server here
121-
119+
122120
// eslint-disable-next-line no-console
123121
console.log('model', JSON.stringify(this.model));
124122
this.$refs.JsonEditor.clearErrorMessage();
@@ -128,6 +126,9 @@
128126
}
129127
});
130128
},
129+
reset() {
130+
this.$refs.JsonEditor.reset();
131+
},
131132
},
132133
components: { JsonEditor },
133134
};
@@ -162,14 +163,10 @@
162163
margin-bottom: 15px
163164
}
164165
165-
.el-form .sub-1 {
166+
.el-form .sub {
166167
margin-left: 10%;
167168
}
168169
169-
.el-form .sub-2 {
170-
margin-left: 20%;
171-
}
172-
173170
.json {
174171
text-align: left;
175172
}

example/schema/newsletter.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "http://json-schema.org/draft-04/schema#",
2+
"$schema": "http://json-schema.org/draft-07/schema#",
33
"type": "object",
44
"title": "Newsletter Subscription",
55
"description": "Sign up for free newsletters and get more delivered to your inbox",

src/JsonEditor.vue

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
import { loadFields } from './parser';
3-
import { initChild, getChild, setVal } from './utils';
3+
import { initChild, getChild, setVal, deepClone } from './utils';
44
const option = { native: true };
55
const components = {
66
title: { component: 'h1', option },
@@ -70,9 +70,9 @@
7070
};
7171
},
7272
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);
7676
},
7777
render(createElement) {
7878
const nodes = [];
@@ -100,7 +100,7 @@
100100
function createForm(fields, sub) {
101101
let node;
102102
if(sub) {
103-
node = setVal(formNode, sub, []);
103+
node = setVal(formNode, sub.pop(), {});
104104
} else {
105105
node = formNode.root;
106106
}
@@ -114,7 +114,8 @@
114114
return createForm.call(this, field, sub ? [ ...sub, key ] : [ key ]);
115115
}
116116
const fieldName = field.name;
117-
const fieldValue = getChild(this.value, field.name.split('.'));
117+
118+
const fieldValue = getChild(this.data, fieldName.split('.'));
118119
if (!field.value) {
119120
field.value = fieldValue;
120121
}
@@ -134,7 +135,7 @@
134135
on: {
135136
input: (event) => {
136137
const value = event && event.target ? event.target.value : event;
137-
const ns = field.name.split('.');
138+
const ns = fieldName.split('.');
138139
const n = ns.pop();
139140
const ret = (ns.length > 0 ? initChild(this.data, ns) : this.data);
140141
this.$set(ret, n, value);
@@ -216,33 +217,33 @@
216217
} else {
217218
formControlsNodes.forEach((node) => formNodes.push(node));
218219
}
219-
if(sub) {
220-
node.push(formNodes[0]);
221-
} else {
222-
node[key] = formNodes[0];
223-
}
220+
node[key] = formNodes[0];
224221
});
225222
}
226223
}
227224
createForm.call(this, this.fields);
228225
229226
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();
235229
Object.keys(fields).forEach((key) => {
236230
if(key === '$sub') return;
237231
const field = fields[key];
238232
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]);
242241
}
243242
});
243+
return nodes;
244244
}
245-
createNode.call(this, this.fields);
245+
const formNodes = createNode.call(this, this.fields);
246+
allFormNodes.push(formNodes);
246247
247248
const labelOptions = this.elementOptions(components.label);
248249
const button = this.$slots.hasOwnProperty('default')
@@ -342,8 +343,8 @@
342343
/**
343344
* Reset the value of all elements of the parent form.
344345
*/
345-
reset() {
346-
for (const key in this.default) {
346+
reset(e) {
347+
for (const key in this.data) {
347348
const ns = key.split('.');
348349
const n = ns.pop();
349350
const ret = (ns.length > 0 ? initChild(this.data, ns) : this.data);

src/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export function deepClone(obj) {
2+
return JSON.parse(JSON.stringify(obj));
3+
}
4+
15
export function getExtendibleLeaf(obj, n, initIt) {
26
const v = obj[n];
37
if (v && typeof v === 'object' && !Array.isArray(v)) {

0 commit comments

Comments
 (0)