Created
June 19, 2022 07:38
-
-
Save Manntrix/0f26f0aeca828a815670ba7588cb991a to your computer and use it in GitHub Desktop.
This file contains 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 { forwardRef, useEffect, useRef } from "react"; | |
import { useTable } from "react-table"; | |
const IndeterminateCheckbox = forwardRef(({ indeterminate, ...rest }, ref) => { | |
const defaultRef = useRef(); | |
const resolvedRef = ref || defaultRef; | |
useEffect(() => { | |
resolvedRef.current.indeterminate = indeterminate; | |
}, [resolvedRef, indeterminate]); | |
return ( | |
<div class="cb action"> | |
<label> | |
<input type="checkbox" ref={resolvedRef} {...rest} /> | |
<span>All</span> | |
</label> | |
</div> | |
); | |
}); | |
const Table = ({ columns, data }) => { | |
const { | |
getTableProps, | |
getTableBodyProps, | |
headerGroups, | |
rows, | |
prepareRow, | |
allColumns, | |
getToggleHideAllColumnsProps, | |
} = useTable({ | |
columns, | |
data, | |
}); | |
// Render the UI for your table | |
return ( | |
<> | |
<div> | |
<div> | |
<IndeterminateCheckbox {...getToggleHideAllColumnsProps()} /> | |
</div> | |
{/* Loop through columns data to create checkbox */} | |
{allColumns.map((column) => ( | |
<div class="cb action" key={column.id}> | |
<label> | |
<input type="checkbox" {...column.getToggleHiddenProps()} />{" "} | |
<span>{column.Header}</span> | |
</label> | |
</div> | |
))} | |
<br /> | |
</div> | |
{/* Table Start */} | |
<table {...getTableProps()}> | |
<thead> | |
{headerGroups.map((headerGroup) => ( | |
<tr {...headerGroup.getHeaderGroupProps()}> | |
{headerGroup.headers.map((column) => ( | |
<th {...column.getHeaderProps()}>{column.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> | |
{/* Table End */} | |
</> | |
); | |
}; | |
export default Table; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment