-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathinjectTable.ts
More file actions
194 lines (183 loc) · 5.66 KB
/
injectTable.ts
File metadata and controls
194 lines (183 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
Injector,
assertInInjectionContext,
computed,
effect,
inject,
signal,
untracked,
} from '@angular/core'
import {
constructReactivityFeature,
constructTable,
} from '@tanstack/table-core'
import { injectStore } from '@tanstack/angular-store'
import { lazyInit } from './lazySignalInitializer'
import type {
RowData,
Table,
TableFeatures,
TableOptions,
TableState,
} from '@tanstack/table-core'
import type { Signal, ValueEqualityFn } from '@angular/core'
export type AngularTable<
TFeatures extends TableFeatures,
TData extends RowData,
TSelected = TableState<TFeatures>,
> = Table<TFeatures, TData> & {
/**
* The selected state from the table store, based on the selector provided.
*/
readonly state: Signal<Readonly<TSelected>>
/**
* A signal that returns the entire table instance. Will update on table/options change.
*/
readonly value: Signal<AngularTable<TFeatures, TData, TSelected>>
/**
* Creates a computed that subscribe to changes in the table store with a custom selector.
* Default equality function is "shallow".
*/
computed: <TSubSelected = {}>(props: {
selector: (state: TableState<TFeatures>) => TSubSelected
equal?: ValueEqualityFn<TSubSelected>
}) => Signal<Readonly<TSubSelected>>
}
/**
* Creates and returns an Angular-reactive table instance.
*
* The initializer is intentionally re-evaluated whenever any signal read inside it changes.
* This is how the adapter keeps the table in sync with Angular's reactivity model.
*
* Because of that behavior, keep expensive/static values (for example `columns`, feature setup, row models)
* as stable references outside the initializer, and only read reactive state (`data()`, pagination/filter/sorting signals, etc.)
* inside it.
*
* The returned table is also signal-reactive: table state and table APIs are wired for Angular signals, so you can safely consume table methods inside `computed(...)` and `effect(...)`.
*
* @example
* 1. Register the table features you need
* ```ts
* // Register only the features you need
* import {tableFeatures, rowPaginationFeature} from '@tanstack/angular-table';
* const _features = tableFeatures({
* rowPaginationFeature,
* // ...all other features you need
* })
*
* // Use all table core features
* import {stockFeatures} from '@tanstack/angular-table';
* const _features = tableFeatures(stockFeatures);
* ```
* 2. Prepare the table columns
* ```ts
* import {ColumnDef} from '@tanstack/angular-table';
*
* type MyData = {}
*
* const columns: ColumnDef<typeof _features, MyData>[] = [
* // ...column definitions
* ]
*
* // or using createColumnHelper
* import {createColumnHelper} from '@tanstack/angular-table';
* const columnHelper = createColumnHelper<typeof _features, MyData>();
* const columns = columnHelper.columns([
* columnHelper.accessor(...),
* // ...other columns
* ])
* ```
* 3. Create the table instance with `injectTable`
* ```ts
* const table = injectTable(() => {
* // ...table options,
* _features,
* columns: columns,
* data: myDataSignal(),
* })
* ```
*
* @returns An Angular-reactive TanStack Table instance.
*/
export function injectTable<
TFeatures extends TableFeatures,
TData extends RowData,
TSelected = TableState<TFeatures>,
>(
options: () => TableOptions<TFeatures, TData>,
selector: (state: TableState<TFeatures>) => TSelected = (state) =>
state as TSelected,
): AngularTable<TFeatures, TData, TSelected> {
assertInInjectionContext(injectTable)
const injector = inject(Injector)
const stateNotifier = signal(0)
const angularReactivityFeature = constructReactivityFeature({
stateNotifier: () => stateNotifier(),
})
return lazyInit(() => {
const resolvedOptions: TableOptions<TFeatures, TData> = {
...options(),
_features: {
...options()._features,
angularReactivityFeature,
},
} as TableOptions<TFeatures, TData>
const table = constructTable(resolvedOptions) as AngularTable<
TFeatures,
TData,
TSelected
>
const tableState = injectStore(table.store, (state) => state, { injector })
const tableOptions = injectStore(table.optionsStore, (state) => state, {
injector,
})
const updatedOptions = computed<TableOptions<TFeatures, TData>>(() => {
const tableOptionsValue = options()
const result: TableOptions<TFeatures, TData> = {
...untracked(() => table.options),
...tableOptionsValue,
_features: { ...tableOptionsValue._features, angularReactivityFeature },
}
if (tableOptionsValue.state) {
result.state = tableOptionsValue.state
}
return result
})
effect(
() => {
const newOptions = updatedOptions()
untracked(() => table.setOptions(newOptions))
},
{ injector, debugName: 'tableOptionsUpdate' },
)
let isMount = true
effect(
() => {
void [tableOptions(), tableState()]
if (!isMount) untracked(() => stateNotifier.update((n) => n + 1))
isMount && (isMount = false)
},
{ injector, debugName: 'tableStateNotifier' },
)
table.computed = function Subscribe<TSubSelected = {}>(props: {
selector: (state: TableState<TFeatures>) => TSubSelected
equal?: ValueEqualityFn<TSubSelected>
}) {
return injectStore(table.store, props.selector, {
injector,
equal: props.equal,
})
}
Object.defineProperty(table, 'state', {
value: injectStore(table.store, selector, { injector }),
})
Object.defineProperty(table, 'value', {
value: computed(() => {
tableOptions()
tableState()
return table
}),
})
return table
})
}