2019-08-09 10:17:04 +00:00
|
|
|
import FormControl from "@material-ui/core/FormControl";
|
|
|
|
import FormControlLabel from "@material-ui/core/FormControlLabel";
|
|
|
|
import FormHelperText from "@material-ui/core/FormHelperText";
|
|
|
|
import FormLabel from "@material-ui/core/FormLabel";
|
|
|
|
import MenuItem from "@material-ui/core/MenuItem";
|
|
|
|
import Radio from "@material-ui/core/Radio";
|
|
|
|
import RadioGroup from "@material-ui/core/RadioGroup";
|
2019-10-28 16:16:49 +00:00
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2019-08-09 10:17:04 +00:00
|
|
|
import classNames from "classnames";
|
|
|
|
import React from "react";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
2019-10-10 12:45:25 +00:00
|
|
|
const useStyles = makeStyles(
|
2019-10-28 16:16:49 +00:00
|
|
|
theme => ({
|
2019-10-10 12:45:25 +00:00
|
|
|
formLabel: {
|
2019-10-28 16:16:49 +00:00
|
|
|
marginBottom: theme.spacing(1)
|
2019-10-10 12:45:25 +00:00
|
|
|
},
|
|
|
|
radioLabel: {
|
2019-11-04 13:36:25 +00:00
|
|
|
marginBottom: -theme.spacing(0.5)
|
2019-10-10 12:45:25 +00:00
|
|
|
},
|
|
|
|
root: {
|
|
|
|
"& $radioLabel": {
|
|
|
|
"&:last-of-type": {
|
|
|
|
marginBottom: 0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
padding: 0,
|
|
|
|
width: "100%"
|
|
|
|
},
|
|
|
|
rootNoLabel: {
|
2019-10-28 16:16:49 +00:00
|
|
|
marginTop: -theme.spacing(1.5)
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
2019-10-10 12:45:25 +00:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
name: "RadioGroupField"
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
2019-10-10 12:45:25 +00:00
|
|
|
);
|
2019-08-09 10:17:04 +00:00
|
|
|
|
2020-07-30 09:54:16 +00:00
|
|
|
export interface RadioGroupFieldChoice<
|
|
|
|
T extends string | number = string | number
|
|
|
|
> {
|
|
|
|
disabled?: boolean;
|
|
|
|
value: T;
|
2019-09-16 11:43:02 +00:00
|
|
|
label: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
2019-09-16 01:00:38 +00:00
|
|
|
interface RadioGroupFieldProps {
|
2019-09-16 11:43:02 +00:00
|
|
|
choices: RadioGroupFieldChoice[];
|
2019-08-09 10:17:04 +00:00
|
|
|
className?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
error?: boolean;
|
|
|
|
hint?: string;
|
|
|
|
label?: string;
|
|
|
|
name?: string;
|
2020-07-30 09:54:16 +00:00
|
|
|
value: string | number;
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange: (event: React.ChangeEvent<any>) => void;
|
|
|
|
}
|
|
|
|
|
2019-10-10 12:45:25 +00:00
|
|
|
export const RadioGroupField: React.FC<RadioGroupFieldProps> = props => {
|
|
|
|
const {
|
2019-08-09 10:17:04 +00:00
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
error,
|
|
|
|
label,
|
|
|
|
choices,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
name,
|
|
|
|
hint
|
2019-10-10 12:45:25 +00:00
|
|
|
} = props;
|
|
|
|
const classes = useStyles(props);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl
|
|
|
|
className={classNames(classes.root, className, {
|
|
|
|
[classes.rootNoLabel]: !label
|
|
|
|
})}
|
|
|
|
error={error}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
|
|
|
{label ? (
|
|
|
|
<FormLabel className={classes.formLabel}>{label}</FormLabel>
|
|
|
|
) : null}
|
|
|
|
<RadioGroup
|
|
|
|
aria-label={name}
|
|
|
|
name={name}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
2019-08-09 10:17:04 +00:00
|
|
|
>
|
2019-10-10 12:45:25 +00:00
|
|
|
{choices.length > 0 ? (
|
|
|
|
choices.map(choice => (
|
|
|
|
<FormControlLabel
|
2020-07-30 09:54:16 +00:00
|
|
|
disabled={choice.disabled}
|
2019-10-10 12:45:25 +00:00
|
|
|
value={choice.value}
|
|
|
|
className={classes.radioLabel}
|
|
|
|
control={<Radio color="primary" />}
|
|
|
|
label={choice.label}
|
|
|
|
key={choice.value}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
) : (
|
|
|
|
<MenuItem disabled={true}>
|
|
|
|
<FormattedMessage defaultMessage="No results found" />
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
</RadioGroup>
|
|
|
|
{hint && <FormHelperText>{hint}</FormHelperText>}
|
|
|
|
</FormControl>
|
|
|
|
);
|
|
|
|
};
|
2019-08-09 10:17:04 +00:00
|
|
|
RadioGroupField.displayName = "RadioGroupField";
|
|
|
|
export default RadioGroupField;
|