import React from 'react' import { render, fireEvent } from '@testing-library/react' import { useTable } from '../../hooks/useTable' import { useRowSelect } from '../useRowSelect' import { useExpanded } from '../useExpanded' const dataPiece = [ { firstName: 'tanner', lastName: 'linsley', age: 29, visits: 100, status: 'In Relationship', progress: 50, }, { firstName: 'derek', lastName: 'perkins', age: 40, visits: 40, status: 'Single', progress: 80, }, { firstName: 'joe', lastName: 'bergevin', age: 45, visits: 20, status: 'Complicated', progress: 10, expanded: true, subRows: [ { firstName: 'bob', lastName: 'ross', age: 52, visits: 40, status: 'In Relationship', progress: 30, }, { firstName: 'john', lastName: 'smith', age: 21, visits: 30, status: 'Single', progress: 60, }, ], }, { firstName: 'jaylen', lastName: 'linsley', age: 26, visits: 99, status: 'In Relationship', progress: 70, }, ] const data = [ ...dataPiece, ...dataPiece, ...dataPiece, ...dataPiece, ...dataPiece, ...dataPiece, ] function Table({ columns, data }) { // Use the state and functions returned from useTable to build your UI const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow, state: { selectedRowIds }, } = useTable( { columns, data, }, useRowSelect, useExpanded, hooks => { hooks.flatColumns.push(columns => [ // Let's make a column for selection { id: 'selection', // The header can use the table's getToggleAllRowsSelectedProps method // to render a checkbox Header: ({ getToggleAllRowsSelectedProps }) => (
| {column.render('Header')} | ))}
|---|
| {cell.render('Cell')} | ) })}
Selected Rows: {Object.keys(selectedRowIds).length}
{JSON.stringify({ selectedRowIds: selectedRowIds }, null, 2)}
>
)
}
const IndeterminateCheckbox = React.forwardRef(
({ indeterminate, ...rest }, ref) => {
const defaultRef = React.useRef()
const resolvedRef = ref || defaultRef
React.useEffect(() => {
resolvedRef.current.indeterminate = indeterminate
}, [resolvedRef, indeterminate])
return
}
)
function App() {
const columns = React.useMemo(
() => [
{
id: 'selectedStatus',
Cell: ({ row }) => (