-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathinjectTable.ts
81 lines (72 loc) · 2.22 KB
/
injectTable.ts
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
import { computed, signal } from '@angular/core'
import type {
RowData,
Table,
TableFeatures,
TableOptions,
TableState,
} from '@tanstack/table-core'
import {
constructTable,
coreFeatures,
getInitialTableState,
isFunction,
} from '@tanstack/table-core'
import { lazyInit } from './lazy-signal-initializer'
import { angularReactivityFeature } from './angularReactivityFeature'
export function injectTable<
TFeatures extends TableFeatures,
TData extends RowData,
>(options: () => TableOptions<TFeatures, TData>): Table<TFeatures, TData> {
return lazyInit(() => {
const features = () => {
return {
...coreFeatures,
...options()._features,
angularReactivityFeature,
}
}
// By default, manage table state here using the table's initial state
const state = signal<TableState<TFeatures>>(
getInitialTableState(features(), options().initialState),
)
const resolvedOptions: TableOptions<TFeatures, TData> = {
...options(),
_features: features(),
state: { ...state(), ...options().state },
} as TableOptions<TFeatures, TData>
const table = constructTable(resolvedOptions)
// Compose table options using computed.
// This is to allow `tableSignal` to listen and set table option
const updatedOptions = computed<TableOptions<TFeatures, TData>>(() => {
// listen to table state changed
const tableState = state()
// listen to input options changed
const tableOptions = options()
return {
...table.options,
...resolvedOptions,
...tableOptions,
_features: features(),
state: { ...tableState, ...tableOptions.state },
onStateChange: (updater) => {
const value = isFunction(updater) ? updater(tableState) : updater
state.set(value)
resolvedOptions.onStateChange?.(updater)
},
}
})
// convert table instance to signal for proxify to listen to any table state and options changes
const tableSignal = computed(
() => {
table.setOptions(updatedOptions())
return table
},
{
equal: () => false,
},
)
table._setRootNotifier(tableSignal as any)
return table
})
}