react-hook-tanstack-table 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -0
- package/package.json +1 -1
- package/src/hooks/useTable.ts +19 -5
- package/src/lib/hasTableArg.ts +1 -1
- package/README.org +0 -29
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# React Hook TanStack Table
|
|
2
|
+
|
|
3
|
+
[Rules of React](https://react.dev/reference/rules) respecting bindings for [TanStack Table](https://tanstack.com/table).
|
|
4
|
+
|
|
5
|
+
Because these hooks respect the Rules of React, they are compatible with the React Compiler, or just memoization in general. They are more performant than `@tanstack/react-table`'s API.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
Install this package and its peer dependencies:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
yarn install react-hook-tanstack-table @tanstack/table-core @tanstack/react-table
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`react-hook-tanstack-table` is a drop-in replacement for `@tanstack/react-table`.
|
|
16
|
+
|
|
17
|
+
**Use our `useReactTable` hook to create your table instance.**
|
|
18
|
+
|
|
19
|
+
You may use our `TableContext` and provide your `table` to it. Our hooks will find it through the context.
|
|
20
|
+
Alternatively, you can provide your `table` instance directly to the hooks.
|
|
21
|
+
|
|
22
|
+
## How it works
|
|
23
|
+
|
|
24
|
+
Our `useReactTable` hook creates a `table` instance, and hooks into its `onStateChanged` handler to listen to state changes.
|
|
25
|
+
|
|
26
|
+
Our other hooks subscribe to these changes using React's [`useSyncExternalStore`](https://react.dev/reference/react/useSyncExternalStore), and run the getters on the relevant part of TanStack Table's API. You can then select the parts of the state that you need.
|
|
27
|
+
|
|
28
|
+
## Hooks
|
|
29
|
+
|
|
30
|
+
### `useTable`
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { useTable } from 'react-hook-tanstack-table'
|
|
34
|
+
|
|
35
|
+
const Table = () => {
|
|
36
|
+
const tableState = useTable((table) => table.state)
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### `useColumn`
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { useColumn } from 'react-hook-tanstack-table'
|
|
44
|
+
|
|
45
|
+
const Column = ({ columnId }: { columnId: string }) => {
|
|
46
|
+
const canFilter = useColumn(columnId, (column) => column.canFilter)
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### `useHeader`
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { useHeader } from 'react-hook-tanstack-table'
|
|
54
|
+
|
|
55
|
+
const Header = ({ headerId }: { headerId: string }) => {
|
|
56
|
+
const isPlaceholder = useHeader(headerId, (header) => header.isPlaceholder)
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### `useRow`
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { useRow } from 'react-hook-tanstack-table'
|
|
64
|
+
|
|
65
|
+
const Row = ({ rowId }, { rowId: string }) => {
|
|
66
|
+
const canSelect = useRow(rowId, (row) => row.canSelect)
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `useCell`
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { useCell } from 'react-hook-tanstack-table'
|
|
74
|
+
|
|
75
|
+
const Cell = ({ column, rowId }: { columnId: string, rowId: string }) => {
|
|
76
|
+
const cellValue = useCell({ column: columnId, row: rowId }, (cell) => cell.value)
|
|
77
|
+
}
|
|
78
|
+
```
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "react-hook-tanstack-table",
|
|
3
3
|
"packageManager": "yarn@4.15.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.2",
|
|
6
6
|
"description": "Rules-of-React respecting bindings for TanStack Table.",
|
|
7
7
|
"author": "Xandor Schiefer <me@xandor.co.za>",
|
|
8
8
|
"license": "MIT",
|
package/src/hooks/useTable.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type {
|
|
|
10
10
|
import { identity } from "../lib/identity"
|
|
11
11
|
import { runGetters } from "../lib/runGetters"
|
|
12
12
|
import { isShallowEqual } from "../lib/isShallowEqual"
|
|
13
|
+
import { hasTableArg } from "../lib/hasTableArg"
|
|
13
14
|
|
|
14
15
|
import type { IsEqual, RunGetters } from "../types"
|
|
15
16
|
|
|
@@ -88,10 +89,23 @@ type Selector<TData extends RowData, Selection> = (
|
|
|
88
89
|
) => Selection
|
|
89
90
|
|
|
90
91
|
export const useTable = <TData extends RowData, Selection = TableValues<TData>>(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
...args:
|
|
93
|
+
| [
|
|
94
|
+
table: Table<TData> | undefined,
|
|
95
|
+
selector?: Selector<TData, Selection> | undefined,
|
|
96
|
+
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
97
|
+
]
|
|
98
|
+
| [
|
|
99
|
+
selector?: Selector<TData, Selection> | undefined,
|
|
100
|
+
isEqual?: IsEqual<NoInfer<Selection>> | undefined,
|
|
101
|
+
]
|
|
94
102
|
): Selection => {
|
|
103
|
+
const [
|
|
104
|
+
table,
|
|
105
|
+
selector = identity as never,
|
|
106
|
+
isEqual = isShallowEqual,
|
|
107
|
+
] = hasTableArg(args) ? args : [undefined, ...args]
|
|
108
|
+
|
|
95
109
|
const getSelection = useCallback(
|
|
96
110
|
(table: Table<TData>) => selector(getTableValues(table)),
|
|
97
111
|
[selector],
|
|
@@ -105,8 +119,8 @@ const tableHook = useTable
|
|
|
105
119
|
const tableHookʹ =
|
|
106
120
|
<TData extends RowData>(table?: Table<TData> | undefined) =>
|
|
107
121
|
<Selection = TableValues<TData>>(
|
|
108
|
-
selector
|
|
109
|
-
isEqual
|
|
122
|
+
selector?: Selector<TData, Selection> | undefined,
|
|
123
|
+
isEqual?: IsEqual<NoInfer<Selection>> | undefined
|
|
110
124
|
): Selection =>
|
|
111
125
|
tableHook(table, selector, isEqual)
|
|
112
126
|
|
package/src/lib/hasTableArg.ts
CHANGED
|
@@ -12,7 +12,7 @@ const isTable = <TData extends RowData>(
|
|
|
12
12
|
|
|
13
13
|
export const hasTableArg = <
|
|
14
14
|
TData extends RowData,
|
|
15
|
-
Rest extends [
|
|
15
|
+
Rest extends [unknown?, ...unknown[]],
|
|
16
16
|
>(
|
|
17
17
|
args: [Table<TData> | undefined, ...Rest] | Rest,
|
|
18
18
|
): args is [Table<TData> | undefined, ...Rest] =>
|
package/README.org
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
* React Hook TanStack Table
|
|
2
|
-
|
|
3
|
-
[[https://react.dev/reference/rules][Rules of React]] respecting bindings for [[https://tanstack.com/table][TanStack Table]].
|
|
4
|
-
|
|
5
|
-
** Development
|
|
6
|
-
|
|
7
|
-
- Install dependencies:
|
|
8
|
-
|
|
9
|
-
#+begin_src shell
|
|
10
|
-
yarn install
|
|
11
|
-
#+end_src
|
|
12
|
-
|
|
13
|
-
- Run the playground:
|
|
14
|
-
|
|
15
|
-
#+begin_src shell
|
|
16
|
-
yarn run play
|
|
17
|
-
#+end_src
|
|
18
|
-
|
|
19
|
-
- Run the unit tests:
|
|
20
|
-
|
|
21
|
-
#+begin_src shell
|
|
22
|
-
yarn run test
|
|
23
|
-
#+end_src
|
|
24
|
-
|
|
25
|
-
- Build the library:
|
|
26
|
-
|
|
27
|
-
#+begin_src shell
|
|
28
|
-
yarn run build
|
|
29
|
-
#+end_src
|