|
| 1 | +import { render } from 'preact' |
| 2 | +import { useReducer, useState } from 'preact/hooks' |
| 3 | +import { flexRender, tableFeatures, useTable } from '@tanstack/preact-table' |
| 4 | +import type { ColumnDef } from '@tanstack/preact-table' |
| 5 | +import './index.css' |
| 6 | + |
| 7 | +// This example uses the classic standalone `useTable` hook to create a table without the new `createTableHelper` util. |
| 8 | + |
| 9 | +// 1. Define what the shape of your data will be for each row |
| 10 | +type Person = { |
| 11 | + firstName: string |
| 12 | + lastName: string |
| 13 | + age: number |
| 14 | + visits: number |
| 15 | + status: string |
| 16 | + progress: number |
| 17 | +} |
| 18 | + |
| 19 | +// 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar) |
| 20 | +const defaultData: Array<Person> = [ |
| 21 | + { |
| 22 | + firstName: 'tanner', |
| 23 | + lastName: 'linsley', |
| 24 | + age: 24, |
| 25 | + visits: 100, |
| 26 | + status: 'In Relationship', |
| 27 | + progress: 50, |
| 28 | + }, |
| 29 | + { |
| 30 | + firstName: 'tandy', |
| 31 | + lastName: 'miller', |
| 32 | + age: 40, |
| 33 | + visits: 40, |
| 34 | + status: 'Single', |
| 35 | + progress: 80, |
| 36 | + }, |
| 37 | + { |
| 38 | + firstName: 'joe', |
| 39 | + lastName: 'dirte', |
| 40 | + age: 45, |
| 41 | + visits: 20, |
| 42 | + status: 'Complicated', |
| 43 | + progress: 10, |
| 44 | + }, |
| 45 | + { |
| 46 | + firstName: 'kevin', |
| 47 | + lastName: 'vandy', |
| 48 | + age: 12, |
| 49 | + visits: 100, |
| 50 | + status: 'Single', |
| 51 | + progress: 70, |
| 52 | + }, |
| 53 | +] |
| 54 | + |
| 55 | +// 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features |
| 56 | +const _features = tableFeatures({}) // util method to create sharable TFeatures object/type |
| 57 | + |
| 58 | +// 4. Define the columns for your table. This uses the new `ColumnDef` type to define columns. Alternatively, check out the createTableHelper/createColumnHelper util for an even more type-safe way to define columns. |
| 59 | +const columns: Array<ColumnDef<typeof _features, Person>> = [ |
| 60 | + { |
| 61 | + accessorKey: 'firstName', // accessorKey method (most common for simple use-cases) |
| 62 | + header: 'First Name', |
| 63 | + cell: (info) => info.getValue(), |
| 64 | + }, |
| 65 | + { |
| 66 | + accessorFn: (row) => row.lastName, // accessorFn used (alternative) along with a custom id |
| 67 | + id: 'lastName', |
| 68 | + header: () => <span>Last Name</span>, |
| 69 | + cell: (info) => <i>{info.getValue<string>()}</i>, |
| 70 | + }, |
| 71 | + { |
| 72 | + accessorFn: (row) => Number(row.age), // accessorFn used to transform the data |
| 73 | + id: 'age', |
| 74 | + header: () => 'Age', |
| 75 | + cell: (info) => { |
| 76 | + return info.renderValue() |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + accessorKey: 'visits', |
| 81 | + header: () => <span>Visits</span>, |
| 82 | + }, |
| 83 | + { |
| 84 | + accessorKey: 'status', |
| 85 | + header: 'Status', |
| 86 | + }, |
| 87 | + { |
| 88 | + accessorKey: 'progress', |
| 89 | + header: 'Profile Progress', |
| 90 | + }, |
| 91 | +] |
| 92 | + |
| 93 | +function App() { |
| 94 | + // 5. Store data with a stable reference |
| 95 | + const [data, _setData] = useState(() => [...defaultData]) |
| 96 | + const rerender = useReducer(() => ({}), {})[1] |
| 97 | + |
| 98 | + // 6. Create the table instance with required _features, columns, and data |
| 99 | + const table = useTable({ |
| 100 | + _features, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking) |
| 101 | + _rowModels: {}, // `Core` row model is now included by default, but you can still override it here |
| 102 | + columns, |
| 103 | + data, |
| 104 | + // add additional table options here |
| 105 | + }) |
| 106 | + |
| 107 | + // 7. Render your table markup from the table instance APIs |
| 108 | + return ( |
| 109 | + <div className="p-2"> |
| 110 | + <table> |
| 111 | + <thead> |
| 112 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 113 | + <tr key={headerGroup.id}> |
| 114 | + {headerGroup.headers.map((header) => ( |
| 115 | + <th key={header.id}> |
| 116 | + {header.isPlaceholder |
| 117 | + ? null |
| 118 | + : flexRender( |
| 119 | + header.column.columnDef.header, |
| 120 | + header.getContext(), |
| 121 | + )} |
| 122 | + </th> |
| 123 | + ))} |
| 124 | + </tr> |
| 125 | + ))} |
| 126 | + </thead> |
| 127 | + <tbody> |
| 128 | + {table.getRowModel().rows.map((row) => ( |
| 129 | + <tr key={row.id}> |
| 130 | + {row.getAllCells().map((cell) => ( |
| 131 | + <td key={cell.id}> |
| 132 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 133 | + </td> |
| 134 | + ))} |
| 135 | + </tr> |
| 136 | + ))} |
| 137 | + </tbody> |
| 138 | + <tfoot> |
| 139 | + {table.getFooterGroups().map((footerGroup) => ( |
| 140 | + <tr key={footerGroup.id}> |
| 141 | + {footerGroup.headers.map((header) => ( |
| 142 | + <th key={header.id}> |
| 143 | + {header.isPlaceholder |
| 144 | + ? null |
| 145 | + : flexRender( |
| 146 | + header.column.columnDef.footer, |
| 147 | + header.getContext(), |
| 148 | + )} |
| 149 | + </th> |
| 150 | + ))} |
| 151 | + </tr> |
| 152 | + ))} |
| 153 | + </tfoot> |
| 154 | + </table> |
| 155 | + <div className="h-4" /> |
| 156 | + <button onClick={() => rerender(0)} className="border p-2"> |
| 157 | + Rerender |
| 158 | + </button> |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +const rootElement = document.getElementById('root') |
| 164 | +if (!rootElement) throw new Error('Failed to find the root element') |
| 165 | + |
| 166 | +render(<App />, rootElement) |
0 commit comments