Skip to content

Commit 9852c1f

Browse files
authored
Merge pull request #159 from lljj-x/feat/vue-cdn
Feat/vue cdn
2 parents e93294f + 1725f20 commit 9852c1f

27 files changed

+397
-142
lines changed

packages/demo/demo-v2/src/pages/index/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
<body>
1717
<div id="app"></div>
1818
<script src="//cdn.jsdelivr.net/npm/@lljj/[email protected]/dist/polyfill.umd.min.js"></script>
19-
<script src="//cdn.bootcss.com/vue/2.6.10/vue.js"></script>
19+
<script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
2020
<script src="//cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
21-
<script src="//cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js"></script>
21+
<script src="//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js"></script>
2222
<script src="//cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script>
2323
<script>
2424
require.config({ paths: { 'vs': '//cdn.jsdelivr.net/npm/[email protected]/min/vs' }});

packages/demo/demo-v2/src/pages/schema-generator/schema-generator.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<body>
1111
<div id="app"></div>
1212
<script src="//cdn.jsdelivr.net/npm/@lljj/[email protected]/dist/polyfill.umd.min.js"></script>
13-
<script src="//cdn.bootcss.com/vue/2.6.10/vue.js"></script>
14-
<script src="//cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js"></script>
13+
<script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
14+
<script src="//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js"></script>
1515
<script src="//cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
1616
</body>
1717
</html>

