Skip to content

Commit 6159160

Browse files
committed
feat(lib): 优化anyOf切换选项值的复用,修复vue3 anyOf无法切换选项
1 parent 55728bc commit 6159160

File tree

20 files changed

+138
-98
lines changed

20 files changed

+138
-98
lines changed

packages/lib/vue2/vue2-core/src/components/Widget.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ export default {
145145
const trueValue = (value === '' || value === null) ? this.emptyValue : value;
146146
if (this.isFormData) {
147147
setPathVal(this.rootFormData, this.curNodePath, trueValue);
148+
} else {
149+
this.$emit('onOtherDataChange', trueValue);
148150
}
149-
this.$emit('onChange', trueValue);
150151
}
151152
}
152153
},

packages/lib/vue2/vue2-core/src/fields/combiningSchemas/SelectLinkageField/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ export default {
4343
methods: {
4444
computedCurSelectIndexByFormData(formData) {
4545
const index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
46-
if (index !== 0) return index;
4746

48-
// 找不到默认等于原本的值
49-
return this.curSelectIndex || 0;
47+
return index || 0;
5048
},
5149

5250
// 下拉选项 VNode
@@ -101,7 +99,7 @@ export default {
10199
...selectWidgetConfig
102100
},
103101
on: {
104-
onChange: (event) => {
102+
onOtherDataChange: (event) => {
105103
this.curSelectIndex = event;
106104
}
107105
}
@@ -145,13 +143,20 @@ export default {
145143
// 设置新值
146144
if (isObject(newOptionData)) {
147145
Object.entries(newOptionData).forEach(([key, value]) => {
148-
if (value !== undefined) {
146+
if (value !== undefined && (curFormData[key] === undefined || this.selectList[newVal].properties[key].const !== undefined)) {
147+
// 这里没找到一个比较合理的新旧值合并方式
148+
//
149+
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
150+
// 2. 其它场景保留同名key的旧的值
149151
setPathVal(curFormData, key, value);
150152
}
151153
});
152154
} else {
153155
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
154156
}
157+
158+
// 可添加一个配置通知外部这里变更
159+
// todo: onChangeOption
155160
}
156161
},
157162
render(h) {

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.esm.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10171,9 +10171,9 @@ var Widget = {
1017110171

1017210172
if (this.isFormData) {
1017310173
setPathVal(this.rootFormData, this.curNodePath, trueValue);
10174+
} else {
10175+
this.$emit('onOtherDataChange', trueValue);
1017410176
}
10175-
10176-
this.$emit('onChange', trueValue);
1017710177
}
1017810178
}
1017910179
},
@@ -11266,9 +11266,7 @@ var SelectLinkageField = {
1126611266
methods: {
1126711267
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
1126811268
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
11269-
if (index !== 0) return index; // 找不到默认等于原本的值
11270-
11271-
return this.curSelectIndex || 0;
11269+
return index || 0;
1127211270
},
1127311271
// 下拉选项 VNode
1127411272
getSelectBoxVNode: function getSelectBoxVNode() {
@@ -11321,7 +11319,7 @@ var SelectLinkageField = {
1132111319
globalOptions: this.globalOptions
1132211320
}, selectWidgetConfig),
1132311321
on: {
11324-
onChange: function onChange(event) {
11322+
onOtherDataChange: function onOtherDataChange(event) {
1132511323
_this.curSelectIndex = event;
1132611324
}
1132711325
}
@@ -11333,6 +11331,8 @@ var SelectLinkageField = {
1133311331
// 如果object 类型 option有添加属性 这里做移除
1133411332
// 对新option计算默认值
1133511333
curSelectIndex: function curSelectIndex(newVal, oldVal) {
11334+
var _this2 = this;
11335+
1133611336
var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值
1133711337

1133811338
var newOptionData = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
@@ -11359,17 +11359,23 @@ var SelectLinkageField = {
1135911359
key = _ref2[0],
1136011360
value = _ref2[1];
1136111361

11362-
if (value !== undefined) {
11362+
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
11363+
// 这里没找到一个比较合理的新旧值合并方式
11364+
//
11365+
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
11366+
// 2. 其它场景保留同名key的旧的值
1136311367
setPathVal(curFormData, key, value);
1136411368
}
1136511369
});
1136611370
} else {
1136711371
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
11368-
}
11372+
} // 可添加一个配置通知外部这里变更
11373+
// todo: onChangeOption
11374+
1136911375
}
1137011376
},
1137111377
render: function render(h) {
11372-
var _this2 = this,
11378+
var _this3 = this,
1137311379
_class4;
1137411380

1137511381
var curNodePath = this.$props.curNodePath;
@@ -11418,14 +11424,14 @@ var SelectLinkageField = {
1141811424
curNodePath: curNodePath,
1141911425
rootFormData: this.rootFormData
1142011426
}), function (key) {
11421-
return key === _this2.combiningType ? undefined : "ui:".concat(key);
11427+
return key === _this3.combiningType ? undefined : "ui:".concat(key);
1142211428
});
1142311429
var userErrOptions = filterObject(getUserErrOptions({
1142411430
schema: this.schema,
1142511431
uiSchema: this.uiSchema,
1142611432
errorSchema: this.errorSchema
1142711433
}), function (key) {
11428-
return key === _this2.combiningType ? undefined : "err:".concat(key);
11434+
return key === _this3.combiningType ? undefined : "err:".concat(key);
1142911435
});
1143011436
childrenVNodeList.push(h(SchemaField, {
1143111437
key: "appendSchema_".concat(this.combiningType),

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.umd.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10179,9 +10179,9 @@
1017910179

1018010180
if (this.isFormData) {
1018110181
setPathVal(this.rootFormData, this.curNodePath, trueValue);
10182+
} else {
10183+
this.$emit('onOtherDataChange', trueValue);
1018210184
}
10183-
10184-
this.$emit('onChange', trueValue);
1018510185
}
1018610186
}
1018710187
},
@@ -11274,9 +11274,7 @@
1127411274
methods: {
1127511275
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
1127611276
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
11277-
if (index !== 0) return index; // 找不到默认等于原本的值
11278-
11279-
return this.curSelectIndex || 0;
11277+
return index || 0;
1128011278
},
1128111279
// 下拉选项 VNode
1128211280
getSelectBoxVNode: function getSelectBoxVNode() {
@@ -11329,7 +11327,7 @@
1132911327
globalOptions: this.globalOptions
1133011328
}, selectWidgetConfig),
1133111329
on: {
11332-
onChange: function onChange(event) {
11330+
onOtherDataChange: function onOtherDataChange(event) {
1133311331
_this.curSelectIndex = event;
1133411332
}
1133511333
}
@@ -11341,6 +11339,8 @@
1134111339
// 如果object 类型 option有添加属性 这里做移除
1134211340
// 对新option计算默认值
1134311341
curSelectIndex: function curSelectIndex(newVal, oldVal) {
11342+
var _this2 = this;
11343+
1134411344
var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值
1134511345

1134611346
var newOptionData = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
@@ -11367,17 +11367,23 @@
1136711367
key = _ref2[0],
1136811368
value = _ref2[1];
1136911369

11370-
if (value !== undefined) {
11370+
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
11371+
// 这里没找到一个比较合理的新旧值合并方式
11372+
//
11373+
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
11374+
// 2. 其它场景保留同名key的旧的值
1137111375
setPathVal(curFormData, key, value);
1137211376
}
1137311377
});
1137411378
} else {
1137511379
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
11376-
}
11380+
} // 可添加一个配置通知外部这里变更
11381+
// todo: onChangeOption
11382+
1137711383
}
1137811384
},
1137911385
render: function render(h) {
11380-
var _this2 = this,
11386+
var _this3 = this,
1138111387
_class4;
1138211388

1138311389
var curNodePath = this.$props.curNodePath;
@@ -11426,14 +11432,14 @@
1142611432
curNodePath: curNodePath,
1142711433
rootFormData: this.rootFormData
1142811434
}), function (key) {
11429-
return key === _this2.combiningType ? undefined : "ui:".concat(key);
11435+
return key === _this3.combiningType ? undefined : "ui:".concat(key);
1143011436
});
1143111437
var userErrOptions = filterObject(getUserErrOptions({
1143211438
schema: this.schema,
1143311439
uiSchema: this.uiSchema,
1143411440
errorSchema: this.errorSchema
1143511441
}), function (key) {
11436-
return key === _this2.combiningType ? undefined : "err:".concat(key);
11442+
return key === _this3.combiningType ? undefined : "err:".concat(key);
1143711443
});
1143811444
childrenVNodeList.push(h(SchemaField, {
1143911445
key: "appendSchema_".concat(this.combiningType),

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.esm.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10171,9 +10171,9 @@ var Widget = {
1017110171

1017210172
if (this.isFormData) {
1017310173
setPathVal(this.rootFormData, this.curNodePath, trueValue);
10174+
} else {
10175+
this.$emit('onOtherDataChange', trueValue);
1017410176
}
10175-
10176-
this.$emit('onChange', trueValue);
1017710177
}
1017810178
}
1017910179
},
@@ -11266,9 +11266,7 @@ var SelectLinkageField = {
1126611266
methods: {
1126711267
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
1126811268
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
11269-
if (index !== 0) return index; // 找不到默认等于原本的值
11270-
11271-
return this.curSelectIndex || 0;
11269+
return index || 0;
1127211270
},
1127311271
// 下拉选项 VNode
1127411272
getSelectBoxVNode: function getSelectBoxVNode() {
@@ -11321,7 +11319,7 @@ var SelectLinkageField = {
1132111319
globalOptions: this.globalOptions
1132211320
}, selectWidgetConfig),
1132311321
on: {
11324-
onChange: function onChange(event) {
11322+
onOtherDataChange: function onOtherDataChange(event) {
1132511323
_this.curSelectIndex = event;
1132611324
}
1132711325
}
@@ -11333,6 +11331,8 @@ var SelectLinkageField = {
1133311331
// 如果object 类型 option有添加属性 这里做移除
1133411332
// 对新option计算默认值
1133511333
curSelectIndex: function curSelectIndex(newVal, oldVal) {
11334+
var _this2 = this;
11335+
1133611336
var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值
1133711337

1133811338
var newOptionData = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
@@ -11359,17 +11359,23 @@ var SelectLinkageField = {
1135911359
key = _ref2[0],
1136011360
value = _ref2[1];
1136111361

11362-
if (value !== undefined) {
11362+
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
11363+
// 这里没找到一个比较合理的新旧值合并方式
11364+
//
11365+
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
11366+
// 2. 其它场景保留同名key的旧的值
1136311367
setPathVal(curFormData, key, value);
1136411368
}
1136511369
});
1136611370
} else {
1136711371
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
11368-
}
11372+
} // 可添加一个配置通知外部这里变更
11373+
// todo: onChangeOption
11374+
1136911375
}
1137011376
},
1137111377
render: function render(h) {
11372-
var _this2 = this,
11378+
var _this3 = this,
1137311379
_class4;
1137411380

1137511381
var curNodePath = this.$props.curNodePath;
@@ -11418,14 +11424,14 @@ var SelectLinkageField = {
1141811424
curNodePath: curNodePath,
1141911425
rootFormData: this.rootFormData
1142011426
}), function (key) {
11421-
return key === _this2.combiningType ? undefined : "ui:".concat(key);
11427+
return key === _this3.combiningType ? undefined : "ui:".concat(key);
1142211428
});
1142311429
var userErrOptions = filterObject(getUserErrOptions({
1142411430
schema: this.schema,
1142511431
uiSchema: this.uiSchema,
1142611432
errorSchema: this.errorSchema
1142711433
}), function (key) {
11428-
return key === _this2.combiningType ? undefined : "err:".concat(key);
11434+
return key === _this3.combiningType ? undefined : "err:".concat(key);
1142911435
});
1143011436
childrenVNodeList.push(h(SchemaField, {
1143111437
key: "appendSchema_".concat(this.combiningType),

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.umd.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10179,9 +10179,9 @@
1017910179

1018010180
if (this.isFormData) {
1018110181
setPathVal(this.rootFormData, this.curNodePath, trueValue);
10182+
} else {
10183+
this.$emit('onOtherDataChange', trueValue);
1018210184
}
10183-
10184-
this.$emit('onChange', trueValue);
1018510185
}
1018610186
}
1018710187
},
@@ -11274,9 +11274,7 @@
1127411274
methods: {
1127511275
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
1127611276
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
11277-
if (index !== 0) return index; // 找不到默认等于原本的值
11278-
11279-
return this.curSelectIndex || 0;
11277+
return index || 0;
1128011278
},
1128111279
// 下拉选项 VNode
1128211280
getSelectBoxVNode: function getSelectBoxVNode() {
@@ -11329,7 +11327,7 @@
1132911327
globalOptions: this.globalOptions
1133011328
}, selectWidgetConfig),
1133111329
on: {
11332-
onChange: function onChange(event) {
11330+
onOtherDataChange: function onOtherDataChange(event) {
1133311331
_this.curSelectIndex = event;
1133411332
}
1133511333
}
@@ -11341,6 +11339,8 @@
1134111339
// 如果object 类型 option有添加属性 这里做移除
1134211340
// 对新option计算默认值
1134311341
curSelectIndex: function curSelectIndex(newVal, oldVal) {
11342+
var _this2 = this;
11343+
1134411344
var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值
1134511345

1134611346
var newOptionData = getDefaultFormState(this.selectList[newVal], undefined, this.rootSchema);
@@ -11367,17 +11367,23 @@
1136711367
key = _ref2[0],
1136811368
value = _ref2[1];
1136911369

11370-
if (value !== undefined) {
11370+
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
11371+
// 这里没找到一个比较合理的新旧值合并方式
11372+
//
11373+
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
11374+
// 2. 其它场景保留同名key的旧的值
1137111375
setPathVal(curFormData, key, value);
1137211376
}
1137311377
});
1137411378
} else {
1137511379
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
11376-
}
11380+
} // 可添加一个配置通知外部这里变更
11381+
// todo: onChangeOption
11382+
1137711383
}
1137811384
},
1137911385
render: function render(h) {
11380-
var _this2 = this,
11386+
var _this3 = this,
1138111387
_class4;
1138211388

1138311389
var curNodePath = this.$props.curNodePath;
@@ -11426,14 +11432,14 @@
1142611432
curNodePath: curNodePath,
1142711433
rootFormData: this.rootFormData
1142811434
}), function (key) {
11429-
return key === _this2.combiningType ? undefined : "ui:".concat(key);
11435+
return key === _this3.combiningType ? undefined : "ui:".concat(key);
1143011436
});
1143111437
var userErrOptions = filterObject(getUserErrOptions({
1143211438
schema: this.schema,
1143311439
uiSchema: this.uiSchema,
1143411440
errorSchema: this.errorSchema
1143511441
}), function (key) {
11436-
return key === _this2.combiningType ? undefined : "err:".concat(key);
11442+
return key === _this3.combiningType ? undefined : "err:".concat(key);
1143711443
});
1143811444
childrenVNodeList.push(h(SchemaField, {
1143911445
key: "appendSchema_".concat(this.combiningType),

0 commit comments

Comments
 (0)