Skip to content

Commit 2c2388d

Browse files
committed
feat: 优化 anyOf 切换
re 19
1 parent d7453b3 commit 2c2388d

File tree

2 files changed

+32
-19
lines changed
  • packages
    • demo/src/index/views/Demo/schemaTypes/18.AnyOf(联动)
    • lib/src/JsonSchemaForm/fields/combiningSchemas/SelectLinkageField

2 files changed

+32
-19
lines changed

packages/demo/src/index/views/Demo/schemaTypes/18.AnyOf(联动)/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export default {
9797
'ui:showTitle': true,
9898
title: 'Second method of identification',
9999
properties: {
100+
firstName: {
101+
type: 'string',
102+
title: 'First name'
103+
},
100104
idCode: {
101105
type: 'string',
102106
title: 'ID code',

packages/lib/src/JsonSchemaForm/fields/combiningSchemas/SelectLinkageField/index.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getPathVal, setPathVal, deletePathVal, nodePath2ClassName
77
} from '../../../common/vueUtils';
88
import {
9-
isEmptyObject, filterObject, guessType, isObject
9+
isEmptyObject, filterObject, isObject, getSchemaType
1010
} from '../../../common/utils';
1111

1212
import {
@@ -49,8 +49,8 @@ export default {
4949
return this.curSelectIndex || 0;
5050
},
5151

52-
// 下拉选项 Vnode
53-
getSelectBoxVnode() {
52+
// 下拉选项 VNode
53+
getSelectBoxVNode() {
5454
// 下拉选项参数
5555
const selectWidgetConfig = getWidgetConfig({
5656
schema: this.schema[`${this.combiningType}Select`] || {}, // 扩展 oneOfSelect,anyOfSelect字段
@@ -86,7 +86,7 @@ export default {
8686
}
8787

8888
// oneOf option 渲染
89-
// 选择框 vnode
89+
// 选择框 VNode
9090
return this.$createElement(
9191
Widget,
9292
{
@@ -115,17 +115,25 @@ export default {
115115
curSelectIndex(newVal, oldVal) {
116116
const curFormData = getPathVal(this.rootFormData, this.curNodePath);
117117

118+
// 计算出 新选项默认值
119+
const newOptionData = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
120+
121+
const hasOwn = Object.prototype.hasOwnProperty;
122+
118123
// 移除旧key
119124
if (isObject(curFormData)) {
120125
const oldSelectSchema = retrieveSchema(
121126
this.selectList[oldVal],
122127
this.rootSchema
123128
);
124-
if (oldSelectSchema.type === 'object' || oldSelectSchema.properties) {
129+
if (getSchemaType(oldSelectSchema) === 'object') {
125130
// 移除旧schema添加的属性
126131
// Object.keys(oldSelectSchema.properties)
127132
for (const key in oldSelectSchema.properties) {
128-
if (Object.prototype.hasOwnProperty.call(oldSelectSchema.properties, key)) {
133+
if (
134+
hasOwn.call(oldSelectSchema.properties, key)
135+
&& !hasOwn.call(newOptionData, key)
136+
) {
129137
deletePathVal(curFormData, key);
130138
// delete curFormData[key];
131139
}
@@ -134,13 +142,14 @@ export default {
134142
}
135143

136144
// 设置新值
137-
const newOptionValue = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
138-
if (guessType(newOptionValue) === 'object') {
139-
Object.entries(newOptionValue).forEach(([key, value]) => {
140-
setPathVal(curFormData, key, value);
145+
if (isObject(newOptionData)) {
146+
Object.entries(newOptionData).forEach(([key, value]) => {
147+
if (value !== undefined && curFormData[key] === undefined) {
148+
setPathVal(curFormData, key, value);
149+
}
141150
});
142151
} else {
143-
setPathVal(this.rootFormData, this.curNodePath, newOptionValue || curFormData);
152+
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
144153
}
145154
}
146155
},
@@ -149,13 +158,13 @@ export default {
149158
const pathClassName = nodePath2ClassName(curNodePath);
150159

151160
// object 需要保持原有属性,如果存在原有属性这里单独渲染
152-
let originVnode = null;
161+
let originVNode = null;
153162
const isTypeObject = (this.schema.type === 'object' || this.schema.properties);
154163
if (isTypeObject && !isEmptyObject(this.schema.properties)) {
155164
const origSchema = Object.assign({}, this.schema);
156165
delete origSchema[this.combiningType];
157166

158-
originVnode = h(SchemaField, {
167+
originVNode = h(SchemaField, {
159168
key: `origin_${this.combiningType}`,
160169
class: {
161170
[`${this.combiningType}_originBox`]: true,
@@ -170,7 +179,7 @@ export default {
170179
}
171180

172181
// 选择附加的节点
173-
const childrenVnodeList = [this.getSelectBoxVnode()];
182+
const childrenVNodeList = [this.getSelectBoxVNode()];
174183

175184
// 当前选中的 oneOf 附加的节点
176185
let curSelectSchema = this.selectList[this.curSelectIndex];
@@ -204,7 +213,7 @@ export default {
204213
errorSchema: this.errorSchema
205214
}), key => (key === this.combiningType ? undefined : `err:${key}`));
206215

207-
childrenVnodeList.push(
216+
childrenVNodeList.push(
208217
h(
209218
SchemaField,
210219
{
@@ -232,8 +241,8 @@ export default {
232241
);
233242
}
234243

235-
// oneOf 校验 vnode
236-
childrenVnodeList.push(
244+
// oneOf 校验 VNode
245+
childrenVNodeList.push(
237246
h(Widget, {
238247
class: {
239248
validateWidget: true,
@@ -250,15 +259,15 @@ export default {
250259
);
251260

252261
return h('div', [
253-
originVnode,
262+
originVNode,
254263
h('div', {
255264
key: `appendBox_${this.combiningType}`,
256265
class: {
257266
appendCombining_box: true,
258267
[`${this.combiningType}_appendBox`]: true,
259268
[`${pathClassName}-appendBox`]: true
260269
}
261-
}, childrenVnodeList)
270+
}, childrenVNodeList)
262271
]);
263272
}
264273
};

0 commit comments

Comments
 (0)