2019-10-20 16:59:02 +02:00
|
|
|
import React, { ChangeEvent } from "react";
|
|
|
|
|
import { Help } from "../index";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
label?: string;
|
|
|
|
|
name?: string;
|
|
|
|
|
checked: boolean;
|
|
|
|
|
onChange?: (value: boolean, name?: string) => void;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
helpText?: string;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
2018-11-13 07:39:19 +01:00
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
class Checkbox extends React.Component<Props> {
|
2019-10-20 16:59:02 +02:00
|
|
|
onCheckboxChange = (event: ChangeEvent<HTMLInputElement>) => {
|
2018-09-03 16:17:36 +02:00
|
|
|
if (this.props.onChange) {
|
2018-11-06 07:59:34 +01:00
|
|
|
this.props.onChange(event.target.checked, this.props.name);
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-02 09:49:34 +02:00
|
|
|
renderHelp = () => {
|
|
|
|
|
const helpText = this.props.helpText;
|
2018-10-04 10:16:20 +02:00
|
|
|
if (helpText) {
|
2018-11-13 07:39:19 +01:00
|
|
|
return <Help message={helpText} />;
|
|
|
|
|
}
|
2018-10-02 09:49:34 +02:00
|
|
|
};
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
render() {
|
|
|
|
|
return (
|
2018-10-02 09:49:34 +02:00
|
|
|
<div className="field is-grouped">
|
2018-10-02 13:04:34 +02:00
|
|
|
<div className="control">
|
2019-10-20 16:59:02 +02:00
|
|
|
{/*
|
|
|
|
|
we have to ignore the next line,
|
|
|
|
|
because jsx label does not the custom disabled attribute
|
|
|
|
|
but bulma does.
|
|
|
|
|
// @ts-ignore */}
|
2018-09-03 16:17:36 +02:00
|
|
|
<label className="checkbox" disabled={this.props.disabled}>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={this.props.checked}
|
|
|
|
|
onChange={this.onCheckboxChange}
|
|
|
|
|
disabled={this.props.disabled}
|
2019-10-20 16:59:02 +02:00
|
|
|
/>{" "}
|
2018-09-03 16:17:36 +02:00
|
|
|
{this.props.label}
|
2018-11-13 07:39:19 +01:00
|
|
|
{this.renderHelp()}
|
2018-09-03 16:17:36 +02:00
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default Checkbox;
|