diff --git a/striker-ui/components/SelectWithLabel.tsx b/striker-ui/components/SelectWithLabel.tsx index b062403a..8da8980f 100644 --- a/striker-ui/components/SelectWithLabel.tsx +++ b/striker-ui/components/SelectWithLabel.tsx @@ -1,4 +1,4 @@ -import { FC } from 'react'; +import { FC, useCallback, useMemo } from 'react'; import { Checkbox as MUICheckbox, FormControl as MUIFormControl, @@ -28,7 +28,7 @@ type SelectWithLabelOptionalProps = { type SelectWithLabelProps = SelectWithLabelOptionalProps & { id: string; - selectItems: SelectItem[]; + selectItems: Array; }; const SELECT_WITH_LABEL_DEFAULT_PROPS: Required = @@ -54,51 +54,86 @@ const SelectWithLabel: FC = ({ inputLabelProps, isReadOnly, messageBoxProps, - selectProps, + selectProps = {}, isCheckableItems = selectProps?.multiple, -}) => ( - - {label && ( - - {label} - - )} - - - -); + const combinedSx = useMemo( + () => + isReadOnly + ? { + [`& .${muiSelectClasses.icon}`]: { + visibility: 'hidden', + }, + + ...selectSx, + } + : selectSx, + [isReadOnly, selectSx], + ); + + const createCheckbox = useCallback( + (value) => + isCheckableItems && ( + + ), + [checkItem, isCheckableItems], + ); + const createMenuItem = useCallback( + (value, displayValue) => ( + + {createCheckbox(value)} + {displayValue} + + ), + [createCheckbox, disableItem, hideItem, id], + ); + + const inputElement = useMemo(() => , [label]); + const labelElement = useMemo( + () => + label && ( + + {label} + + ), + [id, inputLabelProps, label], + ); + const menuItemElements = useMemo( + () => + selectItems.map((item) => { + const { value, displayValue = value }: SelectItem = + typeof item === 'string' ? { value: item } : item; + + return createMenuItem(value, displayValue); + }), + [createMenuItem, selectItems], + ); + + return ( + + {labelElement} + + + + ); +}; SelectWithLabel.defaultProps = SELECT_WITH_LABEL_DEFAULT_PROPS;