DocSpace-buildtools/web/ASC.Web.Components/src/components/checkbox/index.js

150 lines
3.1 KiB
JavaScript
Raw Normal View History

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;
margin: 0;
user-select: none;
-o-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
.checkbox {
margin-right: 8px;
}
${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-06-25 15:49:12 +00:00
`;
const HiddenInput = styled.input`
2019-06-25 15:49:12 +00:00
opacity: 0.0001;
position: absolute;
right: 0;
2019-07-23 14:55:27 +00:00
z-index: -1;
2019-06-25 15:49:12 +00:00
`;
const CheckboxIcon = ({ isChecked, isDisabled, isIndeterminate }) => {
const iconName = isIndeterminate
? "CheckboxIndeterminateIcon"
: isChecked
? "CheckboxCheckedIcon"
: "CheckboxIcon";
2019-06-25 15:49:12 +00:00
let newProps = {
size: "medium",
className: "checkbox"
};
if (isDisabled) {
newProps.isfill = true;
newProps.color = "#F8F9F9";
2019-06-25 15:49:12 +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
};
this.onInputChange = this.onInputChange.bind(this);
2019-07-24 14:28:34 +00:00
}
componentDidMount() {
this.ref.current.indeterminate = this.props.isIndeterminate;
}
2019-06-25 15:49:12 +00:00
2019-07-17 13:11:39 +00:00
componentDidUpdate(prevProps) {
if (this.props.isIndeterminate !== prevProps.isIndeterminate) {
2019-07-17 13:11:39 +00:00
this.ref.current.indeterminate = this.props.isIndeterminate;
}
if (this.props.isChecked !== prevProps.isChecked) {
this.setState({ checked: this.props.isChecked });
2019-07-24 14:28:34 +00:00
}
}
onInputChange(e) {
this.setState({ checked: e.target.checked });
this.props.onChange && this.props.onChange(e);
}
render() {
2019-07-26 12:35:41 +00:00
//console.log("Checkbox render");
return (
<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"
isDisabled={this.props.isDisabled}
disableColor={disableColor}
>
{this.props.label}
</Text.Body>
)}
</Label>
);
}
}
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,
onChange: PropTypes.func
2019-07-24 14:28:34 +00:00
};
Checkbox.defaultProps = {
isChecked: false
};
2019-06-25 15:49:12 +00:00
export default Checkbox;