fix issue with dropdown

This commit is contained in:
Simon Larsen 2023-06-08 17:38:43 +01:00
parent 125c6417cf
commit c018f95e70
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE

View File

@ -33,13 +33,31 @@ export interface ComponentProps {
const Dropdown: FunctionComponent<ComponentProps> = (
props: ComponentProps
): ReactElement => {
const getDropdownOptionFromValue: Function = (value: undefined | DropdownValue | DropdownOption | Array<DropdownOption>): DropdownOption | Array<DropdownOption> => {
if(Array.isArray(value)) {
return value;
}
if (!Array.isArray(value) && typeof value === 'string' || typeof value === 'number') {
return props.options.find((option) => {
return option.value === value;
}) as DropdownOption | Array<DropdownOption>;
} else {
return value as DropdownOption | Array<DropdownOption>;
}
}
const [value, setValue] = useState<
DropdownOption | Array<DropdownOption> | undefined
>(props.initialValue);
>(getDropdownOptionFromValue(props.initialValue));
useEffect(() => {
if (props.value) {
setValue(props.value ? props.value : undefined);
setValue(getDropdownOptionFromValue(props.value ? props.value : undefined));
}
}, [props.value]);