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

185 lines
4.6 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 "PUBLIC_DIR/images/checkbox.indeterminate.react.svg";
import CheckboxCheckedIcon from "PUBLIC_DIR/images/checkbox.checked.react.svg";
import CheckboxIcon from "PUBLIC_DIR/images/checkbox.react.svg";
2019-06-25 15:49:12 +00:00
// eslint-disable-next-line react/prop-types
2022-10-19 11:49:26 +00:00
const RenderCheckboxIcon = ({ isChecked, isIndeterminate, tabIndex }) => {
// let newProps = {
// size: "medium",
// className: "checkbox",
// };
// return <>{React.createElement(Icons[iconName], { ...newProps })}</>;
return (
<>
{isIndeterminate ? (
2022-10-19 11:49:26 +00:00
<CheckboxIndeterminateIcon
tabIndex={tabIndex}
className="checkbox not-selectable"
/>
) : isChecked ? (
2022-10-19 11:49:26 +00:00
<CheckboxCheckedIcon
tabIndex={tabIndex}
className="checkbox not-selectable"
/>
) : (
2022-10-19 11:49:26 +00:00
<CheckboxIcon tabIndex={tabIndex} 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) {
if (this.props.isDisabled) return e.preventDefault();
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,
onChange,
isChecked,
2022-10-19 11:49:26 +00:00
tabIndex,
2023-01-25 09:56:17 +00:00
hasError,
...rest
} = this.props;
return (
<>
<StyledLabel
id={id}
style={style}
isDisabled={isDisabled}
isIndeterminate={isIndeterminate}
className={className}
title={title}
2023-01-25 09:56:17 +00:00
hasError={hasError}
>
<HiddenInput
name={name}
type="checkbox"
checked={this.state.checked}
isDisabled={isDisabled}
ref={this.ref}
value={value}
onChange={this.onInputChange}
2022-10-19 11:49:26 +00:00
tabIndex={-1}
{...rest}
/>
2022-10-19 11:49:26 +00:00
<RenderCheckboxIcon tabIndex={tabIndex} {...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]),
/** Sets the checked state of the checkbox */
2019-06-25 15:49:12 +00:00
isChecked: PropTypes.bool,
/** The state is displayed as a rectangle in the checkbox when set to true */
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,
/** Is triggered whenever the 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,
/** Renders the help button */
helpButton: PropTypes.any,
/** Checkbox tab index */
2022-10-19 11:49:26 +00:00
tabIndex: PropTypes.number,
/** Notifies if the error occurs */
2023-01-25 09:56:17 +00:00
hasError: PropTypes.bool,
2019-07-24 14:28:34 +00:00
};
Checkbox.defaultProps = {
isChecked: false,
truncate: false,
2022-10-19 11:49:26 +00:00
tabIndex: -1,
2023-01-25 09:56:17 +00:00
hasError: false,
2019-07-24 14:28:34 +00:00
};
2019-06-25 15:49:12 +00:00
export default React.memo(Checkbox);