2019-04-21 15:31:08 +02:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import Radio from "@material-ui/core/Radio";
|
|
|
|
import RadioGroup from "@material-ui/core/RadioGroup";
|
|
|
|
import FormControlLabel from "@material-ui/core/FormControlLabel";
|
|
|
|
|
|
|
|
const Bool = props => {
|
|
|
|
const [value, setValue] = useState(props.dflt);
|
|
|
|
const toggle = () => {
|
2019-04-22 18:00:26 +02:00
|
|
|
props.updateValue(props.name, !value);
|
2019-04-21 15:31:08 +02:00
|
|
|
setValue(!value);
|
|
|
|
};
|
2019-04-22 18:00:26 +02:00
|
|
|
// Force state update when rerendering due to props change
|
|
|
|
if (props.value !== value) setValue(props.value);
|
|
|
|
|
2019-04-21 15:31:08 +02:00
|
|
|
return (
|
|
|
|
<RadioGroup onChange={toggle} value={JSON.stringify(value)}>
|
|
|
|
<FormControlLabel
|
|
|
|
control={<Radio color="primary" />}
|
|
|
|
value="false"
|
|
|
|
checked={value === "false" ? true : false}
|
|
|
|
label={props.labels[0]}
|
2019-04-22 18:00:26 +02:00
|
|
|
className="po-list-item"
|
2019-04-21 15:31:08 +02:00
|
|
|
/>
|
|
|
|
<FormControlLabel
|
|
|
|
control={<Radio color="primary" />}
|
|
|
|
value="true"
|
|
|
|
checked={value === "true" ? true : false}
|
|
|
|
label={props.labels[1]}
|
2019-04-22 18:00:26 +02:00
|
|
|
className="po-list-item"
|
2019-04-21 15:31:08 +02:00
|
|
|
/>
|
|
|
|
</RadioGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Bool.propTypes = {
|
|
|
|
dflt: PropTypes.bool,
|
|
|
|
labels: PropTypes.arrayOf(PropTypes.string),
|
2019-04-22 18:00:26 +02:00
|
|
|
updateValue: PropTypes.func.isRequired,
|
2019-04-21 15:31:08 +02:00
|
|
|
name: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
Bool.defaultProps = {
|
|
|
|
dflt: false,
|
|
|
|
labels: ["false", "true"]
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Bool;
|