Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add RadioGroupField component

Krzysztof Bialoglowicz21ee50ef390f
diff --git a/saleor/static/dashboard-next/components/RadioGroupField/RadioGroupField.tsx b/saleor/static/dashboard-next/components/RadioGroupField/RadioGroupField.tsx
new file mode 100644
index 0000000000..38b2e02925
--- /dev/null
+++ b/saleor/static/dashboard-next/components/RadioGroupField/RadioGroupField.tsx
@@ -0,0 +1,104 @@
+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 * as React from "react";
+
+import i18n from "../../i18n";
+
+const styles = createStyles({
+ formControl: {
+ width: "100%",
+ padding: "0 15px"
+ },
+ radioLabel: {
+ "& > span": {
+ padding: "6px"
+ }
+ },
+ formLabel: {
+ marginLeft: "-5px",
+ paddingBottom: "10px"
+ }
+});
+
+interface RadioGroupFieldProps extends WithStyles<typeof styles> {
+ choices: Array<{
+ value: string;
+ label: string | React.ReactNode;
+ }>;
+ className?: string;
+ disabled?: boolean;
+ error?: boolean;
+ hint?: string;
+ label?: string;
+ name?: string;
+ placeholder?: string;
+ value?: string;
+ onChange(event: any);
+}
+
+export const RadioGroupField = withStyles(styles, {
+ name: "RadioGroupField"
+})(
+ ({
+ className,
+ classes,
+ disabled,
+ error,
+ label,
+ choices,
+ value,
+ onChange,
+ name,
+ hint,
+ placeholder
+ }: RadioGroupFieldProps) => {
+ const choicesByKey: { [key: string]: string } =
+ choices === undefined
+ ? {}
+ : choices.reduce((prev, curr) => {
+ prev[curr.value] = curr.label;
+ return prev;
+ }, {});
+
+ return (
+ <FormControl
+ className={classNames(classes.formControl, className)}
+ error={error}
+ disabled={disabled}
+ >
+ <FormLabel className={classes.formLabel}>{label}</FormLabel>
+ <RadioGroup
+ aria-label={name}
+ name={name}
+ className={classes.group}
+ value={value}
+ onChange={onChange}
+ >
+ {choices.length > 0 ? (
+ choices.map(choice => (
+ <FormControlLabel
+ value={choice.value}
+ className={classes.radioLabel}
+ control={<Radio color="primary" />}
+ label={choice.value}
+ key={choice.value}
+ />
+ ))
+ ) : (
+ <MenuItem disabled={true}>{i18n.t("No results found")}</MenuItem>
+ )}
+ </RadioGroup>
+ {hint && <FormHelperText>{hint}</FormHelperText>}
+ </FormControl>
+ );
+ }
+);
+RadioGroupField.displayName = "RadioGroupField";
+export default RadioGroupField;
diff --git a/saleor/static/dashboard-next/components/RadioGroupField/index.ts b/saleor/static/dashboard-next/components/RadioGroupField/index.ts
new file mode 100644
index 0000000000..58ebd26ab1
--- /dev/null
+++ b/saleor/static/dashboard-next/components/RadioGroupField/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./RadioGroupField";
+export * from "./RadioGroupField";