diff --git a/examples/components/Form/Transfer.jsx b/examples/components/Form/Transfer.jsx index 470be4726..a70feed87 100644 --- a/examples/components/Form/Transfer.jsx +++ b/examples/components/Form/Transfer.jsx @@ -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' } ] } diff --git a/src/components/Select.tsx b/src/components/Select.tsx index 2856d4120..641def6cb 100644 --- a/src/components/Select.tsx +++ b/src/components/Select.tsx @@ -150,39 +150,92 @@ export function optionValueCompare( } export function normalizeOptions( - options: string | {[propName: string]: string} | Array | Options + options: string | {[propName: string]: string} | Array | Options, + share: { + values: Array; + options: Array; + } = { + 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) && typeof (options as Array)[0] === 'string' ) { - return (options as Array).map(item => ({ - label: item, - value: item - })); + return (options as Array).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 [];