Options 中允许值相同

This commit is contained in:
2betop 2020-05-27 16:55:23 +08:00
parent a7c568b605
commit 5c59cce7bd
2 changed files with 75 additions and 22 deletions

View File

@ -349,7 +349,7 @@ export default {
children: [
{
label: '诸葛亮',
value: 'zhugeliang2'
value: 'zhugeliang'
}
]
},
@ -358,11 +358,11 @@ export default {
children: [
{
label: '曹操',
value: 'caocao2'
value: 'caocao'
},
{
label: '钟无艳',
value: 'zhongwuyan2'
value: 'zhongwuyan'
}
]
},
@ -371,15 +371,15 @@ export default {
children: [
{
label: '李白',
value: 'libai2'
value: 'libai'
},
{
label: '韩信',
value: 'hanxin2'
value: 'hanxin'
},
{
label: '云中君',
value: 'yunzhongjun2'
value: 'yunzhongjun'
}
]
}

View File

@ -150,39 +150,92 @@ export function optionValueCompare(
}
export function normalizeOptions(
options: string | {[propName: string]: string} | Array<string> | Options
options: string | {[propName: string]: string} | Array<string> | Options,
share: {
values: Array<any>;
options: Array<any>;
} = {
values: [],
options: []
}
): Options {
if (typeof options === 'string') {
return options.split(',').map(item => ({
label: item,
value: item
}));
return options.split(',').map(item => {
const idx = share.values.indexOf(item);
if (~idx) {
return share.options[idx];
}
const option = {
label: item,
value: item
};
share.values.push(option.value);
share.options.push(option);
return option;
});
} else if (
Array.isArray(options as Array<string>) &&
typeof (options as Array<string>)[0] === 'string'
) {
return (options as Array<string>).map(item => ({
label: item,
value: item
}));
return (options as Array<string>).map(item => {
const idx = share.values.indexOf(item);
if (~idx) {
return share.options[idx];
}
const option = {
label: item,
value: item
};
share.values.push(option.value);
share.options.push(option);
return option;
});
} else if (Array.isArray(options as Options)) {
return (options as Options).map(item => {
let option = {
const value = item && item.value;
const idx = value !== undefined ? share.values.indexOf(value) : -1;
if (~idx) {
return share.options[idx];
}
const option = {
...item,
value: item && item.value
value
};
if (typeof option.children !== 'undefined') {
option.children = normalizeOptions(option.children);
option.children = normalizeOptions(option.children, share);
} else if (value !== undefined) {
share.values.push(value);
share.options.push(option);
}
return option;
});
} else if (isPlainObject(options)) {
return Object.keys(options).map(key => ({
label: (options as {[propName: string]: string})[key] as string,
value: key
}));
return Object.keys(options).map(key => {
const idx = share.values.indexOf(key);
if (~idx) {
return share.options[idx];
}
const option = {
label: (options as {[propName: string]: string})[key] as string,
value: key
};
share.values.push(option.value);
share.options.push(option);
return option;
});
}
return [];