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

118 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import { IconButtonProps } from "@material-ui/core/IconButton";
2019-10-30 14:34:24 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-06-19 14:40:52 +00:00
import TableCell from "@material-ui/core/TableCell";
import Toolbar from "@material-ui/core/Toolbar";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-06-19 14:40:52 +00:00
2019-08-09 11:14:35 +00:00
import RowNumberSelect from "@saleor/components/RowNumberSelect";
import { maybe } from "@saleor/misc";
import { ListSettings } from "../../types";
2019-06-19 14:40:52 +00:00
import TablePaginationActions from "./TablePaginationActions";
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(theme => ({
actions: {
color: theme.palette.text.secondary,
flexShrink: 0,
marginLeft: theme.spacing(2.5)
},
caption: {
flexShrink: 0
},
input: {
flexShrink: 0,
fontSize: "inherit"
},
root: {
"&:last-child": {
padding: 0
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
},
select: {
paddingLeft: theme.spacing(),
paddingRight: theme.spacing(2)
},
selectIcon: {
top: 1
},
selectRoot: {
color: theme.palette.text.secondary,
marginLeft: theme.spacing(),
marginRight: theme.spacing(4)
},
spacer: {
flex: "1 1 100%"
},
toolbar: {
height: 56,
minHeight: 56,
paddingLeft: 2,
paddingRight: 2
}
}));
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
interface TablePaginationProps {
2019-06-19 14:40:52 +00:00
backIconButtonProps?: Partial<IconButtonProps>;
colSpan: number;
component?: string | typeof TableCell;
2019-08-09 11:14:35 +00:00
settings?: ListSettings;
2019-06-19 14:40:52 +00:00
hasNextPage: boolean;
hasPreviousPage: boolean;
nextIconButtonProps?: Partial<IconButtonProps>;
onNextPage(event);
onPreviousPage(event);
2019-08-09 11:14:35 +00:00
onUpdateListSettings?(key: keyof ListSettings, value: any): void;
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
const TablePagination: React.FC<TablePaginationProps> = props => {
const {
2019-06-19 14:40:52 +00:00
backIconButtonProps,
colSpan: colSpanProp,
component: Component,
2019-08-09 11:14:35 +00:00
settings,
2019-06-19 14:40:52 +00:00
hasNextPage,
hasPreviousPage,
nextIconButtonProps,
onNextPage,
onPreviousPage,
2019-08-09 11:14:35 +00:00
onUpdateListSettings,
2019-06-19 14:40:52 +00:00
...other
2019-10-30 14:34:24 +00:00
} = props;
const classes = useStyles(props);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
let colSpan;
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
if (Component === TableCell || Component === "td") {
colSpan = colSpanProp || 1000;
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
return (
<Component className={classes.root} colSpan={colSpan} {...other}>
<Toolbar className={classes.toolbar}>
<div className={classes.spacer}>
{maybe(() => settings.rowNumber) && (
<RowNumberSelect
choices={[20, 30, 50, 100]}
settings={settings}
onChange={onUpdateListSettings}
/>
)}
</div>
<TablePaginationActions
backIconButtonProps={backIconButtonProps}
hasNextPage={hasNextPage}
hasPreviousPage={hasPreviousPage}
nextIconButtonProps={nextIconButtonProps}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
/>
</Toolbar>
</Component>
);
};
2019-06-19 14:40:52 +00:00
TablePagination.defaultProps = {
component: TableCell
};
TablePagination.displayName = "TablePagination";
export default TablePagination;