fix item loading issue in multi select dropdown option

This commit is contained in:
Simon Larsen 2023-08-22 10:29:41 +01:00
parent 6bc9e97433
commit f5dab43799
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE

View File

@ -39,9 +39,44 @@ const Dropdown: FunctionComponent<ComponentProps> = (
| DropdownValue
| DropdownOption
| Array<DropdownOption>
| Array<DropdownValue>
): DropdownOption | Array<DropdownOption> => {
if (Array.isArray(value)) {
return value;
if (
Array.isArray(value) &&
value.length > 0 &&
Object.keys(value[0]!).includes('value')
) {
return value as Array<DropdownOption>;
}
if (
Array.isArray(value) &&
value.length > 0 &&
typeof value[0] === 'string'
) {
const options: Array<DropdownOption> = [];
for (const item of value as Array<DropdownValue>) {
if (
(!Array.isArray(item) && typeof item === 'string') ||
typeof item === 'number'
) {
const option:
| DropdownOption
| undefined
| Array<DropdownOption> = props.options.find(
(option: DropdownOption) => {
return option.value === item;
}
) as DropdownOption | Array<DropdownOption>;
if (option) {
options.push(option as DropdownOption);
}
}
}
return options;
}
if (
@ -52,6 +87,7 @@ const Dropdown: FunctionComponent<ComponentProps> = (
return option.value === value;
}) as DropdownOption | Array<DropdownOption>;
}
return value as DropdownOption | Array<DropdownOption>;
};