fix(striker-ui): separate types from Select component

main
Tsu-ba-me 2 years ago committed by digimer
parent 2cd28856df
commit 4cce7b06f4
  1. 72
      striker-ui/components/Select.tsx
  2. 5
      striker-ui/types/Select.d.ts

@ -1,45 +1,25 @@
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_DEFAULT_PROPS: Required<SelectOptionalProps> = {
onClearIndicatorClick: null,
};
const Select: FC<SelectProps> = (selectProps) => {
const {
onClearIndicatorClick = SELECT_DEFAULT_PROPS.onClearIndicatorClick,
const Select: FC<SelectProps> = ({
onClearIndicatorClick,
...muiSelectProps
} = selectProps;
const { children, sx, value } = muiSelectProps;
const clearIndicator: JSX.Element | undefined =
String(value).length > 0 && onClearIndicatorClick ? (
<MUIInputAdornment position="end">
<MUIIconButton onClick={onClearIndicatorClick}>
<CloseIcon fontSize="small" />
</MUIIconButton>
</MUIInputAdornment>
) : undefined;
}) => {
const { sx: selectSx, value, ...restMuiSelectProps } = muiSelectProps;
const combinedSx = {
const combinedSx = useMemo(
() => ({
[`& .${muiSelectClasses.icon}`]: {
color: GREY,
},
@ -59,24 +39,32 @@ const Select: FC<SelectProps> = (selectProps) => {
visibility: 'visible',
},
...sx,
};
...selectSx,
}),
[selectSx],
);
const clearIndicatorElement = useMemo(
() =>
String(value).length > 0 &&
onClearIndicatorClick && (
<MUIInputAdornment position="end">
<MUIIconButton onClick={onClearIndicatorClick}>
<CloseIcon fontSize="small" />
</MUIIconButton>
</MUIInputAdornment>
),
[onClearIndicatorClick, value],
);
return (
<MUISelect
{...{
endAdornment: clearIndicator,
...muiSelectProps,
sx: combinedSx,
}}
>
{children}
</MUISelect>
endAdornment={clearIndicatorElement}
value={value}
{...restMuiSelectProps}
sx={combinedSx}
/>
);
};
Select.defaultProps = SELECT_DEFAULT_PROPS;
export type { SelectProps };
export default Select;

@ -0,0 +1,5 @@
type SelectOptionalProps = {
onClearIndicatorClick?: import('@mui/material').IconButtonProps['onClick'];
};
type SelectProps = import('@mui/material').SelectProps & SelectOptionalProps;
Loading…
Cancel
Save