Skip to content

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

Make sortables generic

dominik-zeglen28fce98d51c7
diff --git a/saleor/static/dashboard-next/attributes/components/AttributeValues/AttributeValues.tsx b/saleor/static/dashboard-next/attributes/components/AttributeValues/AttributeValues.tsx
index f5308d9d6d..98908d5d1c 100644
--- a/saleor/static/dashboard-next/attributes/components/AttributeValues/AttributeValues.tsx
+++ b/saleor/static/dashboard-next/attributes/components/AttributeValues/AttributeValues.tsx
@@ -3,17 +3,20 @@ import Card from "@material-ui/core/Card";
import IconButton from "@material-ui/core/IconButton";
import { Theme } from "@material-ui/core/styles";
import Table from "@material-ui/core/Table";
-import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import DeleteIcon from "@material-ui/icons/Delete";
import makeStyles from "@material-ui/styles/makeStyles";
import React from "react";
-import { SortableContainer, SortableElement } from "react-sortable-hoc";
+import { SortableElement } from "react-sortable-hoc";
import CardTitle from "@saleor/components/CardTitle";
import Skeleton from "@saleor/components/Skeleton";
+import {
+ SortableTableBody,
+ SortableTableRow
+} from "@saleor/components/SortableTable";
import i18n from "@saleor/i18n";
import Draggable from "@saleor/icons/Draggable";
import { maybe, renderCollection, stopPropagation } from "@saleor/misc";
@@ -62,57 +65,6 @@ const useStyles = makeStyles((theme: Theme) => ({
}
}));
-interface SortableTableBodyProps {
- children: React.ReactNode | React.ReactNodeArray;
-}
-
-const SortableTableBody = SortableContainer<SortableTableBodyProps>(
- ({ children }) => <TableBody>{children}</TableBody>,
- {
- withRef: true
- }
-);
-
-interface SortableTableRowProps {
- disabled: boolean;
- value: AttributeDetailsFragment_values;
- onValueDelete: (id: string) => void;
- onValueUpdate: (id: string) => void;
-}
-
-const SortableTableRow = SortableElement<SortableTableRowProps>(
- ({ disabled, value, onValueDelete, onValueUpdate }) => {
- const classes = useStyles({});
-
- return (
- <TableRow
- className={!!value ? classes.link : undefined}
- hover={!!value}
- onClick={!!value ? () => onValueUpdate(value.id) : undefined}
- key={maybe(() => value.id)}
- >
- <TableCell className={classes.columnDrag}>
- <Draggable className={classes.dragIcon} />
- </TableCell>
- <TableCell className={classes.columnAdmin}>
- {maybe(() => value.slug) ? value.slug : <Skeleton />}
- </TableCell>
- <TableCell className={classes.columnStore}>
- {maybe(() => value.name) ? value.name : <Skeleton />}
- </TableCell>
- <TableCell className={classes.iconCell}>
- <IconButton
- disabled={disabled}
- onClick={stopPropagation(() => onValueDelete(value.id))}
- >
- <DeleteIcon color="primary" />
- </IconButton>
- </TableCell>
- </TableRow>
- );
- }
-);
-
const AttributeValues: React.FC<AttributeValuesProps> = ({
disabled,
onValueAdd,
@@ -158,13 +110,27 @@ const AttributeValues: React.FC<AttributeValuesProps> = ({
: undefined,
(value, valueIndex) => (
<SortableTableRow
- disabled={disabled}
- value={value}
- onValueDelete={onValueDelete}
- onValueUpdate={onValueUpdate}
- key={maybe(() => value.id, "skeleton")}
+ className={!!value ? classes.link : undefined}
+ hover={!!value}
+ onClick={!!value ? () => onValueUpdate(value.id) : undefined}
+ key={maybe(() => value.id)}
index={valueIndex}
- />
+ >
+ <TableCell className={classes.columnAdmin}>
+ {maybe(() => value.slug) ? value.slug : <Skeleton />}
+ </TableCell>
+ <TableCell className={classes.columnStore}>
+ {maybe(() => value.name) ? value.name : <Skeleton />}
+ </TableCell>
+ <TableCell className={classes.iconCell}>
+ <IconButton
+ disabled={disabled}
+ onClick={stopPropagation(() => onValueDelete(value.id))}
+ >
+ <DeleteIcon color="primary" />
+ </IconButton>
+ </TableCell>
+ </SortableTableRow>
),
() => (
<TableRow>
diff --git a/saleor/static/dashboard-next/components/SortableTable/SortableHandle.tsx b/saleor/static/dashboard-next/components/SortableTable/SortableHandle.tsx
new file mode 100644
index 0000000000..5133379729
--- /dev/null
+++ b/saleor/static/dashboard-next/components/SortableTable/SortableHandle.tsx
@@ -0,0 +1,28 @@
+import { Theme } from "@material-ui/core/styles";
+import TableCell from "@material-ui/core/TableCell";
+import makeStyles from "@material-ui/styles/makeStyles";
+import * as React from "react";
+import { SortableHandle as SortableHandleHoc } from "react-sortable-hoc";
+
+import Draggable from "@saleor/icons/Draggable";
+
+const useStyles = makeStyles((theme: Theme) => ({
+ columnDrag: {
+ width: 48 + theme.spacing.unit * 1.5
+ },
+ dragIcon: {
+ cursor: "grab"
+ }
+}));
+
+const SortableHandle = SortableHandleHoc(() => {
+ const classes = useStyles({});
+
+ return (
+ <TableCell className={classes.columnDrag}>
+ <Draggable className={classes.dragIcon} />
+ </TableCell>
+ );
+});
+
+export default SortableHandle;
diff --git a/saleor/static/dashboard-next/components/SortableTable/SortableTableBody.tsx b/saleor/static/dashboard-next/components/SortableTable/SortableTableBody.tsx
new file mode 100644
index 0000000000..ea20734a36
--- /dev/null
+++ b/saleor/static/dashboard-next/components/SortableTable/SortableTableBody.tsx
@@ -0,0 +1,13 @@
+import TableBody from "@material-ui/core/TableBody";
+import * as React from "react";
+import { SortableContainer } from "react-sortable-hoc";
+
+interface SortableTableBodyProps {
+ children: React.ReactNode | React.ReactNodeArray;
+}
+
+const SortableTableBody = SortableContainer<SortableTableBodyProps>(
+ ({ children }) => <TableBody>{children}</TableBody>
+);
+
+export default SortableTableBody;
diff --git a/saleor/static/dashboard-next/components/SortableTable/SortableTableRow.tsx b/saleor/static/dashboard-next/components/SortableTable/SortableTableRow.tsx
new file mode 100644
index 0000000000..9a9e46858e
--- /dev/null
+++ b/saleor/static/dashboard-next/components/SortableTable/SortableTableRow.tsx
@@ -0,0 +1,15 @@
+import TableRow, { TableRowProps } from "@material-ui/core/TableRow";
+import * as React from "react";
+import { SortableElement } from "react-sortable-hoc";
+import SortableHandle from "./SortableHandle";
+
+const SortableTableRow = SortableElement<TableRowProps>(
+ ({ children, ...props }) => (
+ <TableRow {...props}>
+ <SortableHandle />
+ {children}
+ </TableRow>
+ )
+);
+
+export default SortableTableRow;
diff --git a/saleor/static/dashboard-next/components/SortableTable/index.ts b/saleor/static/dashboard-next/components/SortableTable/index.ts
new file mode 100644
index 0000000000..15d3a941dd
--- /dev/null
+++ b/saleor/static/dashboard-next/components/SortableTable/index.ts
@@ -0,0 +1,5 @@
+export * from "./SortableTableBody";
+export { default as SortableTableBody } from "./SortableTableBody";
+
+export * from "./SortableTableRow";
+export { default as SortableTableRow } from "./SortableTableRow";