解决 labelField valueField 不是 label value 的问题

This commit is contained in:
liaoxuezhi 2019-11-08 10:52:58 +08:00
parent 9d4b983d9e
commit 70d034460c
2 changed files with 15 additions and 8 deletions

View File

@ -528,7 +528,7 @@ export class Select extends React.Component<SelectProps, SelectState> {
</div>
) : (
<div className={`${ns}Select-value`} key={index}>
{item.label}
{item[labelField || 'label']}
</div>
)
);

View File

@ -50,6 +50,7 @@ export default class SelectControl extends React.Component<SelectProps, any> {
delimiter,
multiple,
type,
valueField,
onChange,
setOptions,
options,
@ -62,7 +63,11 @@ export default class SelectControl extends React.Component<SelectProps, any> {
(Array.isArray(value) ? value : value ? [value] : []).forEach(
(option: any) => {
let resolved = find(options, (item: any) => item.value == option.value);
let resolved = find(
options,
(item: any) =>
item[valueField || 'value'] == option[valueField || 'value']
);
resolved || additonalOptions.push(option);
}
);
@ -70,22 +75,24 @@ export default class SelectControl extends React.Component<SelectProps, any> {
if (joinValues) {
if (multiple) {
newValue = Array.isArray(value)
? (value.map(item => item.value).join(delimiter) as string)
? (value
.map(item => item[valueField || 'value'])
.join(delimiter) as string)
: value
? (value as Option).value
? (value as Option)[valueField || 'value']
: '';
} else {
newValue = newValue ? (newValue as Option).value : '';
newValue = newValue ? (newValue as Option)[valueField || 'value'] : '';
}
} else if (extractValue) {
if (multiple) {
newValue = Array.isArray(value)
? value.map(item => item.value)
? value.map(item => item[valueField || 'value'])
: value
? [(value as Option).value]
? [(value as Option)[valueField || 'value']]
: [''];
} else {
newValue = newValue ? (newValue as Option).value : '';
newValue = newValue ? (newValue as Option)[valueField || 'value'] : '';
}
}