import { createRoot, useState } from 'octane'
import {
createColumnHelper,
tableFeatures,
useTable,
} from '@tanstack/octane-table'
import './index.css'
type Person = {
firstName: string
lastName: string
age: number
visits: number
status: string
progress: number
}
const defaultData: Array<Person> = [
{ 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 },
{ firstName: 'kevin', lastName: 'vandy', age: 12, visits: 100, status: 'Single', progress: 70 },
]
const features = tableFeatures({})
const columnHelper = createColumnHelper<typeof features, Person>()
const columns = columnHelper.columns([
columnHelper.accessor('firstName', {
header: 'First Name',
cell: (info) => String(info.getValue()),
}),
columnHelper.accessor((row) => row.lastName, {
id: 'lastName',
header: () => <span>Last Name</span>,
cell: (info) => <i>{String(info.getValue())}</i>,
}),
columnHelper.accessor((row) => Number(row.age), {
id: 'age',
header: () => 'Age',
cell: (info) => String(info.renderValue()),
}),
columnHelper.accessor('visits', {
header: () => <span>Visits</span>,
}),
columnHelper.accessor('status', { header: 'Status' }),
columnHelper.accessor('progress', { header: 'Profile Progress' }),
])
function App() @{
const [data] = useState(() => [...defaultData])
const table = useTable(
{
debugTable: true,
features,
columns,
data,
},
(state) => state,
)
<div className="demo-root">
<table>
<thead>
@for (const headerGroup of table.getHeaderGroups(); key headerGroup.id) {
<tr>
@for (const header of headerGroup.headers; key header.id) {
<th>
{header.isPlaceholder ? null : <table.FlexRender header={header} />}
</th>
}
</tr>
}
</thead>
<tbody>
@for (const row of table.getRowModel().rows; key row.id) {
<tr>
@for (const cell of row.getAllCells(); key cell.id) {
<td><table.FlexRender cell={cell} /></td>
}
</tr>
}
</tbody>
<tfoot>
@for (const footerGroup of table.getFooterGroups(); key footerGroup.id) {
<tr>
@for (const header of footerGroup.headers; key header.id) {
<th>
{header.isPlaceholder ? null : <table.FlexRender footer={header} />}
</th>
}
</tr>
}
</tfoot>
</table>
<div className="spacer-md" />
</div>
}
const rootElement = document.getElementById('root')
if (!rootElement) throw new Error('Failed to find the root element')
createRoot(rootElement).render(App)