-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathindex.ts
More file actions
80 lines (71 loc) · 2.22 KB
/
index.ts
File metadata and controls
80 lines (71 loc) · 2.22 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
import { computed, type Signal, signal } from '@angular/core'
import {
RowData,
TableOptions,
TableOptionsResolved,
TableState,
createTable,
type Table,
} from '@tanstack/table-core'
import { lazyInit } from './lazy-signal-initializer'
import { proxifyTable } from './proxy'
export * from '@tanstack/table-core'
export {
type FlexRenderContent,
FlexRenderDirective,
FlexRenderDirective as FlexRender,
injectFlexRenderContext,
type FlexRenderComponentProps,
} from './flex-render'
export {
FlexRenderComponent,
flexRenderComponent,
} from './flex-render/flex-render-component'
export function createAngularTable<TData extends RowData>(
options: () => TableOptions<TData>,
existingTable?: Table<TData>
): Table<TData> & Signal<Table<TData>> {
return lazyInit(() => {
const resolvedOptions = {
state: {},
onStateChange: () => {},
renderFallbackValue: null,
...options(),
}
const table = existingTable ?? createTable(resolvedOptions)
// By default, manage table state here using the table's initial state
const state = signal<TableState>(table.initialState)
// Compose table options using computed.
// This is to allow `tableSignal` to listen and set table option
const updatedOptions = computed<TableOptionsResolved<TData>>(() => {
// listen to table state changed
const tableState = state()
// listen to input options changed
const tableOptions = options()
return {
...table.options,
...resolvedOptions,
...tableOptions,
state: { ...tableState, ...tableOptions.state },
onStateChange: updater => {
const value =
updater instanceof Function ? 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,
}
)
// proxify Table instance to provide ability for consumer to listen to any table state changes
return proxifyTable(tableSignal)
})
}