Created
July 23, 2020 07:51
-
-
Save ronihcohen/d312f72558777e19767f6696453bdcbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react" | |
import { | |
useTable, | |
useSortBy, | |
useGlobalFilter, | |
useAsyncDebounce, | |
} from "react-table" | |
function Table({ columns, data }) { | |
// Define a default UI for filtering | |
function GlobalFilter({ | |
preGlobalFilteredRows, | |
globalFilter, | |
setGlobalFilter, | |
}) { | |
const count = preGlobalFilteredRows.length | |
const [value, setValue] = React.useState(globalFilter) | |
const onChange = useAsyncDebounce(value => { | |
setGlobalFilter(value || undefined) | |
}, 200) | |
return ( | |
<span> | |
Search:{" "} | |
<input | |
value={value || ""} | |
onChange={e => { | |
setValue(e.target.value) | |
onChange(e.target.value) | |
}} | |
placeholder={`${count} records...`} | |
style={{ | |
fontSize: "1.1rem", | |
border: "0", | |
}} | |
/> | |
</span> | |
) | |
} | |
const defaultColumn = React.useMemo( | |
() => ({ | |
// Let's set up our default Filter UI | |
Filter: GlobalFilter, | |
}), | |
[] | |
) | |
const { | |
getTableProps, | |
getTableBodyProps, | |
headerGroups, | |
rows, | |
prepareRow, | |
state, | |
visibleColumns, | |
preGlobalFilteredRows, | |
setGlobalFilter, | |
} = useTable( | |
{ | |
columns, | |
data, | |
defaultColumn, // Be sure to pass the defaultColumn option | |
}, | |
useGlobalFilter, | |
useSortBy | |
) | |
return ( | |
<> | |
<table {...getTableProps()}> | |
<thead> | |
{headerGroups.map(headerGroup => ( | |
<tr {...headerGroup.getHeaderGroupProps()}> | |
{headerGroup.headers.map(column => ( | |
// Add the sorting props to control sorting. For this example | |
// we can add them into the header props | |
<th {...column.getHeaderProps(column.getSortByToggleProps())}> | |
{column.render("Header")} | |
{/* Add a sort direction indicator */} | |
<span> | |
{column.isSorted | |
? column.isSortedDesc | |
? " 🔽" | |
: " 🔼" | |
: ""} | |
</span> | |
</th> | |
))} | |
</tr> | |
))} | |
<tr> | |
<th | |
colSpan={visibleColumns.length} | |
style={{ | |
textAlign: "left", | |
}} | |
> | |
<GlobalFilter | |
preGlobalFilteredRows={preGlobalFilteredRows} | |
globalFilter={state.globalFilter} | |
setGlobalFilter={setGlobalFilter} | |
/> | |
</th> | |
</tr> | |
</thead> | |
<tbody {...getTableBodyProps()}> | |
{rows.map((row, i) => { | |
prepareRow(row) | |
return ( | |
<tr data-hash={row.original.hashCode} {...row.getRowProps()}> | |
{row.cells.map(cell => { | |
return <td {...cell.getCellProps()}>{cell.render("Cell")}</td> | |
})} | |
</tr> | |
) | |
})} | |
</tbody> | |
</table> | |
<br /> | |
</> | |
) | |
} | |
export default Table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment