Skip to content

Commit 17bc739

Browse files
committed
Use a stable identifier reference to register devtools in table
1 parent b79d00d commit 17bc739

5 files changed

Lines changed: 46 additions & 35 deletions

File tree

packages/angular-table-devtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"src"
4545
],
4646
"dependencies": {
47-
"@tanstack/devtools-utils": "https://pkg.pr.new/TanStack/devtools/@tanstack/devtools-utils@368",
47+
"@tanstack/devtools-utils": "^0.5.0",
4848
"@tanstack/table-core": "workspace:*",
4949
"@tanstack/table-devtools": "workspace:*"
5050
},

packages/angular-table-devtools/src/injectTanStackTableDevtools.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ import {
55
import {
66
APP_ID,
77
DestroyRef,
8-
Injectable,
98
Injector,
109
assertInInjectionContext,
1110
effect,
1211
inject,
1312
} from '@angular/core'
14-
import type { RowData, Table, TableFeatures } from '@tanstack/table-core'
15-
16-
export interface InjectTanStackTableDevtoolsOptions {
17-
enabled?: () => boolean
18-
injector?: Injector
19-
}
13+
import type { Table } from '@tanstack/table-core'
2014

2115
function normalizeName(name?: string) {
2216
const trimmedName = name?.trim()
@@ -29,30 +23,32 @@ function generateId(): string {
2923
return `tanstacktable-${appId}_${autoId++}${Date.now().toString(36)}`
3024
}
3125

32-
export function injectTanStackTableDevtools<
33-
TFeatures extends TableFeatures = TableFeatures,
34-
TData extends RowData = RowData,
35-
>(
36-
table: () => Table<TFeatures, TData> | undefined,
37-
name?: string,
38-
options?: InjectTanStackTableDevtoolsOptions,
26+
export interface InjectTanStackTableDevtoolsOptions {
27+
table: Table<any, any> | undefined
28+
name: string
29+
enabled?: () => boolean
30+
injector?: Injector
31+
}
32+
33+
export function injectTanStackTableDevtools(
34+
options: () => InjectTanStackTableDevtoolsOptions,
3935
): void {
4036
const registrationId = generateId()
41-
const enabled = () => options?.enabled?.() ?? true
37+
const enabled = () => options().enabled?.() ?? true
4238
assertInInjectionContext(injectTanStackTableDevtools)
43-
const injector = options?.injector ?? inject(Injector)
39+
const injector = options().injector ?? inject(Injector)
4440
const destroyRef = inject(DestroyRef)
4541

4642
effect(
4743
(onCleanup) => {
44+
const { table, name } = options()
4845
const enabledValue = enabled()
49-
const tableValue = table()
50-
if (!enabledValue || !tableValue) {
46+
if (!enabledValue || !table) {
5147
removeTableDevtoolsTarget(registrationId)
5248
}
5349
upsertTableDevtoolsTarget({
5450
id: registrationId,
55-
table: tableValue,
51+
table: table,
5652
name: normalizeName(name),
5753
})
5854
onCleanup(() => {
@@ -67,11 +63,6 @@ export function injectTanStackTableDevtools<
6763
})
6864
}
6965

70-
export function injectTanStackTableDevtoolsNoOp<
71-
TFeatures extends TableFeatures = TableFeatures,
72-
TData extends RowData = RowData,
73-
>(
74-
_table: Table<TFeatures, TData> | undefined,
75-
_name?: string,
76-
_options?: InjectTanStackTableDevtoolsOptions,
66+
export function injectTanStackTableDevtoolsNoOp(
67+
options: () => InjectTanStackTableDevtoolsOptions,
7768
): void {}

packages/angular-table/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tanstack/angular-table",
3-
"version": "9.0.0-alpha.46",
3+
"version": "9.0.0-alpha.49",
44
"description": "Headless UI for building powerful tables & datagrids for Angular.",
55
"author": "Tanner Linsley",
66
"license": "MIT",

packages/react-table-devtools/src/useTanStackTableDevtools.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
removeTableDevtoolsTarget,
66
upsertTableDevtoolsTarget,
77
} from '@tanstack/table-devtools'
8+
import { useEffect } from 'react'
89
import type { RowData, Table, TableFeatures } from '@tanstack/table-core'
910

1011
export interface UseTanStackTableDevtoolsOptions {
@@ -25,24 +26,32 @@ export function useTanStackTableDevtools<
2526
options?: UseTanStackTableDevtoolsOptions,
2627
): void {
2728
const registrationId = React.useId()
29+
const normalizedName = normalizeName(name)
30+
31+
const instanceId =
32+
// instanceId from react table adapter (if it exists) allows for stable devtools registration even if the table instance changes
33+
(table as unknown as { instanceId?: string }).instanceId ||
34+
`${registrationId}${normalizedName ? `-${normalizedName}` : ``}`
35+
2836
const enabled = options?.enabled ?? true
2937

30-
React.useEffect(() => {
38+
useEffect(() => {
3139
if (!enabled || !table) {
32-
removeTableDevtoolsTarget(registrationId)
40+
removeTableDevtoolsTarget(instanceId)
3341
return
3442
}
3543

3644
upsertTableDevtoolsTarget({
37-
id: registrationId,
45+
id: instanceId,
3846
table,
39-
name: normalizeName(name),
47+
name: normalizedName,
4048
})
4149

4250
return () => {
43-
removeTableDevtoolsTarget(registrationId)
51+
removeTableDevtoolsTarget(instanceId)
4452
}
45-
}, [enabled, name, registrationId, table])
53+
// eslint-disable-next-line @eslint-react/exhaustive-deps,react-hooks/exhaustive-deps
54+
}, [enabled, registrationId, instanceId])
4655
}
4756

4857
export function useTanStackTableDevtoolsNoOp<

packages/react-table/src/useTable.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useMemo, useState } from 'react'
3+
import { useId, useMemo, useRef, useState } from 'react'
44
import { constructTable } from '@tanstack/table-core'
55
import { shallow, useSelector } from '@tanstack/react-store'
66
import { reactReactivity } from './reactivity'
@@ -23,6 +23,10 @@ export type ReactTable<
2323
TData extends RowData,
2424
TSelected = TableState<TFeatures>,
2525
> = Table<TFeatures, TData> & {
26+
/**
27+
* A stable id reference for table instance
28+
*/
29+
instanceId?: string
2630
/**
2731
* A React HOC (Higher Order Component) that allows you to subscribe to the table state.
2832
*
@@ -137,6 +141,12 @@ export function useTable<
137141
tableOptions: TableOptions<TFeatures, TData>,
138142
selector?: (state: TableState<TFeatures>) => TSelected,
139143
): ReactTable<TFeatures, TData, TSelected> {
144+
const instanceIdRef = useRef<string | undefined>(undefined)
145+
const id = useId()
146+
if (!instanceIdRef.current) {
147+
// eslint-disable-next-line @eslint-react/purity
148+
instanceIdRef.current = `${id}-${Math.random().toString(36).slice(2, 9)}`
149+
}
140150
const [table] = useState(() => {
141151
const tableInstance = constructTable({
142152
...tableOptions,
@@ -156,6 +166,7 @@ export function useTable<
156166
}) as ReactTable<TFeatures, TData, TSelected>['Subscribe']
157167

158168
tableInstance.FlexRender = FlexRender
169+
tableInstance.instanceId = id
159170

160171
return tableInstance
161172
})

0 commit comments

Comments
 (0)