Skip to content

Instantly share code, notes, and snippets.

@Manntrix
Created June 19, 2022 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manntrix/0f26f0aeca828a815670ba7588cb991a to your computer and use it in GitHub Desktop.
Save Manntrix/0f26f0aeca828a815670ba7588cb991a to your computer and use it in GitHub Desktop.
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