Last active
February 24, 2020 23:56
-
-
Save Blezzoh/9eaad75ce348c4586efdef11f2842f54 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 { Table } from "react-bootstrap"; | |
import { useTable, useSortBy } from "react-table"; | |
/** | |
* As in the previous versions, any react table needs colums where at the core we have a field Header, and accessor | |
* As in the previous versions, a react table has data that consist of an array of JSONs | |
*/ | |
const ReactTable = ({ columns, data }) => { | |
// you can get the react table functions by using the hook useStable | |
const { | |
getTableProps, | |
getTableBodyProps, | |
headerGroups, | |
rows, | |
prepareRow | |
} = useTable( | |
{ | |
columns, | |
data | |
}, | |
// hook for sorting | |
useSortBy | |
); | |
return ( | |
<Table bordered {...getTableProps()}> | |
<thead> | |
{headerGroups.map(headerGroup => ( | |
<tr {...headerGroup.getHeaderGroupProps()}> | |
{headerGroup.headers.map(column => { | |
// three new addition to column: isSorted, isSortedDesc, getSortByToggleProps | |
const { | |
render, | |
getHeaderProps, | |
isSorted, | |
isSortedDesc, | |
getSortByToggleProps | |
} = column; | |
const extraClass = isSorted | |
? isSortedDesc | |
? "desc" | |
: "asc" | |
: ""; | |
return ( | |
<th | |
className={extraClass} | |
// getHeaderProps now receives a function | |
{...getHeaderProps(getSortByToggleProps())} | |
> | |
{render("Header")} | |
</th> | |
); | |
})} | |
</tr> | |
))} | |
</thead> | |
<tbody {...getTableBodyProps()}> | |
{rows.map((row, i) => { | |
prepareRow(row); | |
return ( | |
<tr {...row.getRowProps()}> | |
{row.cells.map(cell => { | |
return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>; | |
})} | |
</tr> | |
); | |
})} | |
</tbody> | |
</Table> | |
); | |
}; | |
export default ReactTable; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment