DocSpace-client/packages/asc-web-components/checkbox/index.js

167 lines
4.1 KiB
JavaScript
Raw Normal View History

import React from "react";
import PropTypes from "prop-types";
import Text from "../text";
import { StyledLabel, HiddenInput } from "./styled-checkbox";
import CheckboxIndeterminateIcon from "./svg/checkbox.indeterminate.react.svg";
import CheckboxCheckedIcon from "./svg/checkbox.checked.react.svg";
import CheckboxIcon from "./svg/checkbox.react.svg";
2019-06-25 15:49:12 +00:00
// eslint-disable-next-line react/prop-types
const RenderCheckboxIcon = ({ isChecked, isIndeterminate }) => {
// let newProps = {
// size: "medium",
// className: "checkbox",
// };
// return <>{React.createElement(Icons[iconName], { ...newProps })}</>;
return (
<>
{isIndeterminate ? (
2021-04-13 09:43:17 +00:00
<CheckboxIndeterminateIcon className="checkbox not-selectable" />
) : isChecked ? (
2021-04-13 09:43:17 +00:00
<CheckboxCheckedIcon className="checkbox not-selectable" />
) : (
2021-04-13 09:43:17 +00:00
<CheckboxIcon className="checkbox not-selectable" />
)}
</>
);
};
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-24 14:28:34 +00:00
};
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;
}
2022-03-24 17:56:51 +00:00
if (this.props.isChecked !== prevProps.isChecked) {
this.setState({ checked: this.props.isChecked });
2019-07-24 14:28:34 +00:00
}
}
onInputChange(e) {
e.stopPropagation();
this.setState({ checked: e.target.checked });
this.props.onChange && this.props.onChange(e);
}
onClick(e) {
return e.preventDefault();
}
render() {
2019-07-26 12:35:41 +00:00
//console.log("Checkbox render");
const {
isDisabled,
isIndeterminate,
id,
className,
label,
style,
value,
title,
truncate,
2022-03-24 17:56:51 +00:00
name,
helpButton,
} = this.props;
return (
<>
<StyledLabel
id={id}
style={style}
isDisabled={isDisabled}
isIndeterminate={isIndeterminate}
className={className}
title={title}
>
<HiddenInput
name={name}
type="checkbox"
checked={this.state.checked}
isDisabled={isDisabled}
ref={this.ref}
value={value}
onChange={this.onInputChange}
/>
<RenderCheckboxIcon {...this.props} />
<div className="wrapper">
{this.props.label && (
<Text
as="span"
title={title}
isDisabled={isDisabled}
truncate={truncate}
className="checkbox-text"
>
{label}
</Text>
)}
{helpButton && (
<span className="help-button" onClick={this.onClick}>
{helpButton}
</span>
)}
</div>
</StyledLabel>
</>
);
}
}
2019-07-24 14:28:34 +00:00
2019-06-25 15:49:12 +00:00
Checkbox.propTypes = {
/** Used as HTML id property */
2019-07-17 13:11:39 +00:00
id: PropTypes.string,
/** Used as HTML `name` property */
2019-06-25 15:49:12 +00:00
name: PropTypes.string,
/** Value of the input */
2019-07-17 13:11:39 +00:00
value: PropTypes.string,
/** Label of the input */
2020-07-29 18:32:50 +00:00
label: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
/** The checked property sets the checked state of a checkbox */
2019-06-25 15:49:12 +00:00
isChecked: PropTypes.bool,
/** If true, this state is shown as a rectangle in the checkbox */
2019-06-25 15:49:12 +00:00
isIndeterminate: PropTypes.bool,
/** Disables the Checkbox input */
2019-06-25 15:49:12 +00:00
isDisabled: PropTypes.bool,
/** Will be triggered whenever an CheckboxInput is clicked */
onChange: PropTypes.func,
/** Accepts class */
className: PropTypes.string,
/** Accepts css style */
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** Title */
title: PropTypes.string,
/** Disables word wrapping */
truncate: PropTypes.bool,
/** Help button render */
helpButton: PropTypes.any,
2022-04-20 09:58:09 +00:00
isLogin: PropTypes.bool,
2019-07-24 14:28:34 +00:00
};
Checkbox.defaultProps = {
isChecked: false,
truncate: false,
2022-04-20 09:58:09 +00:00
isLogin: false,
2019-07-24 14:28:34 +00:00
};
2019-06-25 15:49:12 +00:00
export default React.memo(Checkbox);