Skip to content

Commit

Permalink
Updated react-table filters todisplay column head as as placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Jordan Wright committed Oct 5, 2020
1 parent a787ed1 commit 7110e63
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/components/ItemTable/ReactTable.js
Original file line number Diff line number Diff line change
@@ -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 (
<TextField onChange={e => {
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
Expand All @@ -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 (

<TableContainer component={Paper}>
Expand All @@ -26,7 +51,10 @@ export default function ReactTable({ columns, data }) {
{headerGroups.map(headerGroup => (
<TableRow {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<TableCell {...column.getHeaderProps()}>{column.render("Header")}</TableCell>
<TableCell {...column.getHeaderProps()}>{column.render("Header")}

<div>{column.canFilter ? column.render('Filter') : null}</div> {/* This div renders the Filters based on if the table is set to allow filtering.*/}
</TableCell>
))}
</TableRow>
))}
Expand Down

0 comments on commit 7110e63

Please sign in to comment.