From 182837c93b7d504fe37b5ab5ca2949b15915de8d Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Wed, 15 Feb 2023 16:51:08 -0500 Subject: [PATCH] fix(striker-ui): allow array options in SelectWithLabel --- striker-ui/components/SelectWithLabel.tsx | 125 ++++++++++++++-------- 1 file changed, 80 insertions(+), 45 deletions(-) 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;