Skip to content

Commit e4fde92

Browse files
committed
feat: add support for custom filters
Signed-off-by: Mike Murray <hellomikemurray@gmail.com>
1 parent 9ffd99e commit e4fde92

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

package/src/components/DataTable/DataTable.js

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ import React from "react";
22
import PropTypes from "prop-types";
33
import clsx from "clsx";
44
import {
5+
AppBar,
6+
ButtonGroup,
57
Box,
8+
Drawer,
9+
IconButton,
610
Table,
711
TableBody,
812
TableCell,
913
TableHead,
1014
TextField,
1115
TableRow,
1216
Toolbar,
13-
makeStyles,
14-
Typography
17+
Typography,
18+
makeStyles
1519
} from "@material-ui/core";
1620
import ChevronLeftIcon from "mdi-material-ui/ChevronLeft";
1721
import ChevronRightIcon from "mdi-material-ui/ChevronRight";
22+
import CloseIcon from "mdi-material-ui/Close";
1823
import Button from "../Button";
1924
import Select from "../Select";
2025
import ActionMenu from "../ActionMenu";
@@ -67,13 +72,16 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
6772
actionMenuProps,
6873
ToolbarComponent,
6974
PaginationComponent,
75+
setShowAdditionalFilters,
76+
shouldShowAdditionalFilters,
7077

7178
// Props unique from the useDataTable hook
7279
isSelectable,
7380
onGlobalFilterChange,
7481

7582
// useTable Props
7683
getTableProps,
84+
flatColumns,
7785
headerGroups,
7886
page,
7987
prepareRow,
@@ -90,6 +98,7 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
9098

9199
const classes = useStyles();
92100
const shouldShowStandardToolbar = (actionMenuProps || isFilterable);
101+
const hasMoreFilters = flatColumns.filter(({ canFilter }) => canFilter).length > 3;
93102

94103
return (
95104
<>
@@ -110,6 +119,49 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
110119
variant="outlined"
111120
/>
112121
)}
122+
<Box paddingLeft={2}>
123+
<ButtonGroup>
124+
{flatColumns
125+
.filter(({ canFilter }) => canFilter)
126+
.slice(0, 3)
127+
.map((column) => (
128+
column.render("Filter")
129+
))
130+
}
131+
{hasMoreFilters &&
132+
<Button
133+
color="primary"
134+
onClick={() => setShowAdditionalFilters(!shouldShowAdditionalFilters)}
135+
>
136+
More Filters
137+
</Button>
138+
}
139+
</ButtonGroup>
140+
<Drawer
141+
anchor="right"
142+
open={shouldShowAdditionalFilters}
143+
onClose={() => setShowAdditionalFilters(false)}
144+
>
145+
<AppBar position="sticky">
146+
<Toolbar>
147+
<Box flex={1} paddingLeft={2}>
148+
<Typography variant="h3">More Filters</Typography>
149+
</Box>
150+
<IconButton>
151+
<CloseIcon />
152+
</IconButton>
153+
</Toolbar>
154+
</AppBar>
155+
{flatColumns
156+
.filter(({ canFilter }) => canFilter)
157+
.map((column) => (
158+
React.cloneElement(column.render("Filter"), {
159+
container: "card"
160+
})
161+
))
162+
}
163+
</Drawer>
164+
</Box>
113165
</Toolbar>
114166
))}
115167
<Table ref={ref} {...getTableProps()}>
@@ -278,6 +330,10 @@ DataTable.propTypes = {
278330
* Row data as an array of objects
279331
*/
280332
data: PropTypes.arrayOf(PropTypes.object),
333+
/**
334+
* Flattened array of the original column data
335+
*/
336+
flatColumns: PropTypes.arrayOf(PropTypes.object),
281337
/**
282338
* Get props for table
283339
*/
@@ -334,6 +390,14 @@ DataTable.propTypes = {
334390
* Set the size of the pages
335391
*/
336392
setPageSize: PropTypes.func,
393+
/**
394+
* Callback for setting the state shouldShowAdditionalFilters
395+
*/
396+
setShowAdditionalFilters: PropTypes.func,
397+
/**
398+
* Show or hide the additional filters drawer
399+
*/
400+
shouldShowAdditionalFilters: PropTypes.bool,
337401
/**
338402
* Table state [state, updater]
339403
*/

0 commit comments

Comments
 (0)