Skip to content

Commit c4bb027

Browse files
committed
feat: add filter display
Signed-off-by: Mike Murray <hellomikemurray@gmail.com>
1 parent 46ce960 commit c4bb027

File tree

1 file changed

+73
-10
lines changed

1 file changed

+73
-10
lines changed

package/src/components/DataTable/DataTable.js

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useCallback, useMemo } from "react";
22
import PropTypes from "prop-types";
33
import clsx from "clsx";
44
import {
@@ -25,6 +25,7 @@ import CloseIcon from "mdi-material-ui/Close";
2525
import Button from "../Button";
2626
import Select from "../Select";
2727
import ActionMenu from "../ActionMenu";
28+
import DataTableFilterChipBar from "./helpers/DataTableFilterChipBar";
2829

2930
const useStyles = makeStyles((theme) => ({
3031
pagination: {
@@ -67,6 +68,17 @@ const useStyles = makeStyles((theme) => ({
6768
}
6869
}));
6970

71+
export const defaultLabels = {
72+
allFilters: "All filters",
73+
allFiltersDrawerTitle: "All filters",
74+
globalFilterPlaceholder: "Filter",
75+
next: "Next",
76+
page: "Page",
77+
pageOf: ({ count }) => `of ${count}`,
78+
pageSizeSelect: ({ count }) => `${count} rows`,
79+
previous: "Previous"
80+
};
81+
7082
/**
7183
* @name DataTable
7284
* @param {Object} props Component props
@@ -80,9 +92,11 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
8092
actionMenuProps,
8193
ToolbarComponent,
8294
PaginationComponent,
95+
labels: labelsProp,
8396
setShowAdditionalFilters,
8497
shouldShowAdditionalFilters,
8598
onRowClick,
99+
onRemoveFilter,
86100

87101
// Props unique from the useDataTable hook
88102
isSelectable,
@@ -102,7 +116,7 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
102116
nextPage,
103117
previousPage,
104118
setPageSize,
105-
state: [{ pageIndex, pageSize }]
119+
state: [{ pageIndex, pageSize, filters }]
106120
} = props;
107121

108122
const classes = useStyles();
@@ -112,7 +126,12 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
112126
const shouldShowStandardToolbar = (actionMenuProps || isFilterable);
113127
const activeFilterCount = flatColumns.filter(({ canFilter }) => canFilter).length;
114128

115-
const handleCellClick = React.useCallback((isClickDisabled) => (event) => {
129+
const labels = useMemo(() => ({
130+
...defaultLabels,
131+
...labelsProp
132+
}), [labelsProp]);
133+
134+
const handleCellClick = useCallback((isClickDisabled) => (event) => {
116135
if (isClickDisabled) {
117136
event.stopPropagation();
118137
}
@@ -145,7 +164,7 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
145164
className={classes.textField}
146165
fullWidth
147166
margin="dense"
148-
placeholder="Filter"
167+
placeholder={labels.globalFilterPlaceholder}
149168
onChange={onGlobalFilterChange}
150169
variant="outlined"
151170
/>
@@ -164,7 +183,7 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
164183
color="primary"
165184
onClick={() => setShowAdditionalFilters(!shouldShowAdditionalFilters)}
166185
>
167-
All Filters
186+
{labels.allFilters}
168187
</Button>
169188
}
170189
</ButtonGroup>
@@ -176,7 +195,7 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
176195
<AppBar position="sticky">
177196
<Toolbar>
178197
<Box flex={1} paddingLeft={2}>
179-
<Typography variant="h3">All filters</Typography>
198+
<Typography variant="h3">{labels.allFiltersDrawerTitle}</Typography>
180199
</Box>
181200
<IconButton>
182201
<CloseIcon />
@@ -195,6 +214,12 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
195214
</Box>
196215
</Toolbar>
197216
))}
217+
<DataTableFilterChipBar
218+
columns={flatColumns}
219+
filters={filters}
220+
labels={labels}
221+
onRemove={onRemoveFilter}
222+
/>
198223
<div className={classes.tableWrapper}>
199224
<Table ref={ref} {...getTableProps()}>
200225
<TableHead className={classes.tableHead}>
@@ -273,11 +298,11 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
273298
}}
274299
/>
275300
</Box>
276-
<Typography component="span" variant="body2">{"of "}{pageCount}</Typography>
301+
<Typography component="span" variant="body2">{labels.pageOf({ count: pageCount })}</Typography>
277302
</Box>
278303
<Box flex={1} maxWidth={120}>
279304
<Select
280-
value={{ label: `${pageSize} rows`, value: pageSize }}
305+
value={{ label: labels.pageSizeSelect({ count: pageSize }), value: pageSize }}
281306
onChange={({ value }) => {
282307
setPageSize(value);
283308
}}
@@ -286,11 +311,11 @@ const DataTable = React.forwardRef(function DataTable(props, ref) {
286311
</Box>
287312
<Box flex={1} />
288313
<Button onClick={() => previousPage()} disabled={!canPreviousPage}>
289-
<ChevronLeftIcon /> Previous
314+
<ChevronLeftIcon /> {labels.previous}
290315
</Button>
291316
<Typography component="span">{" | "}</Typography>
292317
<Button onClick={() => nextPage()} disabled={!canNextPage}>
293-
Next <ChevronRightIcon />
318+
{labels.next} <ChevronRightIcon />
294319
</Button>
295320
</Toolbar>
296321
)}
@@ -393,6 +418,39 @@ DataTable.propTypes = {
393418
* Is set to true if the table rows are selectable
394419
*/
395420
isSelectable: PropTypes.bool,
421+
/**
422+
* Labels for various controls
423+
*/
424+
labels: PropTypes.shape({
425+
/**
426+
* The "All filters" button in table toolbar
427+
*/
428+
allFilters: PropTypes.string.isRequired,
429+
/**
430+
* Drawer title for all filters
431+
*/
432+
allFiltersDrawerTitle: PropTypes.string.isRequired,
433+
/**
434+
* Global filter text input label
435+
*/
436+
globalFilterPlaceholder: PropTypes.string.isRequired,
437+
/**
438+
* Next button label
439+
*/
440+
next: PropTypes.string.isRequired,
441+
/**
442+
* Function to generate the total number of pages ({ count }) => \`of ${count}\`,
443+
*/
444+
pageOf: PropTypes.func.isRequired,
445+
/**
446+
* Function to generate the label in select dropdown ({ count }) => \`${count} rows`,
447+
*/
448+
pageSizeSelect: PropTypes.func.isRequired,
449+
/**
450+
* Previous button label
451+
*/
452+
previous: PropTypes.string.isRequired
453+
}),
396454
/**
397455
* Go to next page
398456
*/
@@ -401,6 +459,10 @@ DataTable.propTypes = {
401459
* Event triggered when global filter field has changed
402460
*/
403461
onGlobalFilterChange: PropTypes.func,
462+
/**
463+
* Event triggered when a filter is removed with the `(key, multiSelectValueIfAvailable) => {}` signature.
464+
*/
465+
onRemoveFilter: PropTypes.func,
404466
/**
405467
* Event triggered when a row is clicked
406468
*/
@@ -450,6 +512,7 @@ DataTable.propTypes = {
450512
DataTable.defaultProps = {
451513
ToolbarComponent() { },
452514
PaginationComponent() { },
515+
labels: defaultLabels,
453516
pageSizes: [10, 20, 30, 40, 50]
454517
};
455518

0 commit comments

Comments
 (0)