Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add column picker component
dominik-zeglen904a0c4bd7ee
diff --git a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsxnew file mode 100644index 0000000000..e373f724d9--- /dev/null+++ b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx@@ -0,0 +1,70 @@+import Grow from "@material-ui/core/Grow";+import Popper from "@material-ui/core/Popper";+import { Theme } from "@material-ui/core/styles";+import makeStyles from "@material-ui/styles/makeStyles";+import React from "react";+import ColumnPickerButton from "./ColumnPickerButton";+import ColumnPickerContent, {+ ColumnPickerContentProps+} from "./ColumnPickerContent";++export interface ColumnPickerProps extends ColumnPickerContentProps {+ className?: string;+ initial?: boolean;+}++const useStyles = makeStyles(+ (theme: Theme) => ({+ popper: {+ marginTop: theme.spacing.unit * 2+ }+ }),+ {+ name: "ColumnPicker"+ }+);++const ColumnPicker: React.FC<ColumnPickerProps> = props => {+ const { className, columns, initial, selectedColumns } = props;+ const classes = useStyles(props);+ const anchor = React.useRef<HTMLDivElement>();+ const [isExpanded, setExpansionState] = React.useState(false);++ React.useEffect(() => {+ setTimeout(() => setExpansionState(!!initial), 100);+ }, []);++ return (+ <div ref={anchor} className={className}>+ <ColumnPickerButton+ active={isExpanded}+ onClick={() => setExpansionState(prevState => !prevState)}+ />+ <Popper+ className={classes.popper}+ open={isExpanded}+ anchorEl={anchor.current}+ transition+ disablePortal+ placement="bottom-end"+ >+ {({ TransitionProps, placement }) => (+ <Grow+ {...TransitionProps}+ style={{+ transformOrigin:+ placement === "bottom" ? "right bottom" : "right top"+ }}+ >+ <ColumnPickerContent+ columns={columns}+ selectedColumns={selectedColumns}+ />+ </Grow>+ )}+ </Popper>+ </div>+ );+};++export default ColumnPicker;diff --git a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerButton.tsx b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerButton.tsxnew file mode 100644index 0000000000..bdd7dde479--- /dev/null+++ b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerButton.tsx@@ -0,0 +1,67 @@+import Button from "@material-ui/core/Button";+import { Theme } from "@material-ui/core/styles";+import { fade } from "@material-ui/core/styles/colorManipulator";+import ArrowDropDownIcon from "@material-ui/icons/ArrowDropDown";+import makeStyles from "@material-ui/styles/makeStyles";+import classNames from "classnames";+import React from "react";++import i18n from "@saleor/i18n";++interface ColumnPickerButtonProps {+ active: boolean;+ className?: string;+ onClick: () => void;+}++const useStyles = makeStyles(+ (theme: Theme) => ({+ icon: {+ marginLeft: theme.spacing.unit * 2,+ transition: theme.transitions.duration.short + "ms"+ },+ root: {+ "& span": {+ color: theme.palette.primary.main+ },+ paddingRight: theme.spacing.unit+ },+ rootActive: {+ background: fade(theme.palette.primary.main, 0.1)+ },+ rotate: {+ transform: "rotate(180deg)"+ }+ }),+ {+ name: "ColumnPickerButton"+ }+);++const ColumnPickerButton: React.FC<ColumnPickerButtonProps> = props => {+ const { active, className, onClick } = props;+ const classes = useStyles(props);++ return (+ <Button+ className={classNames(classes.root, className, {+ [classes.rootActive]: active+ })}+ color="primary"+ onClick={onClick}+ variant="outlined"+ >+ {i18n.t("Columns", {+ context: "select visible columns button"+ })}+ <ArrowDropDownIcon+ color="primary"+ className={classNames(classes.icon, {+ [classes.rotate]: active+ })}+ />+ </Button>+ );+};++export default ColumnPickerButton;diff --git a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsxnew file mode 100644index 0000000000..78a0726e89--- /dev/null+++ b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx@@ -0,0 +1,113 @@+import Button from "@material-ui/core/Button";+import Card from "@material-ui/core/Card";+import CardContent from "@material-ui/core/CardContent";+import { Theme } from "@material-ui/core/styles";+import Typography from "@material-ui/core/Typography";+import makeStyles from "@material-ui/styles/makeStyles";+import classNames from "classnames";+import React from "react";++import useElementScroll from "@saleor/hooks/useElementScroll";+import i18n from "@saleor/i18n";+import { isSelected } from "@saleor/utils/lists";+import ControlledCheckbox from "../ControlledCheckbox";+import Hr from "../Hr";++export interface ColumnPickerChoice {+ label: string;+ value: string;+}+export interface ColumnPickerContentProps {+ columns: ColumnPickerChoice[];+ selectedColumns: string[];+}++const useStyles = makeStyles((theme: Theme) => ({+ actionBar: {+ display: "flex",+ justifyContent: "space-between"+ },+ actionBarContainer: {+ boxShadow: `0px 0px 0px 0px ${theme.palette.background.paper}`,+ transition: theme.transitions.duration.short + "ms"+ },+ content: {+ display: "grid",+ gridColumnGap: theme.spacing.unit * 3,+ gridTemplateColumns: "repeat(3, 1fr)",+ maxHeight: 256,+ overflowX: "visible",+ overflowY: "scroll",+ padding: theme.spacing.unit * 2+ },+ contentContainer: {+ padding: 0+ },+ dropShadow: {+ boxShadow: `0px -5px 10px 0px ${theme.overrides.MuiCard.root.borderColor}`+ }+}));++const ColumnPickerContent: React.FC<ColumnPickerContentProps> = props => {+ const { columns, selectedColumns } = props;+ const classes = useStyles(props);+ const anchor = React.useRef<HTMLDivElement>();+ const scrollPosition = useElementScroll(anchor);++ const dropShadow = anchor.current+ ? scrollPosition.y + anchor.current.clientHeight <+ anchor.current.scrollHeight+ : false;++ return (+ <Card>+ <CardContent>+ <Typography color="textSecondary">+ {i18n.t(+ "{{ numberOfSelected }} columns selected out of {{ numberOfTotal }}",+ {+ context: "pick columns to display",+ numberOfSelected: selectedColumns.length,+ numberOfTotal: columns.length+ }+ )}+ </Typography>+ </CardContent>+ <Hr />+ <CardContent className={classes.contentContainer}>+ <div className={classes.content} ref={anchor}>+ {columns.map(column => (+ <ControlledCheckbox+ checked={isSelected(+ column.value,+ selectedColumns,+ (a, b) => a === b+ )}+ name={column.value}+ label={column.label}+ onChange={() => undefined}+ />+ ))}+ </div>+ </CardContent>+ <Hr />+ <CardContent+ className={classNames(classes.actionBarContainer, {+ [classes.dropShadow]: dropShadow+ })}+ >+ <div className={classes.actionBar}>+ <Button color="default">{i18n.t("Reset")}</Button>+ <div>+ <Button color="default">{i18n.t("Cancel")}</Button>+ <Button color="primary" variant="contained">+ {i18n.t("Save")}+ </Button>+ </div>+ </div>+ </CardContent>+ </Card>+ );+};++export default ColumnPickerContent;diff --git a/saleor/static/dashboard-next/components/ColumnPicker/index.ts b/saleor/static/dashboard-next/components/ColumnPicker/index.tsnew file mode 100644index 0000000000..8dd5ec6ac1--- /dev/null+++ b/saleor/static/dashboard-next/components/ColumnPicker/index.ts@@ -0,0 +1,2 @@+export { default } from "./ColumnPicker";+export * from "./ColumnPicker";diff --git a/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx b/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsxindex 8ed381f208..7c18775463 100644--- a/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx+++ b/saleor/static/dashboard-next/components/SaveButtonBar/SaveButtonBar.tsx@@ -9,7 +9,7 @@ import {import classNames from "classnames";import React from "react";-import useScroll from "@saleor/hooks/useScroll";+import useWindowScroll from "@saleor/hooks/useWindowScroll";import i18n from "../../i18n";import { maybe } from "../../misc";import AppActionContext from "../AppLayout/AppActionContext";@@ -82,7 +82,7 @@ export const SaveButtonBar = withStyles(styles, { name: "SaveButtonBar" })(onSave,...props}: SaveButtonBarProps) => {- const scrollPosition = useScroll();+ const scrollPosition = useWindowScroll();const scrolledToBottom =scrollPosition.y + window.innerHeight >= document.body.scrollHeight;diff --git a/saleor/static/dashboard-next/hooks/useElementScroll.ts b/saleor/static/dashboard-next/hooks/useElementScroll.tsnew file mode 100644index 0000000000..0804d824ec--- /dev/null+++ b/saleor/static/dashboard-next/hooks/useElementScroll.ts@@ -0,0 +1,34 @@+import throttle from "lodash-es/throttle";+import { MutableRefObject, useEffect, useState } from "react";++function getPosition(anchor?: HTMLElement) {+ if (!!anchor) {+ return {+ x: anchor.scrollLeft,+ y: anchor.scrollTop+ };+ }+ return {+ x: 0,+ y: 0+ };+}++function useElementScroll(anchor: MutableRefObject<HTMLElement>) {+ const [scroll, setScroll] = useState(getPosition(anchor.current));++ useEffect(() => {+ if (!!anchor.current) {+ const handleScroll = throttle(+ () => setScroll(getPosition(anchor.current)),+ 100+ );+ anchor.current.addEventListener("scroll", handleScroll);++ return () => anchor.current.removeEventListener("scroll", handleScroll);+ }+ }, [anchor.current]);++ return scroll;+}+export default useElementScroll;diff --git a/saleor/static/dashboard-next/hooks/useScroll.ts b/saleor/static/dashboard-next/hooks/useWindowScroll.tssimilarity index 87%rename from saleor/static/dashboard-next/hooks/useScroll.tsrename to saleor/static/dashboard-next/hooks/useWindowScroll.tsindex 6e871b8c23..5218d58c9f 100644--- a/saleor/static/dashboard-next/hooks/useScroll.ts+++ b/saleor/static/dashboard-next/hooks/useWindowScroll.ts@@ -8,11 +8,11 @@ function getPosition() {};}-function useScroll() {+function useWindowScroll() {const [scroll, setScroll] = useState(getPosition);useEffect(() => {- const handleScroll = throttle(() => setScroll(getPosition()), 250);+ const handleScroll = throttle(() => setScroll(getPosition()), 100);window.addEventListener("scroll", handleScroll);@@ -21,4 +21,4 @@ function useScroll() {return scroll;}-export default useScroll;+export default useWindowScroll;diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.jsindex 9a050f083d..93cbb15258 100644--- a/saleor/static/dashboard-next/storybook/config.js+++ b/saleor/static/dashboard-next/storybook/config.js@@ -11,6 +11,7 @@ function loadStories() {require("./stories/components/CardMenu");require("./stories/components/Checkbox");require("./stories/components/Chip");+ require("./stories/components/ColumnPicker");require("./stories/components/Date");require("./stories/components/DateTime");require("./stories/components/DeleteFilterTabDialog");diff --git a/saleor/static/dashboard-next/storybook/stories/components/ColumnPicker.tsx b/saleor/static/dashboard-next/storybook/stories/components/ColumnPicker.tsxnew file mode 100644index 0000000000..6bc1f3350d--- /dev/null+++ b/saleor/static/dashboard-next/storybook/stories/components/ColumnPicker.tsx@@ -0,0 +1,41 @@+import { storiesOf } from "@storybook/react";+import * as React from "react";++import ColumnPicker, {+ ColumnPickerProps+} from "@saleor/components/ColumnPicker";+import { ColumnPickerChoice } from "@saleor/components/ColumnPicker/ColumnPickerContent";+import CardDecorator from "@saleor/storybook/CardDecorator";+import Decorator from "../../Decorator";++const columns: ColumnPickerChoice[] = [+ { label: "Name", value: "name" },+ { label: "Value", value: "value" },+ { label: "Type", value: "type" },+ { label: "Size", value: "size" },+ { label: "Status", value: "isPublished" },+ { label: "Price", value: "price" },+ { label: "Digital", value: "isDigital" },+ ...Array(15)+ .fill(0)+ .map((_, index) => ({+ label: "Attribute " + (index + 1),+ value: "attribute_" + index+ }))+];++const props: ColumnPickerProps = {+ columns,+ initial: true,+ selectedColumns: [1, 3, 4, 6].map(index => columns[index].value)+};++storiesOf("Generics / Column picker", module)+ .addDecorator(storyFn => (+ <div style={{ display: "flex ", justifyContent: "center" }}>+ {storyFn()}+ </div>+ ))+ .addDecorator(CardDecorator)+ .addDecorator(Decorator)+ .add("default", () => <ColumnPicker {...props} />);