TeaWeb/shared/js/ui/react-elements/Checkbox.tsx

52 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-05-20 20:46:59 +02:00
import * as React from "react";
import {ReactElement} from "react";
2020-05-20 20:46:59 +02:00
const cssStyle = require("./Checkbox.scss");
export interface CheckboxProperties {
label?: ReactElement | string;
2020-05-20 20:46:59 +02:00
disabled?: boolean;
onChange?: (value: boolean) => void;
value?: boolean;
2020-05-20 20:46:59 +02:00
initialValue?: boolean;
className?: string;
2020-05-20 20:46:59 +02:00
children?: never;
}
export interface CheckboxState {
checked?: boolean;
disabled?: boolean;
2020-05-20 20:46:59 +02:00
}
export class Checkbox extends React.Component<CheckboxProperties, CheckboxState> {
constructor(props) {
super(props);
this.state = { };
2020-05-20 20:46:59 +02:00
}
render() {
const disabled = typeof this.state.disabled === "boolean" ? this.state.disabled : this.props.disabled;
const checked = typeof this.props.value === "boolean" ? this.props.value : typeof this.state.checked === "boolean" ? this.state.checked : this.props.initialValue;
const disabledClass = disabled ? cssStyle.disabled : "";
2020-05-20 20:46:59 +02:00
return (
<label className={cssStyle.labelCheckbox + " " + disabledClass + " " + this.props.className}>
2020-05-20 20:46:59 +02:00
<div className={cssStyle.checkbox + " " + disabledClass}>
<input type={"checkbox"} checked={checked} disabled={disabled} onChange={event => this.onStateChange(event)} />
2020-05-20 20:46:59 +02:00
<div className={cssStyle.mark} />
</div>
{this.props.label ? <a>{this.props.label}</a> : undefined}
</label>
);
}
private onStateChange(event: React.ChangeEvent) {
2020-05-20 20:46:59 +02:00
if(this.props.onChange)
this.props.onChange((event.target as HTMLInputElement).checked);
2020-05-20 20:46:59 +02:00
this.setState({ checked: !this.state.checked });
}
}