parent
f064623dd3
commit
e2c264a40f
3 changed files with 61 additions and 49 deletions
@ -1,34 +1,62 @@ |
||||
import { |
||||
Button as MUIButton, |
||||
Button as MuiButton, |
||||
buttonClasses as muiButtonClasses, |
||||
SxProps, |
||||
Theme, |
||||
styled, |
||||
} from '@mui/material'; |
||||
import { FC, useMemo } from 'react'; |
||||
import { FC } from 'react'; |
||||
|
||||
import { BLACK, DISABLED, GREY } from '../lib/consts/DEFAULT_THEME'; |
||||
import { |
||||
BLACK, |
||||
BLUE, |
||||
DISABLED, |
||||
GREY, |
||||
RED, |
||||
TEXT, |
||||
} from '../lib/consts/DEFAULT_THEME'; |
||||
|
||||
const MAP_TO_COLOUR: Record<ContainedButtonBackground, string> = { |
||||
blue: BLUE, |
||||
normal: GREY, |
||||
red: RED, |
||||
}; |
||||
|
||||
const ContainedButton: FC<ContainedButtonProps> = ({ sx, ...restProps }) => { |
||||
const combinedSx = useMemo<SxProps<Theme>>( |
||||
() => ({ |
||||
backgroundColor: GREY, |
||||
color: BLACK, |
||||
textTransform: 'none', |
||||
const BaseStyle = styled(MuiButton)({ |
||||
backgroundColor: GREY, |
||||
color: BLACK, |
||||
textTransform: 'none', |
||||
|
||||
'&:hover': { |
||||
backgroundColor: `${GREY}F0`, |
||||
}, |
||||
'&:hover': { |
||||
backgroundColor: `${GREY}F0`, |
||||
}, |
||||
|
||||
[`&.${muiButtonClasses.disabled}`]: { |
||||
backgroundColor: DISABLED, |
||||
}, |
||||
[`&.${muiButtonClasses.disabled}`]: { |
||||
backgroundColor: DISABLED, |
||||
}, |
||||
}); |
||||
|
||||
...sx, |
||||
}), |
||||
[sx], |
||||
); |
||||
const Base: FC<ContainedButtonProps> = (props) => ( |
||||
<BaseStyle variant="contained" {...props} /> |
||||
); |
||||
|
||||
return <MUIButton variant="contained" {...restProps} sx={combinedSx} />; |
||||
}; |
||||
const ContainedButton = styled(Base)((props) => { |
||||
const { background = 'normal' } = props; |
||||
|
||||
let bg: string | undefined; |
||||
let color: string | undefined; |
||||
|
||||
if (background !== 'normal') { |
||||
bg = MAP_TO_COLOUR[background]; |
||||
color = TEXT; |
||||
} |
||||
|
||||
return { |
||||
backgroundColor: bg, |
||||
color, |
||||
|
||||
'&:hover': { |
||||
backgroundColor: `${bg}F0`, |
||||
}, |
||||
}; |
||||
}); |
||||
|
||||
export default ContainedButton; |
||||
|
@ -1 +1,8 @@ |
||||
type ContainedButtonProps = import('@mui/material').ButtonProps; |
||||
type ContainedButtonBackground = 'blue' | 'normal' | 'red'; |
||||
|
||||
type ContainedButtonOptionalProps = { |
||||
background?: ContainedButtonBackground; |
||||
}; |
||||
|
||||
type ContainedButtonProps = import('@mui/material').ButtonProps & |
||||
ContainedButtonOptionalProps; |
||||
|
Loading…
Reference in new issue