2019-07-31 13:37:13 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import styled, { css } from "styled-components";
|
|
|
|
import { Icons } from "../icons";
|
|
|
|
import { Text } from "../text";
|
|
|
|
|
|
|
|
const disableColor = "#A3A9AE";
|
|
|
|
const hoverColor = disableColor;
|
|
|
|
|
2019-06-26 13:25:35 +00:00
|
|
|
const Label = styled.label`
|
2019-06-25 15:49:12 +00:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: relative;
|
2019-07-10 15:16:47 +00:00
|
|
|
margin: 0;
|
2019-07-31 13:37:13 +00:00
|
|
|
user-select: none;
|
|
|
|
-o-user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-webkit-user-select: none;
|
2019-07-10 15:16:47 +00:00
|
|
|
|
|
|
|
.checkbox {
|
2019-07-31 13:37:13 +00:00
|
|
|
margin-right: 8px;
|
2019-07-10 15:16:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
${props =>
|
|
|
|
props.isDisabled
|
|
|
|
? css`
|
|
|
|
cursor: not-allowed;
|
|
|
|
`
|
|
|
|
: css`
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
svg {
|
2019-08-01 08:38:32 +00:00
|
|
|
rect:first-child {
|
|
|
|
stroke: ${hoverColor};
|
|
|
|
}
|
2019-07-31 13:37:13 +00:00
|
|
|
}
|
2019-07-10 15:16:47 +00:00
|
|
|
}
|
2019-07-31 13:37:13 +00:00
|
|
|
`}
|
2019-06-25 15:49:12 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
const HiddenInput = styled.input`
|
2019-06-25 15:49:12 +00:00
|
|
|
opacity: 0.0001;
|
|
|
|
position: absolute;
|
2019-07-10 15:16:47 +00:00
|
|
|
right: 0;
|
2019-07-23 14:55:27 +00:00
|
|
|
z-index: -1;
|
2019-06-25 15:49:12 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
const CheckboxIcon = ({ isChecked, isDisabled, isIndeterminate }) => {
|
|
|
|
const iconName = isIndeterminate
|
|
|
|
? "CheckboxIndeterminateIcon"
|
|
|
|
: isChecked
|
|
|
|
? "CheckboxCheckedIcon"
|
|
|
|
: "CheckboxIcon";
|
2019-06-25 15:49:12 +00:00
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
let newProps = {
|
|
|
|
size: "medium",
|
|
|
|
className: "checkbox"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isDisabled) {
|
|
|
|
newProps.isfill = true;
|
|
|
|
newProps.color = "#F8F9F9";
|
2019-06-25 15:49:12 +00:00
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
if (isIndeterminate || isChecked) {
|
|
|
|
newProps.isStroke = true;
|
|
|
|
newProps.stroke = "#ECEEF1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return <>{React.createElement(Icons[iconName], { ...newProps })}</>;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Checkbox extends React.Component {
|
2019-07-24 14:28:34 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.ref = React.createRef();
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
checked: props.isChecked
|
|
|
|
};
|
2019-07-31 13:37:13 +00:00
|
|
|
|
|
|
|
this.onInputChange = this.onInputChange.bind(this);
|
2019-07-24 14:28:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 08:41:31 +00:00
|
|
|
componentDidMount() {
|
2019-07-10 15:16:47 +00:00
|
|
|
this.ref.current.indeterminate = this.props.isIndeterminate;
|
2019-07-04 08:41:31 +00:00
|
|
|
}
|
2019-06-25 15:49:12 +00:00
|
|
|
|
2019-07-17 13:11:39 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-07-31 13:37:13 +00:00
|
|
|
if (this.props.isIndeterminate !== prevProps.isIndeterminate) {
|
2019-07-17 13:11:39 +00:00
|
|
|
this.ref.current.indeterminate = this.props.isIndeterminate;
|
|
|
|
}
|
2019-07-31 13:37:13 +00:00
|
|
|
if (this.props.isChecked !== prevProps.isChecked) {
|
|
|
|
this.setState({ checked: this.props.isChecked });
|
2019-07-24 14:28:34 +00:00
|
|
|
}
|
2019-07-04 08:41:31 +00:00
|
|
|
}
|
2019-07-31 13:37:13 +00:00
|
|
|
|
|
|
|
onInputChange(e) {
|
|
|
|
this.setState({ checked: e.target.checked });
|
|
|
|
this.props.onChange && this.props.onChange(e);
|
|
|
|
}
|
|
|
|
|
2019-07-04 08:41:31 +00:00
|
|
|
render() {
|
2019-07-26 12:35:41 +00:00
|
|
|
//console.log("Checkbox render");
|
2019-07-04 08:41:31 +00:00
|
|
|
return (
|
2019-07-31 13:37:13 +00:00
|
|
|
<Label htmlFor={this.props.id} isDisabled={this.props.isDisabled}>
|
|
|
|
<HiddenInput
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.checked}
|
|
|
|
disabled={this.props.isDisabled}
|
|
|
|
ref={this.ref}
|
|
|
|
{...this.props}
|
|
|
|
onChange={this.onInputChange}
|
|
|
|
/>
|
|
|
|
<CheckboxIcon {...this.props} />
|
|
|
|
{this.props.label && (
|
|
|
|
<Text.Body
|
2019-08-06 14:31:52 +00:00
|
|
|
as="span"
|
2019-07-31 13:37:13 +00:00
|
|
|
isDisabled={this.props.isDisabled}
|
|
|
|
disableColor={disableColor}
|
|
|
|
>
|
|
|
|
{this.props.label}
|
|
|
|
</Text.Body>
|
|
|
|
)}
|
|
|
|
</Label>
|
2019-07-04 08:41:31 +00:00
|
|
|
);
|
2019-07-31 13:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-24 14:28:34 +00:00
|
|
|
|
2019-06-25 15:49:12 +00:00
|
|
|
Checkbox.propTypes = {
|
2019-07-17 13:11:39 +00:00
|
|
|
id: PropTypes.string,
|
2019-06-25 15:49:12 +00:00
|
|
|
name: PropTypes.string,
|
2019-07-17 13:11:39 +00:00
|
|
|
value: PropTypes.string,
|
2019-06-25 15:49:12 +00:00
|
|
|
label: PropTypes.string,
|
|
|
|
|
|
|
|
isChecked: PropTypes.bool,
|
|
|
|
isIndeterminate: PropTypes.bool,
|
|
|
|
isDisabled: PropTypes.bool,
|
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
onChange: PropTypes.func
|
2019-07-24 14:28:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Checkbox.defaultProps = {
|
|
|
|
isChecked: false
|
|
|
|
};
|
2019-06-25 15:49:12 +00:00
|
|
|
|
2019-07-31 13:37:13 +00:00
|
|
|
export default Checkbox;
|