Skip to content

Commit

Permalink
feat(lib): 优化anyOf切换选项值的复用,修复vue3 anyOf无法切换选项
Browse files Browse the repository at this point in the history
  • Loading branch information
lljj-x committed May 9, 2021
1 parent 55728bc commit 6159160
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 98 deletions.
3 changes: 2 additions & 1 deletion packages/lib/vue2/vue2-core/src/components/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ export default {
const trueValue = (value === '' || value === null) ? this.emptyValue : value;
if (this.isFormData) {
setPathVal(this.rootFormData, this.curNodePath, trueValue);
} else {
this.$emit('onOtherDataChange', trueValue);
}
this.$emit('onChange', trueValue);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ export default {
methods: {
computedCurSelectIndexByFormData(formData) {
const index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index;

// 找不到默认等于原本的值
return this.curSelectIndex || 0;
return index || 0;
},

// 下拉选项 VNode
Expand Down Expand Up @@ -101,7 +99,7 @@ export default {
...selectWidgetConfig
},
on: {
onChange: (event) => {
onOtherDataChange: (event) => {
this.curSelectIndex = event;
}
}
Expand Down Expand Up @@ -145,13 +143,20 @@ export default {
// 设置新值
if (isObject(newOptionData)) {
Object.entries(newOptionData).forEach(([key, value]) => {
if (value !== undefined) {
if (value !== undefined && (curFormData[key] === undefined || this.selectList[newVal].properties[key].const !== undefined)) {
// 这里没找到一个比较合理的新旧值合并方式
//
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
// 2. 其它场景保留同名key的旧的值
setPathVal(curFormData, key, value);
}
});
} else {
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
}

// 可添加一个配置通知外部这里变更
// todo: onChangeOption
}
},
render(h) {
Expand Down
28 changes: 17 additions & 11 deletions packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10171,9 +10171,9 @@ var Widget = {

if (this.isFormData) {
setPathVal(this.rootFormData, this.curNodePath, trueValue);
} else {
this.$emit('onOtherDataChange', trueValue);
}

this.$emit('onChange', trueValue);
}
}
},
Expand Down Expand Up @@ -11266,9 +11266,7 @@ var SelectLinkageField = {
methods: {
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index; // 找不到默认等于原本的值

return this.curSelectIndex || 0;
return index || 0;
},
// 下拉选项 VNode
getSelectBoxVNode: function getSelectBoxVNode() {
Expand Down Expand Up @@ -11321,7 +11319,7 @@ var SelectLinkageField = {
globalOptions: this.globalOptions
}, selectWidgetConfig),
on: {
onChange: function onChange(event) {
onOtherDataChange: function onOtherDataChange(event) {
_this.curSelectIndex = event;
}
}
Expand All @@ -11333,6 +11331,8 @@ var SelectLinkageField = {
// 如果object 类型 option有添加属性 这里做移除
// 对新option计算默认值
curSelectIndex: function curSelectIndex(newVal, oldVal) {
var _this2 = this;

var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值

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

if (value !== undefined) {
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
// 这里没找到一个比较合理的新旧值合并方式
//
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
// 2. 其它场景保留同名key的旧的值
setPathVal(curFormData, key, value);
}
});
} else {
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
}
} // 可添加一个配置通知外部这里变更
// todo: onChangeOption

}
},
render: function render(h) {
var _this2 = this,
var _this3 = this,
_class4;

var curNodePath = this.$props.curNodePath;
Expand Down Expand Up @@ -11418,14 +11424,14 @@ var SelectLinkageField = {
curNodePath: curNodePath,
rootFormData: this.rootFormData
}), function (key) {
return key === _this2.combiningType ? undefined : "ui:".concat(key);
return key === _this3.combiningType ? undefined : "ui:".concat(key);
});
var userErrOptions = filterObject(getUserErrOptions({
schema: this.schema,
uiSchema: this.uiSchema,
errorSchema: this.errorSchema
}), function (key) {
return key === _this2.combiningType ? undefined : "err:".concat(key);
return key === _this3.combiningType ? undefined : "err:".concat(key);
});
childrenVNodeList.push(h(SchemaField, {
key: "appendSchema_".concat(this.combiningType),
Expand Down

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10179,9 +10179,9 @@

if (this.isFormData) {
setPathVal(this.rootFormData, this.curNodePath, trueValue);
} else {
this.$emit('onOtherDataChange', trueValue);
}

this.$emit('onChange', trueValue);
}
}
},
Expand Down Expand Up @@ -11274,9 +11274,7 @@
methods: {
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index; // 找不到默认等于原本的值

return this.curSelectIndex || 0;
return index || 0;
},
// 下拉选项 VNode
getSelectBoxVNode: function getSelectBoxVNode() {
Expand Down Expand Up @@ -11329,7 +11327,7 @@
globalOptions: this.globalOptions
}, selectWidgetConfig),
on: {
onChange: function onChange(event) {
onOtherDataChange: function onOtherDataChange(event) {
_this.curSelectIndex = event;
}
}
Expand All @@ -11341,6 +11339,8 @@
// 如果object 类型 option有添加属性 这里做移除
// 对新option计算默认值
curSelectIndex: function curSelectIndex(newVal, oldVal) {
var _this2 = this;

var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值

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

if (value !== undefined) {
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
// 这里没找到一个比较合理的新旧值合并方式
//
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
// 2. 其它场景保留同名key的旧的值
setPathVal(curFormData, key, value);
}
});
} else {
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
}
} // 可添加一个配置通知外部这里变更
// todo: onChangeOption

}
},
render: function render(h) {
var _this2 = this,
var _this3 = this,
_class4;

var curNodePath = this.$props.curNodePath;
Expand Down Expand Up @@ -11426,14 +11432,14 @@
curNodePath: curNodePath,
rootFormData: this.rootFormData
}), function (key) {
return key === _this2.combiningType ? undefined : "ui:".concat(key);
return key === _this3.combiningType ? undefined : "ui:".concat(key);
});
var userErrOptions = filterObject(getUserErrOptions({
schema: this.schema,
uiSchema: this.uiSchema,
errorSchema: this.errorSchema
}), function (key) {
return key === _this2.combiningType ? undefined : "err:".concat(key);
return key === _this3.combiningType ? undefined : "err:".concat(key);
});
childrenVNodeList.push(h(SchemaField, {
key: "appendSchema_".concat(this.combiningType),
Expand Down

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10171,9 +10171,9 @@ var Widget = {

if (this.isFormData) {
setPathVal(this.rootFormData, this.curNodePath, trueValue);
} else {
this.$emit('onOtherDataChange', trueValue);
}

this.$emit('onChange', trueValue);
}
}
},
Expand Down Expand Up @@ -11266,9 +11266,7 @@ var SelectLinkageField = {
methods: {
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index; // 找不到默认等于原本的值

return this.curSelectIndex || 0;
return index || 0;
},
// 下拉选项 VNode
getSelectBoxVNode: function getSelectBoxVNode() {
Expand Down Expand Up @@ -11321,7 +11319,7 @@ var SelectLinkageField = {
globalOptions: this.globalOptions
}, selectWidgetConfig),
on: {
onChange: function onChange(event) {
onOtherDataChange: function onOtherDataChange(event) {
_this.curSelectIndex = event;
}
}
Expand All @@ -11333,6 +11331,8 @@ var SelectLinkageField = {
// 如果object 类型 option有添加属性 这里做移除
// 对新option计算默认值
curSelectIndex: function curSelectIndex(newVal, oldVal) {
var _this2 = this;

var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值

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

if (value !== undefined) {
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
// 这里没找到一个比较合理的新旧值合并方式
//
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
// 2. 其它场景保留同名key的旧的值
setPathVal(curFormData, key, value);
}
});
} else {
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
}
} // 可添加一个配置通知外部这里变更
// todo: onChangeOption

}
},
render: function render(h) {
var _this2 = this,
var _this3 = this,
_class4;

var curNodePath = this.$props.curNodePath;
Expand Down Expand Up @@ -11418,14 +11424,14 @@ var SelectLinkageField = {
curNodePath: curNodePath,
rootFormData: this.rootFormData
}), function (key) {
return key === _this2.combiningType ? undefined : "ui:".concat(key);
return key === _this3.combiningType ? undefined : "ui:".concat(key);
});
var userErrOptions = filterObject(getUserErrOptions({
schema: this.schema,
uiSchema: this.uiSchema,
errorSchema: this.errorSchema
}), function (key) {
return key === _this2.combiningType ? undefined : "err:".concat(key);
return key === _this3.combiningType ? undefined : "err:".concat(key);
});
childrenVNodeList.push(h(SchemaField, {
key: "appendSchema_".concat(this.combiningType),
Expand Down

Large diffs are not rendered by default.

28 changes: 17 additions & 11 deletions packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10179,9 +10179,9 @@

if (this.isFormData) {
setPathVal(this.rootFormData, this.curNodePath, trueValue);
} else {
this.$emit('onOtherDataChange', trueValue);
}

this.$emit('onChange', trueValue);
}
}
},
Expand Down Expand Up @@ -11274,9 +11274,7 @@
methods: {
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index; // 找不到默认等于原本的值

return this.curSelectIndex || 0;
return index || 0;
},
// 下拉选项 VNode
getSelectBoxVNode: function getSelectBoxVNode() {
Expand Down Expand Up @@ -11329,7 +11327,7 @@
globalOptions: this.globalOptions
}, selectWidgetConfig),
on: {
onChange: function onChange(event) {
onOtherDataChange: function onOtherDataChange(event) {
_this.curSelectIndex = event;
}
}
Expand All @@ -11341,6 +11339,8 @@
// 如果object 类型 option有添加属性 这里做移除
// 对新option计算默认值
curSelectIndex: function curSelectIndex(newVal, oldVal) {
var _this2 = this;

var curFormData = getPathVal$1(this.rootFormData, this.curNodePath); // 计算出 新选项默认值

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

if (value !== undefined) {
if (value !== undefined && (curFormData[key] === undefined || _this2.selectList[newVal].properties[key].const !== undefined)) {
// 这里没找到一个比较合理的新旧值合并方式
//
// 1. 如果anyOf里面同名属性中的schema包含了 const 配置,产生了新的值这里做覆盖处理
// 2. 其它场景保留同名key的旧的值
setPathVal(curFormData, key, value);
}
});
} else {
setPathVal(this.rootFormData, this.curNodePath, newOptionData || curFormData);
}
} // 可添加一个配置通知外部这里变更
// todo: onChangeOption

}
},
render: function render(h) {
var _this2 = this,
var _this3 = this,
_class4;

var curNodePath = this.$props.curNodePath;
Expand Down Expand Up @@ -11426,14 +11432,14 @@
curNodePath: curNodePath,
rootFormData: this.rootFormData
}), function (key) {
return key === _this2.combiningType ? undefined : "ui:".concat(key);
return key === _this3.combiningType ? undefined : "ui:".concat(key);
});
var userErrOptions = filterObject(getUserErrOptions({
schema: this.schema,
uiSchema: this.uiSchema,
errorSchema: this.errorSchema
}), function (key) {
return key === _this2.combiningType ? undefined : "err:".concat(key);
return key === _this3.combiningType ? undefined : "err:".concat(key);
});
childrenVNodeList.push(h(SchemaField, {
key: "appendSchema_".concat(this.combiningType),
Expand Down

0 comments on commit 6159160

Please sign in to comment.