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

101 lines
2.5 KiB
TypeScript
Raw Normal View History

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";
import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
import classNames from "classnames";
import React from "react";
import { FormattedMessage } from "react-intl";
2019-08-09 10:17:04 +00:00
const styles = createStyles({
formControl: {
2019-09-11 08:29:17 +00:00
padding: 0,
2019-08-09 10:17:04 +00:00
width: "100%"
},
formLabel: {
marginLeft: "-5px",
paddingBottom: "10px"
},
radioLabel: {
"& > span": {
padding: "6px"
}
2019-09-11 14:24:24 +00:00
},
secondLabel: {
display: "block",
fontSize: "12px"
2019-08-09 10:17:04 +00:00
}
});
2019-09-16 01:00:38 +00:00
interface RadioGroupFieldProps {
2019-08-09 10:17:04 +00:00
choices: Array<{
value: string;
2019-09-16 01:00:38 +00:00
label: React.ReactNode;
2019-08-09 10:17:04 +00:00
}>;
className?: string;
disabled?: boolean;
error?: boolean;
hint?: string;
label?: string;
name?: string;
value?: string;
onChange: (event: React.ChangeEvent<any>) => void;
}
export const RadioGroupField = withStyles(styles, {
name: "RadioGroupField"
})(
({
className,
classes,
disabled,
error,
label,
choices,
value,
onChange,
name,
hint
2019-09-16 01:00:38 +00:00
}: RadioGroupFieldProps & WithStyles<typeof styles>) => {
2019-08-09 10:17:04 +00:00
return (
<FormControl
className={classNames(classes.formControl, className)}
error={error}
disabled={disabled}
>
{label ? (
<FormLabel className={classes.formLabel}>{label}</FormLabel>
) : null}
<RadioGroup
aria-label={name}
name={name}
value={value}
onChange={onChange}
>
{choices.length > 0 ? (
2019-09-11 14:24:24 +00:00
choices.map(choice => (
<FormControlLabel
value={choice.value}
className={classes.radioLabel}
control={<Radio color="primary" />}
2019-09-16 01:00:38 +00:00
label={choice.label}
2019-09-11 14:24:24 +00:00
key={choice.value}
/>
))
2019-08-09 10:17:04 +00:00
) : (
<MenuItem disabled={true}>
<FormattedMessage defaultMessage="No results found" />
</MenuItem>
2019-08-09 10:17:04 +00:00
)}
</RadioGroup>
{hint && <FormHelperText>{hint}</FormHelperText>}
</FormControl>
);
}
);
RadioGroupField.displayName = "RadioGroupField";
export default RadioGroupField;