From 7110e638e9bc2d4600e53ff110d092410f7a6c13 Mon Sep 17 00:00:00 2001 From: Tyler Jordan Wright Date: Mon, 5 Oct 2020 14:44:27 -0400 Subject: [PATCH] Updated react-table filters todisplay column head as as placeholder --- src/components/ItemTable/ReactTable.js | 40 ++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/src/components/ItemTable/ReactTable.js b/src/components/ItemTable/ReactTable.js index f4ca9c4..8cb5875 100644 --- a/src/components/ItemTable/ReactTable.js +++ b/src/components/ItemTable/ReactTable.js @@ -1,9 +1,30 @@ import { Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Paper, TextField } from "@material-ui/core"; import React from "react"; -import { useTable } from "react-table"; +import { useTable, useFilters, filterTypes, } from "react-table"; export default function ReactTable({ columns, data }) { // Use the useTable Hook to send the columns and data to build the table + function DefaultColumnFilter({ + column + }) { + + return ( + { + column.setFilter(e.target.value || undefined) + }} + label={column.Header} + /> + ) + + + } + + const defaultColumn = React.useMemo( + () => ({ + Filter: DefaultColumnFilter, + }), + [] + ); const { getTableProps, // table props from react-table getTableBodyProps, // table body props from react-table @@ -12,12 +33,16 @@ export default function ReactTable({ columns, data }) { prepareRow // Prepare the row (this function needs to be called for each row before getting the row props) } = useTable({ columns, - data - }); + data, + defaultColumn, + filterTypes, + }, + useFilters); + + + - - return ( @@ -26,7 +51,10 @@ export default function ReactTable({ columns, data }) { {headerGroups.map(headerGroup => ( {headerGroup.headers.map(column => ( - {column.render("Header")} + {column.render("Header")} + +
{column.canFilter ? column.render('Filter') : null}
{/* This div renders the Filters based on if the table is set to allow filtering.*/} +
))}
))}