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