2019-06-19 14:40:52 +00:00
|
|
|
import { Omit } from "@material-ui/core";
|
|
|
|
import ButtonBase from "@material-ui/core/ButtonBase";
|
|
|
|
import { CheckboxProps as MuiCheckboxProps } from "@material-ui/core/Checkbox";
|
|
|
|
import {
|
|
|
|
createStyles,
|
|
|
|
Theme,
|
|
|
|
withStyles,
|
|
|
|
WithStyles
|
|
|
|
} from "@material-ui/core/styles";
|
2019-09-05 08:32:21 +00:00
|
|
|
import { fade } from "@material-ui/core/styles/colorManipulator";
|
2019-06-19 14:40:52 +00:00
|
|
|
import classNames from "classnames";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
export type CheckboxProps = Omit<
|
|
|
|
MuiCheckboxProps,
|
|
|
|
| "checkedIcon"
|
|
|
|
| "color"
|
|
|
|
| "icon"
|
|
|
|
| "indeterminateIcon"
|
|
|
|
| "classes"
|
|
|
|
| "onChange"
|
2019-08-09 11:14:35 +00:00
|
|
|
| "onClick"
|
2019-06-19 14:40:52 +00:00
|
|
|
> & {
|
2019-08-09 11:14:35 +00:00
|
|
|
disableClickPropagation?: boolean;
|
2019-06-19 14:40:52 +00:00
|
|
|
onChange?: (event: React.ChangeEvent<any>) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const styles = (theme: Theme) =>
|
|
|
|
createStyles({
|
|
|
|
box: {
|
|
|
|
"&$checked": {
|
|
|
|
"&:before": {
|
2019-09-05 08:32:21 +00:00
|
|
|
background: theme.palette.primary.main,
|
2019-09-10 15:57:52 +00:00
|
|
|
color: theme.palette.background.paper,
|
2019-09-09 14:07:09 +00:00
|
|
|
content: '"\\2713"',
|
2019-09-05 08:32:21 +00:00
|
|
|
fontWeight: "bold",
|
|
|
|
textAlign: "center"
|
2019-06-19 14:40:52 +00:00
|
|
|
},
|
|
|
|
borderColor: theme.palette.primary.main
|
|
|
|
},
|
|
|
|
"&$disabled": {
|
|
|
|
borderColor: theme.palette.grey[200]
|
|
|
|
},
|
|
|
|
"&$indeterminate": {
|
|
|
|
"&:before": {
|
|
|
|
background: theme.palette.primary.main,
|
|
|
|
height: 2,
|
|
|
|
top: 5
|
|
|
|
},
|
|
|
|
borderColor: theme.palette.primary.main
|
|
|
|
},
|
|
|
|
"&:before": {
|
|
|
|
background: "rgba(0, 0, 0, 0)",
|
|
|
|
content: '""',
|
2019-09-05 08:32:21 +00:00
|
|
|
height: 14,
|
|
|
|
left: -1,
|
2019-06-19 14:40:52 +00:00
|
|
|
position: "absolute",
|
2019-09-05 08:32:21 +00:00
|
|
|
top: -1,
|
2019-06-19 14:40:52 +00:00
|
|
|
transition: theme.transitions.duration.short + "ms",
|
2019-09-05 08:32:21 +00:00
|
|
|
width: 14
|
2019-06-19 14:40:52 +00:00
|
|
|
},
|
2019-09-05 08:32:21 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
WebkitAppearance: "none",
|
2019-09-10 15:57:52 +00:00
|
|
|
border: `1px solid ${theme.palette.action.active}`,
|
2019-06-19 14:40:52 +00:00
|
|
|
boxSizing: "border-box",
|
|
|
|
cursor: "pointer",
|
|
|
|
height: 14,
|
|
|
|
outline: "0",
|
|
|
|
position: "relative",
|
|
|
|
userSelect: "none",
|
|
|
|
width: 14
|
|
|
|
},
|
|
|
|
checked: {},
|
|
|
|
disabled: {},
|
|
|
|
indeterminate: {},
|
|
|
|
root: {
|
2019-09-09 14:07:09 +00:00
|
|
|
"&:hover": {
|
|
|
|
background: fade(theme.palette.primary.main, 0.1)
|
|
|
|
},
|
2019-09-11 08:33:57 +00:00
|
|
|
alignSelf: "start",
|
2019-06-19 14:40:52 +00:00
|
|
|
borderRadius: "100%",
|
|
|
|
cursor: "pointer",
|
|
|
|
display: "flex",
|
2019-09-05 08:32:21 +00:00
|
|
|
height: 30,
|
2019-06-19 14:40:52 +00:00
|
|
|
justifyContent: "center",
|
2019-10-22 11:35:19 +00:00
|
|
|
margin: "5px 9px",
|
2019-09-09 14:07:09 +00:00
|
|
|
width: 30
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const Checkbox = withStyles(styles, { name: "Checkbox" })(
|
|
|
|
({
|
|
|
|
checked,
|
|
|
|
className,
|
|
|
|
classes,
|
|
|
|
disabled,
|
2019-08-09 11:14:35 +00:00
|
|
|
disableClickPropagation,
|
2019-06-19 14:40:52 +00:00
|
|
|
indeterminate,
|
|
|
|
onChange,
|
|
|
|
value,
|
|
|
|
name,
|
|
|
|
...props
|
|
|
|
}: CheckboxProps & WithStyles<typeof styles>) => {
|
|
|
|
const inputRef = React.useRef<HTMLInputElement>(null);
|
2019-08-09 11:14:35 +00:00
|
|
|
const handleClick = React.useCallback(
|
|
|
|
disableClickPropagation
|
|
|
|
? event => {
|
|
|
|
event.stopPropagation();
|
|
|
|
inputRef.current.click();
|
|
|
|
}
|
|
|
|
: () => inputRef.current.click(),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
|
|
|
<ButtonBase
|
|
|
|
{...props}
|
|
|
|
centerRipple
|
|
|
|
className={classNames(classes.root, className)}
|
|
|
|
disabled={disabled}
|
2019-08-09 11:14:35 +00:00
|
|
|
onClick={handleClick}
|
2019-06-19 14:40:52 +00:00
|
|
|
>
|
|
|
|
<input
|
|
|
|
className={classNames(classes.box, {
|
|
|
|
[classes.checked]: checked,
|
|
|
|
[classes.disabled]: disabled,
|
|
|
|
[classes.indeterminate]: indeterminate
|
|
|
|
})}
|
|
|
|
disabled={disabled}
|
|
|
|
type="checkbox"
|
|
|
|
name={name}
|
|
|
|
value={checked !== undefined && checked.toString()}
|
|
|
|
ref={inputRef}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
</ButtonBase>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Checkbox.displayName = "Checkbox";
|
|
|
|
export default Checkbox;
|