Last active
November 15, 2023 19:53
-
-
Save tannerlinsley/c63cd35cdba2189f31d211ee2f2cafb3 to your computer and use it in GitHub Desktop.
A quick snippet of an early ReactTable v8 table that renders!
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 React from 'react' | |
import ReactDOM from 'react-dom' | |
import './index.css' | |
import { createTable } from 'react-table' | |
type Row = { | |
firstName: string | |
lastName: string | |
age: number | |
visits: number | |
status: string | |
progress: number | |
} | |
const defaultData: Row[] = [ | |
{ | |
firstName: 'tanner', | |
lastName: 'linsley', | |
age: 24, | |
visits: 100, | |
status: 'In Relationship', | |
progress: 50, | |
}, | |
{ | |
firstName: 'tandy', | |
lastName: 'miller', | |
age: 40, | |
visits: 40, | |
status: 'Single', | |
progress: 80, | |
}, | |
{ | |
firstName: 'joe', | |
lastName: 'dirte', | |
age: 45, | |
visits: 20, | |
status: 'Complicated', | |
progress: 10, | |
}, | |
] | |
let table = createTable() | |
.RowType<Row>() | |
const defaultColumns = table.createColumns([ | |
table.createGroup({ | |
header: 'Name', | |
footer: props => props.column.id, | |
columns: [ | |
table.createColumn('firstName', { | |
cell: info => info.value, | |
footer: props => props.column.id, | |
}), | |
table.createColumn(row => row.lastName, { | |
id: 'lastName', | |
cell: info => info.value, | |
header: <span>Last Name</span>, | |
footer: props => props.column.id, | |
}), | |
], | |
}), | |
table.createGroup({ | |
header: 'Info', | |
footer: props => props.column.id, | |
columns: [ | |
table.createColumn('age', { | |
header: () => 'Age', | |
footer: props => props.column.id, | |
}), | |
table.createGroup({ | |
header: 'More Info', | |
columns: [ | |
table.createColumn('visits', { | |
header: () => <span>Visits</span>, | |
footer: props => props.column.id, | |
}), | |
table.createColumn('status', { | |
header: 'Status', | |
footer: props => props.column.id, | |
}), | |
table.createColumn('progress', { | |
header: 'Profile Progress', | |
footer: props => props.column.id, | |
}), | |
], | |
}), | |
], | |
}), | |
]) | |
function App() { | |
const [data] = React.useState<Row[]>(() => [...defaultData]) | |
const [columns] = React.useState<typeof defaultColumns>(() => [ | |
...defaultColumns, | |
]) | |
const rerender = React.useReducer(() => ({}), {})[1] | |
const instance = table.useTable({ | |
data, | |
columns, | |
}) | |
return ( | |
<> | |
<table {...instance.getTableProps()}> | |
<thead> | |
{instance.getHeaderGroups().map(headerGroup => ( | |
<tr {...headerGroup.getHeaderGroupProps()}> | |
{headerGroup.headers.map(header => ( | |
<th {...header.getHeaderProps()}> | |
{header.isPlaceholder ? null : header.renderHeader()} | |
</th> | |
))} | |
</tr> | |
))} | |
</thead> | |
<tbody {...instance.getTableBodyProps()}> | |
{instance.getRows().map(row => ( | |
<tr {...row.getRowProps()}> | |
{row.getAllCells().map(cell => ( | |
<td {...cell.getCellProps()}>{cell.renderCell()}</td> | |
))} | |
</tr> | |
))} | |
</tbody> | |
<tfoot> | |
{instance.getFooterGroups().map(footerGroup => ( | |
<tr {...footerGroup.getFooterGroupProps()}> | |
{footerGroup.headers.map(header => ( | |
<th {...header.getFooterProps()}> | |
{header.isPlaceholder ? null : header.renderFooter()} | |
</th> | |
))} | |
</tr> | |
))} | |
</tfoot> | |
</table> | |
<button onClick={() => rerender()}>Rerender</button> | |
</> | |
) | |
} | |
ReactDOM.render( | |
<React.StrictMode> | |
<App /> | |
</React.StrictMode>, | |
document.getElementById('root') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is react-table v7.