mirror of
https://github.com/gosticks/react-table.git
synced 2026-08-20 00:40:18 +00:00
The renderer function for headers, columns, cells, aggregates, filters, etc used to mix properties
from all of those contexts, including rows. Now thow contexts are located on their own reserved
properties, eg. `Cell: ({ cell: { value}, row, column, ...instance }) => value`
BREAKING CHANGE: The renderer function for headers, columns, cells, aggregates, filters, etc used
142 lines
3.1 KiB
JavaScript
142 lines
3.1 KiB
JavaScript
import React from 'react'
|
|
import { render, fireEvent } from '@testing-library/react'
|
|
import { useTable } from '../../hooks/useTable'
|
|
import { useSortBy } from '../useSortBy'
|
|
|
|
const data = [
|
|
{
|
|
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,
|
|
},
|
|
]
|
|
|
|
const defaultColumn = {
|
|
Cell: ({ cell: { value }, column: { id } }) => `${id}: ${value}`,
|
|
}
|
|
|
|
function Table({ columns, data }) {
|
|
const { getTableProps, headerGroups, rows, prepareRow } = useTable(
|
|
{
|
|
columns,
|
|
data,
|
|
defaultColumn,
|
|
},
|
|
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.sorted ? (column.sortedDesc ? ' 🔽' : ' 🔼') : ''}
|
|
</span>
|
|
</th>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</thead>
|
|
<tbody>
|
|
{rows.map(
|
|
(row, i) =>
|
|
prepareRow(row) || (
|
|
<tr {...row.getRowProps()}>
|
|
{row.cells.map(cell => {
|
|
return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
|
|
})}
|
|
</tr>
|
|
)
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
)
|
|
}
|
|
|
|
function App() {
|
|
const columns = React.useMemo(
|
|
() => [
|
|
{
|
|
Header: 'Name',
|
|
columns: [
|
|
{
|
|
Header: 'First Name',
|
|
accessor: 'firstName',
|
|
},
|
|
{
|
|
Header: 'Last Name',
|
|
accessor: 'lastName',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
Header: 'Info',
|
|
columns: [
|
|
{
|
|
Header: 'Age',
|
|
accessor: 'age',
|
|
},
|
|
{
|
|
Header: 'Visits',
|
|
accessor: 'visits',
|
|
},
|
|
{
|
|
Header: 'Status',
|
|
accessor: 'status',
|
|
},
|
|
{
|
|
Header: 'Profile Progress',
|
|
accessor: 'progress',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
[]
|
|
)
|
|
|
|
return <Table columns={columns} data={data} />
|
|
}
|
|
|
|
test('renders a sortable table', () => {
|
|
const { getByText, asFragment } = render(<App />)
|
|
|
|
const beforeSort = asFragment()
|
|
|
|
fireEvent.click(getByText('First Name'))
|
|
|
|
const afterSort1 = asFragment()
|
|
|
|
fireEvent.click(getByText('First Name'))
|
|
|
|
const afterSort2 = asFragment()
|
|
|
|
expect(beforeSort).toMatchDiffSnapshot(afterSort1)
|
|
expect(afterSort1).toMatchDiffSnapshot(afterSort2)
|
|
})
|