packages/demo/demo-v2/src/pages/vue-editor/views/editor/Editor.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
2+
<TestDemo v-if="false"></TestDemo>
23
<div
4+
v-else
35
v-loading="loading"
46
:class="{
57
[$style.previewBox]: isPreview
@@ -136,6 +138,7 @@ import * as arrayMethods from 'demo-common/utils/array';
136138
import componentWithDialog from 'demo-common/components/component-with-dialog';
137139
138140
import JsonPerttyPrint from 'demo-common/components/JsonPerttyPrint.vue';
141+
import TestDemo from './TestDemo';
139142
import EditorToolBar from './EditorToolBar.vue';
140143
import EditorHeader from './EditorHeader.vue';
141144
import ViewComponentWrap from './components/ViewComponentWrap.vue';
@@ -157,6 +160,7 @@ export default {
157160
name: 'Editor',
158161
components: {
159162
...components,
163+
TestDemo,
160164
VueElementForm,
161165
Draggable,
162166
EditorToolBar,
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<template>
2+
<div
3+
v-if="true"
4+
class="demo-wrapper"
5+
>
6+
<VueElementForm
7+
v-model="formData"
8+
strict-mode
9+
class="demo-form-box"
10+
:schema="schema"
11+
@on-submit="handleSubmit"
12+
>
13+
</VueElementForm>
14+
</div>
15+
</template>
16+
17+
<script>
18+
import VueElementForm from '@lljj/vue-json-schema-form/src/index';
19+
20+
export default {
21+
components: {
22+
VueElementForm
23+
},
24+
data() {
25+
return {
26+
formData: {
27+
item: [
28+
{
29+
text: '作者'
30+
},
31+
{
32+
imgUrl: '',
33+
step: 1,
34+
text: '机构'
35+
}
36+
]
37+
},
38+
schema: {
39+
title: 'DEMO',
40+
type: 'object',
41+
properties: {
42+
item: {
43+
title: '左侧点击栏目',
44+
type: 'array',
45+
required: [],
46+
minItems: 1,
47+
items: {
48+
title: '栏目类型',
49+
type: 'object',
50+
anyOf: [
51+
{
52+
title: '唯一可点击栏目',
53+
required: ['text', 'imgUrl', 'step'],
54+
properties: {
55+
text: {
56+
type: 'string',
57+
title: '栏目名称'
58+
},
59+
imgUrl: {
60+
title: '点击弹出图',
61+
type: 'string',
62+
},
63+
step: {
64+
title: '第一步',
65+
type: 'number',
66+
const: 1,
67+
default: 1,
68+
'ui:disabled': true,
69+
'ui:hidden': true
70+
}
71+
}
72+
},
73+
{
74+
title: '其他栏目',
75+
required: ['text'],
76+
properties: {
77+
text: {
78+
type: 'string',
79+
title: '栏目名称'
80+
}
81+
}
82+
}
83+
]
84+
}
85+
}
86+
}
87+
},
88+
};
89+
},
90+
created() {
91+
// 初始数据
92+
// this.formData = {
93+
// item: [
94+
// {
95+
// text: '作者'
96+
// },
97+
// {
98+
// imgUrl: '',
99+
// step: 1,
100+
// text: '机构'
101+
// }
102+
// ]
103+
// };
104+
},
105+
methods: {
106+
handleSubmit(formData) {
107+
debugger;
108+
}
109+
}
110+
};
111+
</script>
112+
113+
114+
<style scoped>
115+
.demo-wrapper {
116+
z-index: 1000;
117+
position: fixed;
118+
width: 100%;
119+
height: 100%;
120+
background: #FFFFFF;
121+
}
122+
.demo-form-box {
123+
margin: 0 auto;
124+
width: 600px;
125+
}
126+
</style>

packages/demo/demo-v2/src/pages/vue-editor/vue-editor.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<body>
1111
<div id="app"></div>
1212
<script src="//cdn.jsdelivr.net/npm/@lljj/[email protected]/dist/polyfill.umd.min.js"></script>
13-
<script src="//cdn.bootcss.com/vue/2.6.10/vue.js"></script>
14-
<script src="//cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js"></script>
13+
<script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
14+
<script src="//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js"></script>
1515
<script src="//cdn.jsdelivr.net/npm/[email protected]/lib/index.js"></script>
1616
</body>
1717
</html>

packages/docs/docs/zh/guide/basic-config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,13 @@ formProps = {
791791
}
792792
```
793793

794+
### strict-mode
795+
严格模式,开启后计算表单默认值时会对 `anyOf`/`onyOf` 做严格匹配。参见 issue: [#157](https://github.com/lljj-x/vue-json-schema-form/issues/157)
796+
797+
* 类型:`boolean`
798+
* default:`false`
799+
800+
794801
## 事件 Emit Event
795802
emit所有事件如下:
796803

packages/lib/utils/schema/getDefaultFormState.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function computeDefaults(
4949
parentDefaults,
5050
rootSchema,
5151
rawFormData = {},
52-
includeUndefinedValues = false
52+
includeUndefinedValues = false,
53+
haveAllFields = false
5354
) {
5455
let schema = isObject(_schema) ? _schema : {};
5556
const formData = isObject(rawFormData) ? rawFormData : {};
@@ -76,7 +77,8 @@ function computeDefaults(
7677
defaults,
7778
rootSchema,
7879
formData,
79-
includeUndefinedValues
80+
includeUndefinedValues,
81+
haveAllFields
8082
);
8183
} else if /* ('dependencies' in schema) {
8284
const resolvedSchema = resolveDependencies(schema, rootSchema, formData);
@@ -85,19 +87,21 @@ function computeDefaults(
8587
defaults,
8688
rootSchema,
8789
formData,
88-
includeUndefinedValues
90+
includeUndefinedValues,
91+
haveAllFields
8992
);
9093
} else if */ (isFixedItems(schema)) {
9194
defaults = schema.items.map((itemSchema, idx) => computeDefaults(
9295
itemSchema,
9396
Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined,
9497
rootSchema,
9598
formData,
96-
includeUndefinedValues
99+
includeUndefinedValues,
100+
haveAllFields
97101
));
98102
} else if ('oneOf' in schema) {
99103
const matchSchema = retrieveSchema(
100-
schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)],
104+
schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema, haveAllFields)],
101105
rootSchema,
102106
formData
103107
);
@@ -115,7 +119,7 @@ function computeDefaults(
115119
// }
116120
} else if ('anyOf' in schema) {
117121
const matchSchema = retrieveSchema(
118-
schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)],
122+
schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema, haveAllFields)],
119123
rootSchema,
120124
formData
121125
);
@@ -151,7 +155,8 @@ function computeDefaults(
151155
(defaults || {})[key],
152156
rootSchema,
153157
(formData || {})[key],
154-
includeUndefinedValues
158+
includeUndefinedValues,
159+
haveAllFields
155160
);
156161
if (includeUndefinedValues || computedDefault !== undefined) {
157162
acc[key] = computedDefault;
@@ -167,7 +172,8 @@ function computeDefaults(
167172
item,
168173
rootSchema,
169174
{},
170-
includeUndefinedValues
175+
includeUndefinedValues,
176+
haveAllFields
171177
));
172178
}
173179

@@ -179,7 +185,8 @@ function computeDefaults(
179185
rootSchema,
180186
item,
181187
{},
182-
includeUndefinedValues
188+
includeUndefinedValues,
189+
haveAllFields
183190
));
184191
}
185192
if (schema.minItems) {
@@ -194,7 +201,7 @@ function computeDefaults(
194201

195202
const fillerEntries = fillObj(
196203
new Array(schema.minItems - defaultsLength), computeDefaults(
197-
fillerSchema, fillerSchema.defaults, rootSchema, {}, includeUndefinedValues
204+
fillerSchema, fillerSchema.defaults, rootSchema, {}, includeUndefinedValues, haveAllFields
198205
)
199206
);
200207
return defaultEntries.concat(fillerEntries);
@@ -216,7 +223,8 @@ export default function getDefaultFormState(
216223
_schema,
217224
formData,
218225
rootSchema = {},
219-
includeUndefinedValues = true
226+
includeUndefinedValues = true,
227+
haveAllFields = false
220228
) {
221229
if (!isObject(_schema)) {
222230
throw new Error(`Invalid schema: ${_schema}`);
@@ -228,7 +236,8 @@ export default function getDefaultFormState(
228236
_schema.default,
229237
rootSchema,
230238
formData,
231-
includeUndefinedValues
239+
includeUndefinedValues,
240+
haveAllFields
232241
);
233242

234243
if (typeof formData === 'undefined') {

packages/lib/vue2/vue2-core/src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default function createForm(globalOptions = {}) {
4040
};
4141
},
4242
data() {
43-
const formData = getDefaultFormState(this.$props.schema, this.$props.value, this.$props.schema);
43+
const formData = getDefaultFormState(this.$props.schema, this.$props.value, this.$props.schema, this.$props.strictMode);
4444

4545
// 保持v-model双向数据及时性
4646
this.emitFormDataChange(formData, this.value);
@@ -96,7 +96,7 @@ export default function createForm(globalOptions = {}) {
9696
// 避免用于双向绑定v-model 可能导致的循环调用
9797
willReceiveProps(newVal, oldVal) {
9898
if (!deepEquals(newVal, oldVal)) {
99-
const formData = getDefaultFormState(this.$props.schema, this.$props.value, this.$props.schema);
99+
const formData = getDefaultFormState(this.$props.schema, this.$props.value, this.$props.schema, this.$props.strictMode);
100100
if (!deepEquals(this.formData, formData)) {
101101
this.formData = formData;
102102
}
@@ -176,6 +176,11 @@ export default function createForm(globalOptions = {}) {
176176
layoutColumn: !inline,
177177
[`layoutColumn-${layoutColumn}`]: !inline
178178
},
179+
nativeOn: {
180+
submit(e) {
181+
e.preventDefault();
182+
}
183+
},
179184
ref: 'genEditForm',
180185
props: {
181186
model: self.formData,

packages/lib/vue2/vue2-core/src/props.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export default {
2424
type: Boolean,
2525
default: false,
2626
},
27+
strictMode: {
28+
type: Boolean,
29+
default: false,
30+
},
2731
schema: {
2832
type: Object,
2933
default: () => ({}),

0 commit comments

Comments
 (0)