From 1094ad7d20273e75ac1d56958ee34181478e99dc Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Mon, 2 May 2022 14:40:31 -0400 Subject: [PATCH] fix(striker-ui): add button to clear Select --- striker-ui/components/Select.tsx | 54 +++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/striker-ui/components/Select.tsx b/striker-ui/components/Select.tsx index e8d038bf..8f98cce0 100644 --- a/striker-ui/components/Select.tsx +++ b/striker-ui/components/Select.tsx @@ -1,20 +1,63 @@ +import { FC } from 'react'; 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 { GREY } from '../lib/consts/DEFAULT_THEME'; -type SelectProps = MUISelectProps; +type SelectOptionalProps = { + onClearIndicatorClick?: MUIIconButtonProps['onClick'] | null; +}; + +type SelectProps = MUISelectProps & SelectOptionalProps; + +const SELECT_DEFAULT_PROPS: Required = { + onClearIndicatorClick: null, +}; + +const Select: FC = (selectProps) => { + const { + onClearIndicatorClick = SELECT_DEFAULT_PROPS.onClearIndicatorClick, + ...muiSelectProps + } = selectProps; + const { children, sx } = muiSelectProps; + const clearIndicator: JSX.Element | undefined = onClearIndicatorClick ? ( + + + + + + ) : undefined; -const Select = (selectProps: SelectProps): JSX.Element => { - const { children, sx } = selectProps; const combinedSx = { [`& .${muiSelectClasses.icon}`]: { color: GREY, }, + [`& .${muiInputAdornmentClasses.root}`]: { + marginRight: '.8em', + }, + + [`& .${muiIconButtonClasses.root}`]: { + color: GREY, + visibility: 'hidden', + }, + + [`&:hover .${muiInputAdornmentClasses.root} .${muiIconButtonClasses.root}, + &.${inputClasses.focused} .${muiInputAdornmentClasses.root} .${muiIconButtonClasses.root}`]: + { + visibility: 'visible', + }, + ...sx, }; @@ -22,7 +65,8 @@ const Select = (selectProps: SelectProps): JSX.Element => { @@ -31,6 +75,8 @@ const Select = (selectProps: SelectProps): JSX.Element => { ); }; +Select.defaultProps = SELECT_DEFAULT_PROPS; + export type { SelectProps }; export default Select;