2024-07-02 14:12:48 +02:00

73 lines
1.8 KiB
TypeScript

import React from "react";
import { IOption } from "../Form/SelectField";
import Tooltip from "../ToolTip";
import Typography, { ITypo, ITypoColor } from "../Typography";
import classes from "./classes.module.scss";
import classNames from "classnames";
type IProps = {
name?: string;
option: IOption;
toolTip?: string;
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
checked: boolean;
disabled?: boolean;
};
type IState = {
checked: boolean;
};
export default class CheckBox extends React.Component<IProps, IState> {
static defaultProps = {
toolTip: "",
checked: false,
disabled: false,
};
constructor(props: IProps) {
super(props);
this.state = {
checked: this.props.checked ?? false,
};
this.onChange = this.onChange.bind(this);
}
public override render(): JSX.Element {
return (
<Typography typo={ITypo.P_ERR_16} color={ITypoColor.COLOR_GENERIC_BLACK}>
<label className={classNames(classes["root"], this.props.disabled && classes["disabled"])}>
<input
type="checkbox"
name={this.props.name ?? (this.props.option.value as string)}
value={this.props.option.value as string}
onChange={this.onChange}
checked={this.state.checked}
disabled={this.props.disabled}
/>
{this.props.option.label}
{this.props.toolTip && <Tooltip className={classes["tooltip"]} text={this.props.toolTip} />}
</label>
</Typography>
);
}
public override componentDidUpdate(prevProps: Readonly<IProps>): void {
if (prevProps.checked !== this.props.checked) {
this.setState({
checked: this.props.checked,
});
}
}
private onChange(e: React.ChangeEvent<HTMLInputElement>) {
this.setState({
checked: !this.state.checked,
});
this.props.onChange && this.props.onChange(e);
}
}