Skip to content

Commit

Permalink
fix(lib): 修复 anyOf 类型,编辑时不能匹配正确选项
Browse files Browse the repository at this point in the history
fix #31
  • Loading branch information
lljj-x committed Jan 19, 2021
1 parent 0120d2b commit d747722
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 23 deletions.
12 changes: 12 additions & 0 deletions packages/demo/demo-common/schemaTypes/18.AnyOf(联动)/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export default {
'ui:showTitle': true,
title: 'First method of identification',
properties: {
type: {
'ui:hidden': true,
type: 'string',
default: 'userName',
const: 'userName'
},
firstName: {
type: 'string',
title: 'First name',
Expand All @@ -97,6 +103,12 @@ export default {
'ui:showTitle': true,
title: 'Second method of identification',
properties: {
type: {
'ui:hidden': true,
type: 'string',
default: 'id',
const: 'id'
},
firstName: {
type: 'string',
title: 'First name'
Expand Down
6 changes: 4 additions & 2 deletions packages/lib/utils/schema/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export function isValid(schema, data) {
}

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

// Remove the "required" field as it's likely that not all fields have
// been filled in yet, which will mean that the schema is not valid
delete augmentedSchema.required;

// 如果编辑回填数据的场景 可直接使用 required 判断
if (!haveAllFields) delete augmentedSchema.required;

if (isValid(augmentedSchema, formData)) {
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
},
methods: {
computedCurSelectIndexByFormData(formData) {
const index = getMatchingOption(formData, this.selectList, this.rootSchema);
const index = getMatchingOption(formData, this.selectList, this.rootSchema, true);
if (index !== 0) return index;

// 找不到默认等于原本的值
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9039,6 +9039,8 @@ function isValid(schema, data) {
} // oneOf anyOf 通过formData的值来找到当前匹配项索引

function getMatchingOption(formData, options, rootSchema) {
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

// eslint-disable-next-line no-plusplus
for (var i = 0; i < options.length; i++) {
var option = options[i]; // If the schema describes an object then we need to add slightly more
Expand Down Expand Up @@ -9078,9 +9080,10 @@ function getMatchingOption(formData, options, rootSchema) {
augmentedSchema = Object.assign({}, option, requiresAnyOf);
} // Remove the "required" field as it's likely that not all fields have
// been filled in yet, which will mean that the schema is not valid
// 如果编辑回填数据的场景 可直接使用 required 判断


delete augmentedSchema.required;
if (!haveAllFields) delete augmentedSchema.required;

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

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

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

return this.curSelectIndex || 0;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9047,6 +9047,8 @@
} // oneOf anyOf 通过formData的值来找到当前匹配项索引

function getMatchingOption(formData, options, rootSchema) {
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

// eslint-disable-next-line no-plusplus
for (var i = 0; i < options.length; i++) {
var option = options[i]; // If the schema describes an object then we need to add slightly more
Expand Down Expand Up @@ -9086,9 +9088,10 @@
augmentedSchema = Object.assign({}, option, requiresAnyOf);
} // Remove the "required" field as it's likely that not all fields have
// been filled in yet, which will mean that the schema is not valid
// 如果编辑回填数据的场景 可直接使用 required 判断


delete augmentedSchema.required;
if (!haveAllFields) delete augmentedSchema.required;

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

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

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

return this.curSelectIndex || 0;
Expand Down

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions packages/lib/vue2/vue2-form-iview3/dist/vue2FormIview3.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9039,6 +9039,8 @@ function isValid(schema, data) {
} // oneOf anyOf 通过formData的值来找到当前匹配项索引

function getMatchingOption(formData, options, rootSchema) {
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

// eslint-disable-next-line no-plusplus
for (var i = 0; i < options.length; i++) {
var option = options[i]; // If the schema describes an object then we need to add slightly more
Expand Down Expand Up @@ -9078,9 +9080,10 @@ function getMatchingOption(formData, options, rootSchema) {
augmentedSchema = Object.assign({}, option, requiresAnyOf);
} // Remove the "required" field as it's likely that not all fields have
// been filled in yet, which will mean that the schema is not valid
// 如果编辑回填数据的场景 可直接使用 required 判断


delete augmentedSchema.required;
if (!haveAllFields) delete augmentedSchema.required;

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

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

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

return this.curSelectIndex || 0;
Expand Down

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions packages/lib/vue2/vue2-form-iview3/dist/vue2FormIview3.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9047,6 +9047,8 @@
} // oneOf anyOf 通过formData的值来找到当前匹配项索引

function getMatchingOption(formData, options, rootSchema) {
var haveAllFields = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;

// eslint-disable-next-line no-plusplus
for (var i = 0; i < options.length; i++) {
var option = options[i]; // If the schema describes an object then we need to add slightly more
Expand Down Expand Up @@ -9086,9 +9088,10 @@
augmentedSchema = Object.assign({}, option, requiresAnyOf);
} // Remove the "required" field as it's likely that not all fields have
// been filled in yet, which will mean that the schema is not valid
// 如果编辑回填数据的场景 可直接使用 required 判断


delete augmentedSchema.required;
if (!haveAllFields) delete augmentedSchema.required;

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

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

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

return this.curSelectIndex || 0;
Expand Down

Large diffs are not rendered by default.

0 comments on commit d747722

Please sign in to comment.