saleor-dashboard/src/components/RadioSwitchField/RadioSwitchField.tsx

96 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-09-16 01:00:38 +00:00
import FormControl from "@material-ui/core/FormControl";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Radio from "@material-ui/core/Radio";
import RadioGroup from "@material-ui/core/RadioGroup";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-09-16 01:00:38 +00:00
import classNames from "classnames";
import React from "react";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles({
2019-09-16 01:00:38 +00:00
formControl: {
padding: 0,
width: "100%"
},
formLabel: {
marginLeft: "-5px",
paddingBottom: "10px"
},
radioLabel: {
"& > span": {
padding: "10px 6px"
}
},
secondLabel: {
display: "block",
fontSize: "12px"
}
});
interface RadioSwitchFieldProps {
className?: string;
disabled?: boolean;
error?: boolean;
firstOptionLabel: React.ReactNode;
name?: string;
secondOptionLabel: React.ReactNode;
value?: boolean;
onChange: (event: React.ChangeEvent<any>) => void;
}
2019-10-30 14:34:24 +00:00
export const RadioSwitchField: React.FC<RadioSwitchFieldProps> = props => {
const {
2019-09-16 01:00:38 +00:00
className,
disabled,
error,
firstOptionLabel,
onChange,
name,
secondOptionLabel,
value
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
2019-09-16 01:00:38 +00:00
2019-10-30 14:34:24 +00:00
const initialValue = value ? "true" : "false";
2019-09-16 01:00:38 +00:00
2019-10-30 14:34:24 +00:00
const change = event => {
onChange({
target: {
name: event.target.name,
value: event.target.value === "true" ? true : false
}
} as any);
};
return (
<FormControl
className={classNames(classes.formControl, className)}
error={error}
disabled={disabled}
>
<RadioGroup
aria-label={name}
name={name}
value={initialValue}
onChange={event => change(event)}
2019-09-16 01:00:38 +00:00
>
2019-10-30 14:34:24 +00:00
<FormControlLabel
value="true"
className={classes.radioLabel}
control={<Radio color="primary" />}
label={firstOptionLabel}
2019-09-16 01:00:38 +00:00
name={name}
2019-10-30 14:34:24 +00:00
/>
<FormControlLabel
value="false"
className={classes.radioLabel}
control={<Radio color="primary" />}
label={secondOptionLabel}
name={name}
/>
</RadioGroup>
</FormControl>
);
};
2019-09-16 01:00:38 +00:00
RadioSwitchField.displayName = "RadioSwitchField";
export default RadioSwitchField;