-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add preact integration for tanstack-table #6173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,196 @@ | ||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||
| title: Table State (Preact) Guide | ||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ## Table State (Preact) Guide | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| TanStack Table has a simple underlying internal state management system to store and manage the state of the table. It also lets you selectively pull out any state that you need to manage in your own state management. This guide will walk you through the different ways in which you can interact with and manage the state of the table. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ### Accessing Table State | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| You do not need to set up anything special in order for the table state to work. If you pass nothing into either `state`, `initialState`, or any of the `on[State]Change` table options, the table will manage its own state internally. You can access any part of this internal state by using the `table.getState()` table instance API. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| console.log(table.getState()) //access the entire internal state | ||||||||||||||||||||||||||||||||||
| console.log(table.getState().rowSelection) //access just the row selection state | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ### Custom Initial State | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| If all you need to do for certain states is customize their initial default values, you still do not need to manage any of the state yourself. You can simply set values in the `initialState` option of the table instance. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||
| initialState: { | ||||||||||||||||||||||||||||||||||
| columnOrder: ['age', 'firstName', 'lastName'], //customize the initial column order | ||||||||||||||||||||||||||||||||||
| columnVisibility: { | ||||||||||||||||||||||||||||||||||
| id: false //hide the id column by default | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| expanded: true, //expand all rows by default | ||||||||||||||||||||||||||||||||||
| sorting: [ | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| id: 'age', | ||||||||||||||||||||||||||||||||||
| desc: true //sort by age in descending order by default | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| > **Note**: Only specify each particular state in either `initialState` or `state`, but not both. If you pass in a particular state value to both `initialState` and `state`, the initialized state in `state` will overwrite any corresponding value in `initialState`. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ### Controlled State | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| If you need easy access to the table state in other areas of your application, TanStack Table makes it easy to control and manage any or all of the table state in your own state management system. You can do this by passing in your own state and state management functions to the `state` and `on[State]Change` table options. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #### Individual Controlled State | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| You can control just the state that you need easy access to. You do NOT have to control all of the table state if you do not need to. It is recommended to only control the state that you need on a case-by-case basis. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| In order to control a particular state, you need to both pass in the corresponding `state` value and the `on[State]Change` function to the table instance. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Let's take filtering, sorting, and pagination as an example in a "manual" server-side data fetching scenario. You can store the filtering, sorting, and pagination state in your own state management, but leave out any other state like column order, column visibility, etc. if your API does not care about those values. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| const [columnFilters, setColumnFilters] = useState([]) //no default filters | ||||||||||||||||||||||||||||||||||
| const [sorting, setSorting] = useState([{ | ||||||||||||||||||||||||||||||||||
| id: 'age', | ||||||||||||||||||||||||||||||||||
| desc: true, //sort by age in descending order by default | ||||||||||||||||||||||||||||||||||
| }]) | ||||||||||||||||||||||||||||||||||
| const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 15 }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| //Use our controlled state values to fetch data | ||||||||||||||||||||||||||||||||||
| const tableQuery = useQuery({ | ||||||||||||||||||||||||||||||||||
| queryKey: ['users', columnFilters, sorting, pagination], | ||||||||||||||||||||||||||||||||||
| queryFn: () => fetchUsers(columnFilters, sorting, pagination), | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data: tableQuery.data, | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| state: { | ||||||||||||||||||||||||||||||||||
| columnFilters, //pass controlled state back to the table (overrides internal state) | ||||||||||||||||||||||||||||||||||
| sorting, | ||||||||||||||||||||||||||||||||||
| pagination | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| onColumnFiltersChange: setColumnFilters, //hoist columnFilters state into our own state management | ||||||||||||||||||||||||||||||||||
| onSortingChange: setSorting, | ||||||||||||||||||||||||||||||||||
| onPaginationChange: setPagination, | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #### Fully Controlled State | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Alternatively, you can control the entire table state with the `onStateChange` table option. It will hoist out the entire table state into your own state management system. Be careful with this approach, as you might find that raising some frequently changing state values up a preact tree, like `columnSizingInfo` state`, might cause bad performance issues. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| A couple of more tricks may be needed to make this work. If you use the `onStateChange` table option, the initial values of the `state` must be populated with all of the relevant state values for all of the features that you want to use. You can either manually type out all of the initial state values, or use the `table.setOptions` API in a special way as shown below. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| //create a table instance with default state values | ||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||
| //... Note: `state` values are NOT passed in yet | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const [state, setState] = useState({ | ||||||||||||||||||||||||||||||||||
| ...table.initialState, //populate the initial state with all of the default state values from the table instance | ||||||||||||||||||||||||||||||||||
| pagination: { | ||||||||||||||||||||||||||||||||||
| pageIndex: 0, | ||||||||||||||||||||||||||||||||||
| pageSize: 15 //optionally customize the initial pagination state. | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| //Use the table.setOptions API to merge our fully controlled state onto the table instance | ||||||||||||||||||||||||||||||||||
| table.setOptions(prev => ({ | ||||||||||||||||||||||||||||||||||
| ...prev, //preserve any other options that we have set up above | ||||||||||||||||||||||||||||||||||
| state, //our fully controlled state overrides the internal state | ||||||||||||||||||||||||||||||||||
| onStateChange: setState //any state changes will be pushed up to our own state management | ||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ### On State Change Callbacks | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| So far, we have seen the `on[State]Change` and `onStateChange` table options work to "hoist" the table state changes into our own state management. However, there are a few things about using these options that you should be aware of. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #### 1. **State Change Callbacks MUST have their corresponding state value in the `state` option**. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Specifying an `on[State]Change` callback tells the table instance that this will be a controlled state. If you do not specify the corresponding `state` value, that state will be "frozen" with its initial value. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| const [sorting, setSorting] = useState([]) | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| state: { | ||||||||||||||||||||||||||||||||||
| sorting, //required because we are using `onSortingChange` | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| onSortingChange: setSorting, //makes the `state.sorting` controlled | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #### 2. **Updaters can either be raw values or callback functions**. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| The `on[State]Change` and `onStateChange` callbacks work exactly like the `setState` functions in React/Preact. The updater values can either be a new state value or a callback function that takes the previous state value and returns the new state value. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| What implications does this have? It means that if you want to add in some extra logic in any of the `on[State]Change` callbacks, you can do so, but you need to check whether or not the new incoming updater value is a function or value. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```jsx | ||||||||||||||||||||||||||||||||||
| const [sorting, setSorting] = useState([]) | ||||||||||||||||||||||||||||||||||
| const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 10 }) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const table = usePreactTable({ | ||||||||||||||||||||||||||||||||||
| columns, | ||||||||||||||||||||||||||||||||||
| data, | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| state: { | ||||||||||||||||||||||||||||||||||
| pagination, | ||||||||||||||||||||||||||||||||||
| sorting, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| //syntax 1 | ||||||||||||||||||||||||||||||||||
| onPaginationChange: (updater) => { | ||||||||||||||||||||||||||||||||||
| setPagination(old => { | ||||||||||||||||||||||||||||||||||
| const newPaginationValue = updater instanceof Function ? updater(old) : updater | ||||||||||||||||||||||||||||||||||
| //do something with the new pagination value | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| return newPaginationValue | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| //syntax 2 | ||||||||||||||||||||||||||||||||||
| onSortingChange: (updater) => { | ||||||||||||||||||||||||||||||||||
| const newSortingValue = updater instanceof Function ? updater(sorting) : updater | ||||||||||||||||||||||||||||||||||
| //do something with the new sorting value | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| setSorting(updater) //normal state update | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ### State Types | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| All complex states in TanStack Table have their own TypeScript types that you can import and use. This can be handy for ensuring that you are using the correct data structures and properties for the state values that you are controlling. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ```tsx | ||||||||||||||||||||||||||||||||||
| import { usePreactTable, type SortingState } from '@tanstack/preact-table' | ||||||||||||||||||||||||||||||||||
| //... | ||||||||||||||||||||||||||||||||||
| const [sorting, setSorting] = useState<SortingState[]>([ | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| id: 'age', //you should get autocomplete for the `id` and `desc` properties | ||||||||||||||||||||||||||||||||||
| desc: true, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+188
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect type usage:
Proposed fix-const [sorting, setSorting] = useState<SortingState[]>([
+const [sorting, setSorting] = useState<SortingState>([📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| title: Preact Table | ||
| --- | ||
|
|
||
| The `@tanstack/preact-table` adapter is a wrapper around the core table logic. Most of its job is related to managing state the "preact" way, providing types and the rendering implementation of cell/header/footer templates. | ||
|
|
||
| ## `usePreactTable` | ||
|
|
||
| Takes an `options` object and returns a table. | ||
|
|
||
| ```tsx | ||
| import { usePreactTable } from '@tanstack/preact-table' | ||
|
|
||
| function App() { | ||
| const table = usePreactTable(options) | ||
|
|
||
| // ...render your table | ||
| } | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { | ||
| "name": "@tanstack/preact-table", | ||
| "version": "8.21.3", | ||
| "description": "Headless UI for building powerful tables & datagrids for Preact.", | ||
| "author": "Tanner Linsley", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/TanStack/table.git", | ||
| "directory": "packages/preact-table" | ||
| }, | ||
| "homepage": "https://tanstack.com/table", | ||
| "funding": { | ||
| "type": "github", | ||
| "url": "https://github.com/sponsors/tannerlinsley" | ||
| }, | ||
| "keywords": [ | ||
| "preact", | ||
| "table", | ||
| "preact-table", | ||
| "datagrid" | ||
| ], | ||
| "type": "commonjs", | ||
| "module": "build/lib/index.esm.js", | ||
| "main": "build/lib/index.js", | ||
| "types": "build/lib/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./build/lib/index.d.ts", | ||
| "import": "./build/lib/index.mjs", | ||
| "default": "./build/lib/index.js" | ||
| }, | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "sideEffects": false, | ||
| "engines": { | ||
| "node": ">=12" | ||
| }, | ||
| "files": [ | ||
| "build/lib/*", | ||
| "build/umd/*", | ||
| "src" | ||
| ], | ||
| "scripts": { | ||
| "clean": "rimraf ./build", | ||
| "test:lib": "vitest", | ||
| "test:lib:dev": "pnpm test:lib --watch", | ||
| "test:types": "tsc --noEmit", | ||
| "build": "pnpm build:rollup && pnpm build:types", | ||
| "build:rollup": "rollup --config rollup.config.mjs", | ||
| "build:types": "tsc --emitDeclarationOnly" | ||
| }, | ||
| "dependencies": { | ||
| "@tanstack/table-core": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "preact": "^10.23.2" | ||
| }, | ||
| "peerDependencies": { | ||
| "preact": ">=10" | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: stray backtick in prose.
There's a mismatched backtick:
`columnSizingInfo` state`— the trailing backtick after "state" is erroneous.Proposed fix
More precisely, on line 96 fix:
📝 Committable suggestion
🧰 Tools
🪛 LanguageTool
[grammar] ~96-~96: Ensure spelling is correct
Context: ...e frequently changing state values up a preact tree, like
columnSizingInfostate`, m...(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)
🤖 Prompt for AI Agents