Skip to content

Commit d747722

Browse files
committed
fix(lib): 修复 anyOf 类型,编辑时不能匹配正确选项
fix #31
1 parent 0120d2b commit d747722

File tree

11 files changed

+49
-23
lines changed

11 files changed

+49
-23
lines changed

packages/demo/demo-common/schemaTypes/18.AnyOf(联动)/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ export default {
8282
'ui:showTitle': true,
8383
title: 'First method of identification',
8484
properties: {
85+
type: {
86+
'ui:hidden': true,
87+
type: 'string',
88+
default: 'userName',
89+
const: 'userName'
90+
},
8591
firstName: {
8692
type: 'string',
8793
title: 'First name',
@@ -97,6 +103,12 @@ export default {
97103
'ui:showTitle': true,
98104
title: 'Second method of identification',
99105
properties: {
106+
type: {
107+
'ui:hidden': true,
108+
type: 'string',
109+
default: 'id',
110+
const: 'id'
111+
},
100112
firstName: {
101113
type: 'string',
102114
title: 'First name'

packages/lib/utils/schema/validate.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export function isValid(schema, data) {
242242
}
243243

244244
// oneOf anyOf 通过formData的值来找到当前匹配项索引
245-
export function getMatchingOption(formData, options, rootSchema) {
245+
export function getMatchingOption(formData, options, rootSchema, haveAllFields = false) {
246246
// eslint-disable-next-line no-plusplus
247247
for (let i = 0; i < options.length; i++) {
248248
const option = options[i];
@@ -286,7 +286,9 @@ export function getMatchingOption(formData, options, rootSchema) {
286286

287287
// Remove the "required" field as it's likely that not all fields have
288288
// been filled in yet, which will mean that the schema is not valid
289-
delete augmentedSchema.required;
289+
290+
// 如果编辑回填数据的场景 可直接使用 required 判断
291+
if (!haveAllFields) delete augmentedSchema.required;
290292

291293
if (isValid(augmentedSchema, formData)) {
292294
return i;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default {
4242
},
4343
methods: {
4444
computedCurSelectIndexByFormData(formData) {
45-
const index = getMatchingOption(formData, this.selectList, this.rootSchema);
45+
const index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
4646
if (index !== 0) return index;
4747

4848
// 找不到默认等于原本的值

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9039,6 +9039,8 @@ function isValid(schema, data) {
90399039
} // oneOf anyOf 通过formData的值来找到当前匹配项索引
90409040

90419041
function getMatchingOption(formData, options, rootSchema) {
9042+
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
9043+
90429044
// eslint-disable-next-line no-plusplus
90439045
for (var i = 0; i < options.length; i++) {
90449046
var option = options[i]; // If the schema describes an object then we need to add slightly more
@@ -9078,9 +9080,10 @@ function getMatchingOption(formData, options, rootSchema) {
90789080
augmentedSchema = Object.assign({}, option, requiresAnyOf);
90799081
} // Remove the "required" field as it's likely that not all fields have
90809082
// been filled in yet, which will mean that the schema is not valid
9083+
// 如果编辑回填数据的场景 可直接使用 required 判断
90819084

90829085

9083-
delete augmentedSchema.required;
9086+
if (!haveAllFields) delete augmentedSchema.required;
90849087

90859088
if (isValid(augmentedSchema, formData)) {
90869089
return i;
@@ -9183,7 +9186,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
91839186
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
91849187
});
91859188
} else if ('oneOf' in schema) {
9186-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf)];
9189+
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
91879190

91889191
if (schema.properties && matchSchema.properties) {
91899192
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9194,7 +9197,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
91949197
schema = matchSchema;
91959198
}
91969199
} else if ('anyOf' in schema) {
9197-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf)];
9200+
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
91989201

91999202
if (schema.properties && _matchSchema.properties) {
92009203
// 对象 anyOf 需要合并原属性和 anyOf 属性
@@ -10911,7 +10914,7 @@ var SelectLinkageField = {
1091110914
},
1091210915
methods: {
1091310916
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
10914-
var index = getMatchingOption(formData, this.selectList, this.rootSchema);
10917+
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
1091510918
if (index !== 0) return index; // 找不到默认等于原本的值
1091610919

1091710920
return this.curSelectIndex || 0;

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9047,6 +9047,8 @@
90479047
} // oneOf anyOf 通过formData的值来找到当前匹配项索引
90489048

90499049
function getMatchingOption(formData, options, rootSchema) {
9050+
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
9051+
90509052
// eslint-disable-next-line no-plusplus
90519053
for (var i = 0; i < options.length; i++) {
90529054
var option = options[i]; // If the schema describes an object then we need to add slightly more
@@ -9086,9 +9088,10 @@
90869088
augmentedSchema = Object.assign({}, option, requiresAnyOf);
90879089
} // Remove the "required" field as it's likely that not all fields have
90889090
// been filled in yet, which will mean that the schema is not valid
9091+
// 如果编辑回填数据的场景 可直接使用 required 判断
90899092

90909093

9091-
delete augmentedSchema.required;
9094+
if (!haveAllFields) delete augmentedSchema.required;
90929095

90939096
if (isValid(augmentedSchema, formData)) {
90949097
return i;
@@ -9191,7 +9194,7 @@
91919194
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
91929195
});
91939196
} else if ('oneOf' in schema) {
9194-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf)];
9197+
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
91959198

91969199
if (schema.properties && matchSchema.properties) {
91979200
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9202,7 +9205,7 @@
92029205
schema = matchSchema;
92039206
}
92049207
} else if ('anyOf' in schema) {
9205-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf)];
9208+
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
92069209

92079210
if (schema.properties && _matchSchema.properties) {
92089211
// 对象 anyOf 需要合并原属性和 anyOf 属性
@@ -10919,7 +10922,7 @@
1091910922
},
1092010923
methods: {
1092110924
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
10922-
var index = getMatchingOption(formData, this.selectList, this.rootSchema);
10925+
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
1092310926
if (index !== 0) return index; // 找不到默认等于原本的值
1092410927

1092510928
return this.curSelectIndex || 0;

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/vue2FormIview3.esm.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9039,6 +9039,8 @@ function isValid(schema, data) {
90399039
} // oneOf anyOf 通过formData的值来找到当前匹配项索引
90409040

90419041
function getMatchingOption(formData, options, rootSchema) {
9042+
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
9043+
90429044
// eslint-disable-next-line no-plusplus
90439045
for (var i = 0; i < options.length; i++) {
90449046
var option = options[i]; // If the schema describes an object then we need to add slightly more
@@ -9078,9 +9080,10 @@ function getMatchingOption(formData, options, rootSchema) {
90789080
augmentedSchema = Object.assign({}, option, requiresAnyOf);
90799081
} // Remove the "required" field as it's likely that not all fields have
90809082
// been filled in yet, which will mean that the schema is not valid
9083+
// 如果编辑回填数据的场景 可直接使用 required 判断
90819084

90829085

9083-
delete augmentedSchema.required;
9086+
if (!haveAllFields) delete augmentedSchema.required;
90849087

90859088
if (isValid(augmentedSchema, formData)) {
90869089
return i;
@@ -9183,7 +9186,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
91839186
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
91849187
});
91859188
} else if ('oneOf' in schema) {
9186-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf)];
9189+
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
91879190

91889191
if (schema.properties && matchSchema.properties) {
91899192
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9194,7 +9197,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
91949197
schema = matchSchema;
91959198
}
91969199
} else if ('anyOf' in schema) {
9197-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf)];
9200+
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
91989201

91999202
if (schema.properties && _matchSchema.properties) {
92009203
// 对象 anyOf 需要合并原属性和 anyOf 属性
@@ -10915,7 +10918,7 @@ var SelectLinkageField = {
1091510918
},
1091610919
methods: {
1091710920
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
10918-
var index = getMatchingOption(formData, this.selectList, this.rootSchema);
10921+
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
1091910922
if (index !== 0) return index; // 找不到默认等于原本的值
1092010923

1092110924
return this.curSelectIndex || 0;

packages/lib/vue2/vue2-form-iview3/dist/vue2FormIview3.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/vue2FormIview3.umd.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9047,6 +9047,8 @@
90479047
} // oneOf anyOf 通过formData的值来找到当前匹配项索引
90489048

90499049
function getMatchingOption(formData, options, rootSchema) {
9050+
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
9051+
90509052
// eslint-disable-next-line no-plusplus
90519053
for (var i = 0; i < options.length; i++) {
90529054
var option = options[i]; // If the schema describes an object then we need to add slightly more
@@ -9086,9 +9088,10 @@
90869088
augmentedSchema = Object.assign({}, option, requiresAnyOf);
90879089
} // Remove the "required" field as it's likely that not all fields have
90889090
// been filled in yet, which will mean that the schema is not valid
9091+
// 如果编辑回填数据的场景 可直接使用 required 判断
90899092

90909093

9091-
delete augmentedSchema.required;
9094+
if (!haveAllFields) delete augmentedSchema.required;
90929095

90939096
if (isValid(augmentedSchema, formData)) {
90949097
return i;
@@ -9191,7 +9194,7 @@
91919194
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
91929195
});
91939196
} else if ('oneOf' in schema) {
9194-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf)];
9197+
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
91959198

91969199
if (schema.properties && matchSchema.properties) {
91979200
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9202,7 +9205,7 @@
92029205
schema = matchSchema;
92039206
}
92049207
} else if ('anyOf' in schema) {
9205-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf)];
9208+
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
92069209

92079210
if (schema.properties && _matchSchema.properties) {
92089211
// 对象 anyOf 需要合并原属性和 anyOf 属性
@@ -10923,7 +10926,7 @@
1092310926
},
1092410927
methods: {
1092510928
computedCurSelectIndexByFormData: function computedCurSelectIndexByFormData(formData) {
10926-
var index = getMatchingOption(formData, this.selectList, this.rootSchema);
10929+
var index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
1092710930
if (index !== 0) return index; // 找不到默认等于原本的值
1092810931

1092910932
return this.curSelectIndex || 0;

0 commit comments

Comments
 (0)