2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
2022-08-02 10:51:42 +00:00
|
|
|
import React from 'react';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { makeStyles } from 'tss-react/mui';
|
2022-08-02 10:51:42 +00:00
|
|
|
|
|
|
|
import { isMobileBrowser } from '../../../environment/utils';
|
|
|
|
import Icon from '../../../icons/components/Icon';
|
|
|
|
import { IconCheckMark } from '../../../icons/svg';
|
|
|
|
import { withPixelLineHeight } from '../../../styles/functions.web';
|
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
interface ICheckboxProps {
|
2022-08-02 10:51:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the input is checked or not.
|
|
|
|
*/
|
|
|
|
checked: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class name for additional styles.
|
|
|
|
*/
|
|
|
|
className?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the input is disabled or not.
|
|
|
|
*/
|
|
|
|
disabled?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The label of the input.
|
|
|
|
*/
|
|
|
|
label: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the input.
|
|
|
|
*/
|
|
|
|
name?: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change callback.
|
|
|
|
*/
|
|
|
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
}
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
const useStyles = makeStyles()((theme: Theme) => {
|
2022-08-02 10:51:42 +00:00
|
|
|
return {
|
|
|
|
formControl: {
|
|
|
|
...withPixelLineHeight(theme.typography.bodyLongRegular),
|
|
|
|
color: theme.palette.text01,
|
|
|
|
display: 'inline-flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
|
|
|
|
'&.is-mobile': {
|
|
|
|
...withPixelLineHeight(theme.typography.bodyLongRegularLarge)
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
activeArea: {
|
|
|
|
display: 'grid',
|
|
|
|
placeContent: 'center',
|
|
|
|
width: '24px',
|
|
|
|
height: '24px',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
marginRight: '15px',
|
|
|
|
position: 'relative',
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
|
|
'& input[type="checkbox"]': {
|
|
|
|
appearance: 'none',
|
|
|
|
backgroundColor: 'transparent',
|
|
|
|
margin: 0,
|
|
|
|
font: 'inherit',
|
|
|
|
color: theme.palette.icon03,
|
|
|
|
width: '18px',
|
|
|
|
height: '18px',
|
|
|
|
border: `2px solid ${theme.palette.icon03}`,
|
|
|
|
borderRadius: '3px',
|
|
|
|
cursor: 'pointer',
|
|
|
|
|
|
|
|
display: 'grid',
|
|
|
|
placeContent: 'center',
|
|
|
|
|
|
|
|
'&::before': {
|
|
|
|
content: 'url("")',
|
|
|
|
width: '18px',
|
|
|
|
height: '18px',
|
|
|
|
opacity: 0,
|
|
|
|
backgroundColor: theme.palette.action01,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
border: 0,
|
|
|
|
borderRadius: '3px',
|
|
|
|
transition: '.2s'
|
|
|
|
},
|
|
|
|
|
|
|
|
'&:checked::before': {
|
|
|
|
opacity: 1
|
|
|
|
},
|
|
|
|
|
|
|
|
'&:disabled': {
|
|
|
|
backgroundColor: theme.palette.ui03,
|
|
|
|
borderColor: theme.palette.ui04,
|
|
|
|
|
|
|
|
'&::before': {
|
|
|
|
backgroundColor: theme.palette.ui04
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'&:checked+.checkmark': {
|
|
|
|
opacity: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .checkmark': {
|
|
|
|
position: 'absolute',
|
|
|
|
left: '3px',
|
|
|
|
top: '3px',
|
|
|
|
opacity: 0,
|
|
|
|
transition: '.2s'
|
|
|
|
},
|
|
|
|
|
|
|
|
'&.is-mobile': {
|
|
|
|
width: '40px',
|
|
|
|
height: '40px',
|
|
|
|
|
|
|
|
'& input[type="checkbox"]': {
|
|
|
|
width: '24px',
|
|
|
|
height: '24px',
|
|
|
|
|
|
|
|
'&::before': {
|
|
|
|
width: '24px',
|
|
|
|
height: '24px'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'& .checkmark': {
|
|
|
|
left: '11px',
|
|
|
|
top: '10px'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const Checkbox = ({
|
|
|
|
checked,
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
label,
|
|
|
|
name,
|
|
|
|
onChange
|
2022-10-20 09:11:27 +00:00
|
|
|
}: ICheckboxProps) => {
|
2022-09-13 07:36:00 +00:00
|
|
|
const { classes: styles, cx, theme } = useStyles();
|
2022-08-02 10:51:42 +00:00
|
|
|
const isMobile = isMobileBrowser();
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
return (
|
|
|
|
<div className = { cx(styles.formControl, isMobile && 'is-mobile', className) }>
|
|
|
|
<label className = { cx(styles.activeArea, isMobile && 'is-mobile') }>
|
|
|
|
<input
|
|
|
|
checked = { checked }
|
|
|
|
disabled = { disabled }
|
|
|
|
name = { name }
|
|
|
|
onChange = { onChange }
|
|
|
|
type = 'checkbox' />
|
|
|
|
<Icon
|
|
|
|
className = 'checkmark'
|
|
|
|
color = { disabled ? theme.palette.icon03 : theme.palette.icon01 }
|
|
|
|
size = { 18 }
|
|
|
|
src = { IconCheckMark } />
|
|
|
|
</label>
|
|
|
|
<label>{label}</label>
|
|
|
|
</div>
|
|
|
|
);
|
2022-08-02 10:51:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Checkbox;
|