diff --git a/striker-ui/components/Select.tsx b/striker-ui/components/Select.tsx index 9e037ae8..fffbc715 100644 --- a/striker-ui/components/Select.tsx +++ b/striker-ui/components/Select.tsx @@ -1,82 +1,70 @@ -import { FC } from 'react'; +import { Close as CloseIcon } from '@mui/icons-material'; import { IconButton as MUIIconButton, - IconButtonProps as MUIIconButtonProps, iconButtonClasses as muiIconButtonClasses, inputClasses, Select as MUISelect, selectClasses as muiSelectClasses, - SelectProps as MUISelectProps, InputAdornment as MUIInputAdornment, inputAdornmentClasses as muiInputAdornmentClasses, } from '@mui/material'; -import { Close as CloseIcon } from '@mui/icons-material'; +import { FC, useMemo } from 'react'; import { GREY } from '../lib/consts/DEFAULT_THEME'; -type SelectOptionalProps = { - onClearIndicatorClick?: MUIIconButtonProps['onClick'] | null; -}; - -type SelectProps = MUISelectProps & SelectOptionalProps; +const Select: FC = ({ + onClearIndicatorClick, + ...muiSelectProps +}) => { + const { sx: selectSx, value, ...restMuiSelectProps } = muiSelectProps; -const SELECT_DEFAULT_PROPS: Required = { - onClearIndicatorClick: null, -}; - -const Select: FC = (selectProps) => { - const { - onClearIndicatorClick = SELECT_DEFAULT_PROPS.onClearIndicatorClick, - ...muiSelectProps - } = selectProps; - const { children, sx, value } = muiSelectProps; - const clearIndicator: JSX.Element | undefined = - String(value).length > 0 && onClearIndicatorClick ? ( - - - - - - ) : undefined; - - const combinedSx = { - [`& .${muiSelectClasses.icon}`]: { - color: GREY, - }, + const combinedSx = useMemo( + () => ({ + [`& .${muiSelectClasses.icon}`]: { + color: GREY, + }, - [`& .${muiInputAdornmentClasses.root}`]: { - marginRight: '.8em', - }, + [`& .${muiInputAdornmentClasses.root}`]: { + marginRight: '.8em', + }, - [`& .${muiIconButtonClasses.root}`]: { - color: GREY, - visibility: 'hidden', - }, + [`& .${muiIconButtonClasses.root}`]: { + color: GREY, + visibility: 'hidden', + }, - [`&:hover .${muiInputAdornmentClasses.root} .${muiIconButtonClasses.root}, + [`&:hover .${muiInputAdornmentClasses.root} .${muiIconButtonClasses.root}, &.${inputClasses.focused} .${muiInputAdornmentClasses.root} .${muiIconButtonClasses.root}`]: - { - visibility: 'visible', - }, + { + visibility: 'visible', + }, + + ...selectSx, + }), + [selectSx], + ); - ...sx, - }; + const clearIndicatorElement = useMemo( + () => + String(value).length > 0 && + onClearIndicatorClick && ( + + + + + + ), + [onClearIndicatorClick, value], + ); return ( - {children} - + endAdornment={clearIndicatorElement} + value={value} + {...restMuiSelectProps} + sx={combinedSx} + /> ); }; -Select.defaultProps = SELECT_DEFAULT_PROPS; - -export type { SelectProps }; - export default Select; diff --git a/striker-ui/types/Select.d.ts b/striker-ui/types/Select.d.ts new file mode 100644 index 00000000..6b656304 --- /dev/null +++ b/striker-ui/types/Select.d.ts @@ -0,0 +1,5 @@ +type SelectOptionalProps = { + onClearIndicatorClick?: import('@mui/material').IconButtonProps['onClick']; +}; + +type SelectProps = import('@mui/material').SelectProps & SelectOptionalProps;