Skip to content

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

Add checkbox component

dominik-zeglena78a12f460d4
diff --git a/saleor/static/dashboard-next/components/Checkbox/Checkbox.tsx b/saleor/static/dashboard-next/components/Checkbox/Checkbox.tsx
new file mode 100644
index 0000000000..012eedb865
--- /dev/null
+++ b/saleor/static/dashboard-next/components/Checkbox/Checkbox.tsx
@@ -0,0 +1,111 @@
+import { Omit } from "@material-ui/core";
+import ButtonBase from "@material-ui/core/ButtonBase";
+import { CheckboxProps as MuiCheckboxProps } from "@material-ui/core/Checkbox";
+import {
+ createStyles,
+ Theme,
+ withStyles,
+ WithStyles
+} from "@material-ui/core/styles";
+import classNames from "classnames";
+import * as React from "react";
+
+export type CheckboxProps = Omit<
+ MuiCheckboxProps,
+ "checkedIcon" | "color" | "icon" | "indeterminateIcon" | "classes"
+>;
+
+const styles = (theme: Theme) =>
+ createStyles({
+ box: {
+ "&$checked": {
+ "&:before": {
+ background: theme.palette.primary.main
+ },
+ borderColor: theme.palette.primary.main
+ },
+ "&$indeterminate": {
+ "&:before": {
+ background: theme.palette.primary.main,
+ height: 2,
+ top: 5
+ },
+ borderColor: theme.palette.primary.main
+ },
+ "&:before": {
+ background: "rgba(0, 0, 0, 0)",
+ borderRadius: 2,
+ content: '""',
+ height: 8,
+ left: 2,
+ position: "absolute",
+ top: 2,
+ transition: theme.transitions.duration.short + "ms",
+ width: 8
+ },
+ WebkitAppearance: "none",
+ border: `1px solid ${theme.palette.grey[500]}`,
+ borderRadius: 4,
+ cursor: "pointer",
+ height: 14,
+ outline: "0",
+ position: "relative",
+ userSelect: "none",
+ width: 14
+ },
+ checked: {},
+ indeterminate: {},
+ root: {
+ alignItems: "center",
+ borderRadius: "100%",
+ cursor: "pointer",
+ display: "flex",
+ height: 48,
+ justifyContent: "center",
+ width: 48
+ }
+ });
+const Checkbox = withStyles(styles, { name: "Checkbox" })(
+ ({
+ checked,
+ className,
+ classes,
+ indeterminate,
+ onChange,
+ onClick,
+ value,
+ name,
+ ...props
+ }: CheckboxProps & WithStyles<typeof styles>) => {
+ const inputRef = React.useRef<HTMLInputElement>(null);
+ const handleClick = (event: React.FormEvent<HTMLElement>) =>
+ onChange(
+ {
+ ...event,
+ target: inputRef.current
+ } as any,
+ !checked
+ );
+ return (
+ <ButtonBase
+ {...props}
+ centerRipple
+ className={classNames(classes.root, className)}
+ onClick={handleClick}
+ >
+ <input
+ className={classNames(classes.box, {
+ [classes.checked]: checked,
+ [classes.indeterminate]: indeterminate
+ })}
+ type="checkbox"
+ name={name}
+ value={checked.toString()}
+ ref={inputRef}
+ />
+ </ButtonBase>
+ );
+ }
+);
+Checkbox.displayName = "Checkbox";
+export default Checkbox;
diff --git a/saleor/static/dashboard-next/components/Checkbox/index.ts b/saleor/static/dashboard-next/components/Checkbox/index.ts
new file mode 100644
index 0000000000..3d46e4f826
--- /dev/null
+++ b/saleor/static/dashboard-next/components/Checkbox/index.ts
@@ -0,0 +1,2 @@
+export { default } from './Checkbox';
+export * from './Checkbox';
\ No newline at end of file
diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.js
index bbbf339295..172044633e 100644
--- a/saleor/static/dashboard-next/storybook/config.js
+++ b/saleor/static/dashboard-next/storybook/config.js
@@ -7,6 +7,7 @@ function loadStories() {
require("./stories/components/AddressEdit");
require("./stories/components/AddressFormatter");
require("./stories/components/CardMenu");
+ require("./stories/components/Checkbox");
require("./stories/components/Date");
require("./stories/components/DateTime");
require("./stories/components/EditableTableCell");
diff --git a/saleor/static/dashboard-next/storybook/stories/components/Checkbox.tsx b/saleor/static/dashboard-next/storybook/stories/components/Checkbox.tsx
new file mode 100644
index 0000000000..9293a48e91
--- /dev/null
+++ b/saleor/static/dashboard-next/storybook/stories/components/Checkbox.tsx
@@ -0,0 +1,38 @@
+import { storiesOf } from "@storybook/react";
+import * as React from "react";
+
+import Checkbox, { CheckboxProps } from "../../../components/Checkbox";
+import Form from "../../../components/Form";
+import CardDecorator from "../../CardDecorator";
+import Decorator from "../../Decorator";
+
+const props: CheckboxProps = {
+ checked: false,
+ name: "data"
+};
+
+storiesOf("Generics / Checkbox", module)
+ .addDecorator(CardDecorator)
+ .addDecorator(Decorator)
+ .add("checked", () => <Checkbox {...props} checked={true} />)
+ .add("unchecked", () => <Checkbox {...props} />)
+ .add("undeterminate", () => <Checkbox {...props} indeterminate={true} />)
+ .add("interactive", () => (
+ <Form initial={{ data: false }}>
+ {({ change, data }) => (
+ <Checkbox
+ {...props}
+ checked={data.data}
+ // Omit second argument
+ onChange={event =>
+ change({
+ target: {
+ name: event.target.name,
+ value: !data.data
+ }
+ } as any)
+ }
+ />
+ )}
+ </Form>
+ ));