@tanstack/angular-table 9.0.0-alpha.9 → 9.0.0-beta.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 +127 -0
- package/dist/README.md +127 -0
- package/dist/fesm2022/tanstack-angular-table-static-functions.mjs +6 -0
- package/dist/fesm2022/tanstack-angular-table-static-functions.mjs.map +1 -0
- package/dist/fesm2022/tanstack-angular-table.mjs +1337 -239
- package/dist/fesm2022/tanstack-angular-table.mjs.map +1 -1
- package/dist/types/tanstack-angular-table-static-functions.d.ts +1 -0
- package/dist/types/tanstack-angular-table.d.ts +793 -0
- package/package.json +39 -19
- package/skills/angular/angular-rendering-directives/SKILL.md +415 -0
- package/skills/angular/angular-rendering-directives/references/content-shapes.md +142 -0
- package/skills/angular/angular-rendering-directives/references/create-table-hook-registries.md +89 -0
- package/skills/angular/angular-rendering-directives/references/di-tokens.md +171 -0
- package/skills/angular/angular-rendering-directives/references/flex-render-component-options.md +64 -0
- package/skills/angular/client-to-server/SKILL.md +467 -0
- package/skills/angular/compose-with-tanstack-query/SKILL.md +482 -0
- package/skills/angular/compose-with-tanstack-store/SKILL.md +397 -0
- package/skills/angular/compose-with-tanstack-virtual/SKILL.md +400 -0
- package/skills/angular/getting-started/SKILL.md +496 -0
- package/skills/angular/getting-started/references/feature-row-model-mapping.md +48 -0
- package/skills/angular/migrate-v8-to-v9/SKILL.md +419 -0
- package/skills/angular/migrate-v8-to-v9/references/v8-to-v9-mapping.md +261 -0
- package/skills/angular/production-readiness/SKILL.md +469 -0
- package/skills/angular/table-state/SKILL.md +429 -0
- package/skills/angular/table-state/references/external-atoms-and-app-hook.md +152 -0
- package/src/flex-render/context.ts +14 -0
- package/src/flex-render/flags.ts +34 -0
- package/src/flex-render/flexRenderComponent.ts +288 -0
- package/src/flex-render/flexRenderComponentFactory.ts +251 -0
- package/src/flex-render/renderer.ts +393 -0
- package/src/flex-render/view.ts +226 -0
- package/src/flexRender.ts +124 -0
- package/src/helpers/cell.ts +104 -0
- package/src/helpers/createTableHook.ts +499 -0
- package/src/helpers/flexRenderCell.ts +136 -0
- package/src/helpers/header.ts +99 -0
- package/src/helpers/table.ts +85 -0
- package/src/index.ts +23 -70
- package/src/injectTable.ts +212 -0
- package/src/{lazy-signal-initializer.ts → lazySignalInitializer.ts} +2 -2
- package/src/reactivity.ts +105 -0
- package/static-functions/index.ts +1 -0
- package/static-functions/ng-package.json +6 -0
- package/dist/esm2022/flex-render.mjs +0 -148
- package/dist/esm2022/index.mjs +0 -48
- package/dist/esm2022/lazy-signal-initializer.mjs +0 -43
- package/dist/esm2022/proxy.mjs +0 -83
- package/dist/esm2022/tanstack-angular-table.mjs +0 -5
- package/dist/flex-render.d.ts +0 -30
- package/dist/index.d.ts +0 -5
- package/dist/lazy-signal-initializer.d.ts +0 -5
- package/dist/proxy.d.ts +0 -3
- package/src/flex-render.ts +0 -184
- package/src/proxy.ts +0 -97
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Directive, InjectionToken, inject, input } from '@angular/core'
|
|
2
|
+
import { CellData, Header, RowData, TableFeatures } from '@tanstack/table-core'
|
|
3
|
+
import type { Signal } from '@angular/core'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* DI context shape for a TanStack Table header.
|
|
7
|
+
*
|
|
8
|
+
* This exists to make the current `Header` injectable by any nested component/directive
|
|
9
|
+
* without passing it through inputs/props.
|
|
10
|
+
*/
|
|
11
|
+
export interface TanStackTableHeaderContext<
|
|
12
|
+
TFeatures extends TableFeatures,
|
|
13
|
+
TData extends RowData,
|
|
14
|
+
TValue extends CellData,
|
|
15
|
+
> {
|
|
16
|
+
/** Signal that returns the current header instance. */
|
|
17
|
+
header: Signal<Header<TFeatures, TData, TValue>>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Injection token that provides access to the current header.
|
|
22
|
+
*
|
|
23
|
+
* This token is provided by the {@link TanStackTableHeader} directive.
|
|
24
|
+
*/
|
|
25
|
+
export const TanStackTableHeaderToken = new InjectionToken<
|
|
26
|
+
TanStackTableHeaderContext<any, any, any>['header']
|
|
27
|
+
>('[TanStack Table] HeaderContext')
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Provides a TanStack Table `Header` instance in Angular DI.
|
|
31
|
+
*
|
|
32
|
+
* The header can be injected by:
|
|
33
|
+
* - any descendant of an element using `[tanStackTableHeader]="..."`
|
|
34
|
+
* - any component instantiated by `*flexRender` when the render props contains `header`
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```html
|
|
38
|
+
* <th [tanStackTableHeader]="header">
|
|
39
|
+
* <app-sort-indicator />
|
|
40
|
+
* </th>
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* @Component({
|
|
45
|
+
* selector: 'app-sort-indicator',
|
|
46
|
+
* template: `
|
|
47
|
+
* <button (click)="toggle()">
|
|
48
|
+
* {{ header().column.id }}
|
|
49
|
+
* </button>
|
|
50
|
+
* `,
|
|
51
|
+
* })
|
|
52
|
+
* export class SortIndicatorComponent {
|
|
53
|
+
* readonly header = injectTableHeaderContext()
|
|
54
|
+
*
|
|
55
|
+
* toggle() {
|
|
56
|
+
* this.header().column.toggleSorting()
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
@Directive({
|
|
62
|
+
selector: '[tanStackTableHeader]',
|
|
63
|
+
exportAs: 'header',
|
|
64
|
+
providers: [
|
|
65
|
+
{
|
|
66
|
+
provide: TanStackTableHeaderToken,
|
|
67
|
+
useFactory: () => inject(TanStackTableHeader).header,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
})
|
|
71
|
+
export class TanStackTableHeader<
|
|
72
|
+
TFeatures extends TableFeatures,
|
|
73
|
+
TData extends RowData,
|
|
74
|
+
TValue extends CellData,
|
|
75
|
+
> implements TanStackTableHeaderContext<TFeatures, TData, TValue> {
|
|
76
|
+
/**
|
|
77
|
+
* The current TanStack Table header.
|
|
78
|
+
*
|
|
79
|
+
* Provided as a required signal input so DI consumers always read the latest value.
|
|
80
|
+
*/
|
|
81
|
+
readonly header = input.required<Header<TFeatures, TData, TValue>>({
|
|
82
|
+
alias: 'tanStackTableHeader',
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Injects the current TanStack Table header signal.
|
|
88
|
+
*
|
|
89
|
+
* Available when:
|
|
90
|
+
* - there is a nearest `[tanStackTableHeader]` directive in the DI tree, or
|
|
91
|
+
* - the caller is rendered via `*flexRender` with render props containing `header`
|
|
92
|
+
*/
|
|
93
|
+
export function injectTableHeaderContext<
|
|
94
|
+
TFeatures extends TableFeatures,
|
|
95
|
+
TData extends RowData,
|
|
96
|
+
TValue extends CellData,
|
|
97
|
+
>(): TanStackTableHeaderContext<TFeatures, TData, TValue>['header'] {
|
|
98
|
+
return inject(TanStackTableHeaderToken)
|
|
99
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Directive, InjectionToken, inject, input } from '@angular/core'
|
|
2
|
+
import { RowData, TableFeatures } from '@tanstack/table-core'
|
|
3
|
+
import { AngularTable } from '../injectTable'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Injection token that provides access to the current {@link AngularTable} instance.
|
|
8
|
+
*
|
|
9
|
+
* This token is provided by the {@link TanStackTable} directive.
|
|
10
|
+
*/
|
|
11
|
+
export const TanStackTableToken = new InjectionToken<
|
|
12
|
+
Signal<AngularTable<any, any>>
|
|
13
|
+
>('[TanStack Table] Table Context')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Provides a TanStack Table instance (`AngularTable`) in Angular DI.
|
|
17
|
+
*
|
|
18
|
+
* The table can be injected by:
|
|
19
|
+
* - any descendant of an element using `[tanStackTable]="..."`
|
|
20
|
+
* - any component instantiated by `*flexRender` when the render props contains `table`
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```html
|
|
24
|
+
* <div [tanStackTable]="table">
|
|
25
|
+
* <app-pagination />
|
|
26
|
+
* </div>
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* @Component({
|
|
31
|
+
* selector: 'app-pagination',
|
|
32
|
+
* template: `
|
|
33
|
+
* <button (click)="prev()" [disabled]="!table().getCanPreviousPage()">Prev</button>
|
|
34
|
+
* <button (click)="next()" [disabled]="!table().getCanNextPage()">Next</button>
|
|
35
|
+
* `,
|
|
36
|
+
* })
|
|
37
|
+
* export class PaginationComponent {
|
|
38
|
+
* readonly table = injectTableContext()
|
|
39
|
+
*
|
|
40
|
+
* prev() {
|
|
41
|
+
* this.table().previousPage()
|
|
42
|
+
* }
|
|
43
|
+
* next() {
|
|
44
|
+
* this.table().nextPage()
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
@Directive({
|
|
50
|
+
selector: '[tanStackTable]',
|
|
51
|
+
exportAs: 'table',
|
|
52
|
+
providers: [
|
|
53
|
+
{
|
|
54
|
+
provide: TanStackTableToken,
|
|
55
|
+
useFactory: () => inject(TanStackTable).table,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
export class TanStackTable<
|
|
60
|
+
TFeatures extends TableFeatures,
|
|
61
|
+
TData extends RowData,
|
|
62
|
+
> {
|
|
63
|
+
/**
|
|
64
|
+
* The current TanStack Table instance.
|
|
65
|
+
*
|
|
66
|
+
* Provided as a required signal input so DI consumers always read the latest value.
|
|
67
|
+
*/
|
|
68
|
+
readonly table = input.required<AngularTable<TFeatures, TData>>({
|
|
69
|
+
alias: 'tanStackTable',
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Injects the current TanStack Table instance signal.
|
|
75
|
+
*
|
|
76
|
+
* Available when:
|
|
77
|
+
* - there is a nearest `[tanStackTable]` directive in the DI tree, or
|
|
78
|
+
* - the caller is rendered via `*flexRender` with render props containing `table`
|
|
79
|
+
*/
|
|
80
|
+
export function injectTableContext<
|
|
81
|
+
TFeatures extends TableFeatures,
|
|
82
|
+
TData extends RowData,
|
|
83
|
+
>(): Signal<AngularTable<TFeatures, TData>> {
|
|
84
|
+
return inject(TanStackTableToken)
|
|
85
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,73 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
RowData,
|
|
4
|
-
TableOptions,
|
|
5
|
-
TableOptionsResolved,
|
|
6
|
-
TableState,
|
|
7
|
-
_createTable,
|
|
8
|
-
type Table,
|
|
9
|
-
} from '@tanstack/table-core'
|
|
10
|
-
import { lazyInit } from './lazy-signal-initializer'
|
|
11
|
-
import { proxifyTable } from './proxy'
|
|
1
|
+
import { FlexRenderCell } from './helpers/flexRenderCell'
|
|
2
|
+
import { FlexRenderDirective } from './flexRender'
|
|
12
3
|
|
|
13
4
|
export * from '@tanstack/table-core'
|
|
14
5
|
|
|
15
|
-
export
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const state = signal<TableState>(table.initialState)
|
|
37
|
-
|
|
38
|
-
// Compose table options using computed.
|
|
39
|
-
// This is to allow `tableSignal` to listen and set table option
|
|
40
|
-
const updatedOptions = computed<TableOptionsResolved<TData>>(() => {
|
|
41
|
-
// listen to table state changed
|
|
42
|
-
const tableState = state()
|
|
43
|
-
// listen to input options changed
|
|
44
|
-
const tableOptions = options()
|
|
45
|
-
return {
|
|
46
|
-
...table.options,
|
|
47
|
-
...resolvedOptions,
|
|
48
|
-
...tableOptions,
|
|
49
|
-
state: { ...tableState, ...tableOptions.state },
|
|
50
|
-
onStateChange: updater => {
|
|
51
|
-
const value =
|
|
52
|
-
updater instanceof Function ? updater(tableState) : updater
|
|
53
|
-
state.set(value)
|
|
54
|
-
resolvedOptions.onStateChange?.(updater)
|
|
55
|
-
},
|
|
56
|
-
}
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
// convert table instance to signal for proxify to listen to any table state and options changes
|
|
60
|
-
const tableSignal = computed(
|
|
61
|
-
() => {
|
|
62
|
-
table.setOptions(updatedOptions())
|
|
63
|
-
return table
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
equal: () => false,
|
|
67
|
-
}
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
// proxify Table instance to provide ability for consumer to listen to any table state changes
|
|
71
|
-
return proxifyTable(tableSignal)
|
|
72
|
-
})
|
|
73
|
-
}
|
|
6
|
+
export * from './flexRender'
|
|
7
|
+
export * from './injectTable'
|
|
8
|
+
export * from './flex-render/flexRenderComponent'
|
|
9
|
+
|
|
10
|
+
export * from './helpers/cell'
|
|
11
|
+
export * from './helpers/header'
|
|
12
|
+
export * from './helpers/table'
|
|
13
|
+
export * from './helpers/createTableHook'
|
|
14
|
+
export * from './helpers/flexRenderCell'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Constant helper to import FlexRender directives.
|
|
18
|
+
*
|
|
19
|
+
* You should prefer to use this constant over importing the directives separately,
|
|
20
|
+
* as it ensures you always have the correct set of directives over library updates.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link FlexRenderDirective} and {@link FlexRenderCell} for more details on the directives included in this export.
|
|
23
|
+
*/
|
|
24
|
+
export const FlexRender = [FlexRenderDirective, FlexRenderCell] as const
|
|
25
|
+
|
|
26
|
+
export { shallow } from '@tanstack/angular-store'
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DestroyRef,
|
|
3
|
+
Injector,
|
|
4
|
+
NgZone,
|
|
5
|
+
assertInInjectionContext,
|
|
6
|
+
effect,
|
|
7
|
+
inject,
|
|
8
|
+
untracked,
|
|
9
|
+
} from '@angular/core'
|
|
10
|
+
import { constructTable } from '@tanstack/table-core'
|
|
11
|
+
import { lazyInit } from './lazySignalInitializer'
|
|
12
|
+
import { angularReactivity } from './reactivity'
|
|
13
|
+
import type {
|
|
14
|
+
RowData,
|
|
15
|
+
Table,
|
|
16
|
+
TableFeatures,
|
|
17
|
+
TableOptions,
|
|
18
|
+
TableState,
|
|
19
|
+
} from '@tanstack/table-core'
|
|
20
|
+
import type {
|
|
21
|
+
Atom,
|
|
22
|
+
ReadonlyAtom,
|
|
23
|
+
ReadonlyStore,
|
|
24
|
+
Store,
|
|
25
|
+
} from '@tanstack/angular-store'
|
|
26
|
+
|
|
27
|
+
export type SubscribeSource<TValue> =
|
|
28
|
+
| Atom<TValue>
|
|
29
|
+
| ReadonlyAtom<TValue>
|
|
30
|
+
| Store<TValue>
|
|
31
|
+
| ReadonlyStore<TValue>
|
|
32
|
+
|
|
33
|
+
export type AngularTable<
|
|
34
|
+
TFeatures extends TableFeatures,
|
|
35
|
+
TData extends RowData,
|
|
36
|
+
> = Table<TFeatures, TData> & {
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated Prefer `table.atoms.<slice>.get()` for template/render reads
|
|
39
|
+
* of a specific state slice, `table.state` for full-state debug snapshots, or
|
|
40
|
+
* Angular computed values around explicit selectors. `table.store.state` is a
|
|
41
|
+
* current-value snapshot and is easy to misuse in render code.
|
|
42
|
+
*/
|
|
43
|
+
readonly store: Table<TFeatures, TData>['store']
|
|
44
|
+
/**
|
|
45
|
+
* The current table state exposed as a flat proxy. Prefer
|
|
46
|
+
* `table.atoms.<slice>.get()` when reading a specific slice.
|
|
47
|
+
*/
|
|
48
|
+
readonly state: Readonly<TableState<TFeatures>>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function createStateProxy<
|
|
52
|
+
TFeatures extends TableFeatures,
|
|
53
|
+
TData extends RowData,
|
|
54
|
+
>(table: Table<TFeatures, TData>): Readonly<TableState<TFeatures>> {
|
|
55
|
+
const getSnapshot = () => {
|
|
56
|
+
const snapshot = {} as TableState<TFeatures>
|
|
57
|
+
const stateKeys = Object.keys(table.initialState) as Array<
|
|
58
|
+
string & keyof TableState<TFeatures>
|
|
59
|
+
>
|
|
60
|
+
|
|
61
|
+
for (const key of stateKeys) {
|
|
62
|
+
;(snapshot as Record<string, unknown>)[key] = table.atoms[key].get()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return snapshot
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const target = {} as TableState<TFeatures>
|
|
69
|
+
|
|
70
|
+
return new Proxy(target, {
|
|
71
|
+
get(target, prop, receiver) {
|
|
72
|
+
if (prop === 'toJSON') {
|
|
73
|
+
return getSnapshot
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof prop === 'string' && prop in table.initialState) {
|
|
77
|
+
return table.atoms[prop as keyof TableState<TFeatures>]?.get()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return Reflect.get(target, prop, receiver)
|
|
81
|
+
},
|
|
82
|
+
has(_, prop) {
|
|
83
|
+
return typeof prop === 'string' && prop in table.initialState
|
|
84
|
+
},
|
|
85
|
+
ownKeys() {
|
|
86
|
+
return Reflect.ownKeys(table.initialState)
|
|
87
|
+
},
|
|
88
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
89
|
+
if (typeof prop !== 'string' || !(prop in table.initialState)) {
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Creates and returns an Angular-reactive table instance.
|
|
103
|
+
*
|
|
104
|
+
* The initializer is intentionally re-evaluated whenever any signal read inside it changes.
|
|
105
|
+
* This is how the adapter keeps the table in sync with Angular's reactivity model.
|
|
106
|
+
*
|
|
107
|
+
* Because of that behavior, keep expensive/static values (for example `columns`, feature setup, row models)
|
|
108
|
+
* as stable references outside the initializer, and only read reactive state (`data()`, pagination/filter/sorting signals, etc.)
|
|
109
|
+
* inside it.
|
|
110
|
+
*
|
|
111
|
+
* 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(...)`.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* 1. Register the table features you need
|
|
115
|
+
* ```ts
|
|
116
|
+
* // Register only the features you need
|
|
117
|
+
* import {tableFeatures, rowPaginationFeature} from '@tanstack/angular-table';
|
|
118
|
+
* const features = tableFeatures({
|
|
119
|
+
* rowPaginationFeature,
|
|
120
|
+
* // ...all other features you need
|
|
121
|
+
* })
|
|
122
|
+
*
|
|
123
|
+
* // Use all table core features
|
|
124
|
+
* import {stockFeatures} from '@tanstack/angular-table';
|
|
125
|
+
* const features = tableFeatures(stockFeatures);
|
|
126
|
+
* ```
|
|
127
|
+
* 2. Prepare the table columns
|
|
128
|
+
* ```ts
|
|
129
|
+
* import {ColumnDef} from '@tanstack/angular-table';
|
|
130
|
+
*
|
|
131
|
+
* type MyData = {}
|
|
132
|
+
*
|
|
133
|
+
* const columns: ColumnDef<typeof features, MyData>[] = [
|
|
134
|
+
* // ...column definitions
|
|
135
|
+
* ]
|
|
136
|
+
*
|
|
137
|
+
* // or using createColumnHelper
|
|
138
|
+
* import {createColumnHelper} from '@tanstack/angular-table';
|
|
139
|
+
* const columnHelper = createColumnHelper<typeof features, MyData>();
|
|
140
|
+
* const columns = columnHelper.columns([
|
|
141
|
+
* columnHelper.accessor(...),
|
|
142
|
+
* // ...other columns
|
|
143
|
+
* ])
|
|
144
|
+
* ```
|
|
145
|
+
* 3. Create the table instance with `injectTable`
|
|
146
|
+
* ```ts
|
|
147
|
+
* const table = injectTable(() => {
|
|
148
|
+
* // ...table options,
|
|
149
|
+
* features,
|
|
150
|
+
* columns: columns,
|
|
151
|
+
* data: myDataSignal(),
|
|
152
|
+
* })
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @returns An Angular-reactive TanStack Table instance.
|
|
156
|
+
*/
|
|
157
|
+
export function injectTable<
|
|
158
|
+
TFeatures extends TableFeatures,
|
|
159
|
+
TData extends RowData,
|
|
160
|
+
>(
|
|
161
|
+
options: () => TableOptions<TFeatures, TData>,
|
|
162
|
+
): AngularTable<TFeatures, TData> {
|
|
163
|
+
assertInInjectionContext(injectTable)
|
|
164
|
+
const injector = inject(Injector)
|
|
165
|
+
const ngZone = inject(NgZone)
|
|
166
|
+
|
|
167
|
+
return ngZone.runOutsideAngular(() =>
|
|
168
|
+
lazyInit(() => {
|
|
169
|
+
const table = constructTable({
|
|
170
|
+
...options(),
|
|
171
|
+
features: {
|
|
172
|
+
coreReativityFeature: angularReactivity(injector),
|
|
173
|
+
...options().features,
|
|
174
|
+
},
|
|
175
|
+
}) as AngularTable<TFeatures, TData>
|
|
176
|
+
|
|
177
|
+
injector.get(DestroyRef).onDestroy(() => {
|
|
178
|
+
table._reactivity.unmount?.()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
const stateProxy = createStateProxy(table)
|
|
182
|
+
|
|
183
|
+
Object.defineProperty(table, 'state', {
|
|
184
|
+
get() {
|
|
185
|
+
return stateProxy
|
|
186
|
+
},
|
|
187
|
+
configurable: true,
|
|
188
|
+
enumerable: true,
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
let isMount = true
|
|
192
|
+
effect(
|
|
193
|
+
() => {
|
|
194
|
+
const newOptions = options()
|
|
195
|
+
if (isMount) {
|
|
196
|
+
isMount = false
|
|
197
|
+
return
|
|
198
|
+
}
|
|
199
|
+
untracked(() =>
|
|
200
|
+
table.setOptions((previous) => ({
|
|
201
|
+
...previous,
|
|
202
|
+
...newOptions,
|
|
203
|
+
})),
|
|
204
|
+
)
|
|
205
|
+
},
|
|
206
|
+
{ injector, debugName: 'tableOptionsUpdate' },
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
return table
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
}
|
|
@@ -2,7 +2,7 @@ import { untracked } from '@angular/core'
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Implementation from @tanstack/angular-query
|
|
5
|
-
* {
|
|
5
|
+
* {https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/util/lazy-init/lazy-init.ts}
|
|
6
6
|
*/
|
|
7
7
|
export function lazyInit<T extends object>(initializer: () => T): T {
|
|
8
8
|
let object: T | null = null
|
|
@@ -18,7 +18,7 @@ export function lazyInit<T extends object>(initializer: () => T): T {
|
|
|
18
18
|
const table = () => {}
|
|
19
19
|
|
|
20
20
|
return new Proxy<T>(table as T, {
|
|
21
|
-
apply(target: T, thisArg: any, argArray: any
|
|
21
|
+
apply(target: T, thisArg: any, argArray: Array<any>): any {
|
|
22
22
|
initializeObject()
|
|
23
23
|
if (typeof object === 'function') {
|
|
24
24
|
return Reflect.apply(object, thisArg, argArray)
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { NgZone, computed, signal, untracked } from '@angular/core'
|
|
2
|
+
import { toObservable } from '@angular/core/rxjs-interop'
|
|
3
|
+
import type { Injector, Signal, WritableSignal } from '@angular/core'
|
|
4
|
+
import type {
|
|
5
|
+
Atom,
|
|
6
|
+
Observer,
|
|
7
|
+
ReadonlyAtom,
|
|
8
|
+
Subscription,
|
|
9
|
+
} from '@tanstack/angular-store'
|
|
10
|
+
import type {
|
|
11
|
+
TableAtomOptions,
|
|
12
|
+
TableReactivityBindings,
|
|
13
|
+
} from '@tanstack/table-core/reactivity'
|
|
14
|
+
|
|
15
|
+
function signalToReadonlyAtom<T>(
|
|
16
|
+
signal: Signal<T>,
|
|
17
|
+
injector: Injector,
|
|
18
|
+
debugName?: string,
|
|
19
|
+
): ReadonlyAtom<T> {
|
|
20
|
+
const _signal = Object.assign(signal, {
|
|
21
|
+
get: () => signal(),
|
|
22
|
+
subscribe: (observer: Observer<T>) => {
|
|
23
|
+
return untracked(() =>
|
|
24
|
+
toObservable(computed(signal), { injector: injector }).subscribe(
|
|
25
|
+
observer,
|
|
26
|
+
),
|
|
27
|
+
)
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
if (debugName) {
|
|
31
|
+
_signal.toString = () => debugName
|
|
32
|
+
}
|
|
33
|
+
return _signal
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function signalToWritableAtom<T>(
|
|
37
|
+
signal: WritableSignal<T>,
|
|
38
|
+
injector: Injector,
|
|
39
|
+
debugName?: string,
|
|
40
|
+
): Atom<T> {
|
|
41
|
+
const _signal = Object.assign(signal.asReadonly(), {
|
|
42
|
+
set: (updater: T | ((prevVal: T) => T)) => {
|
|
43
|
+
typeof updater === 'function'
|
|
44
|
+
? signal.update(updater as (val: T) => T)
|
|
45
|
+
: signal.set(updater)
|
|
46
|
+
},
|
|
47
|
+
get: () => signal(),
|
|
48
|
+
subscribe: (observer: Observer<T>) => {
|
|
49
|
+
return untracked(() =>
|
|
50
|
+
toObservable(computed(signal), { injector: injector }).subscribe(
|
|
51
|
+
observer,
|
|
52
|
+
),
|
|
53
|
+
)
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
if (debugName) {
|
|
57
|
+
_signal.toString = () => debugName
|
|
58
|
+
}
|
|
59
|
+
return _signal
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Creates the table-core reactivity bindings used by the Angular adapter.
|
|
64
|
+
*
|
|
65
|
+
* Table state atoms are backed by TanStack Store atoms. The options store stays
|
|
66
|
+
* framework-native because row-model APIs read `table.options` directly during
|
|
67
|
+
* render. Readonly table atoms bridge Store dependency tracking into Angular
|
|
68
|
+
* computed signals.
|
|
69
|
+
*/
|
|
70
|
+
export function angularReactivity(injector: Injector): TableReactivityBindings {
|
|
71
|
+
const ngZone = injector.get(NgZone)
|
|
72
|
+
const subscriptions = new Set<Subscription>()
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
createOptionsStore: true,
|
|
76
|
+
wrapExternalAtoms: true,
|
|
77
|
+
addSubscription: (subscription) => {
|
|
78
|
+
subscriptions.add(subscription)
|
|
79
|
+
},
|
|
80
|
+
unmount: () => {
|
|
81
|
+
subscriptions.forEach((s) => s.unsubscribe())
|
|
82
|
+
subscriptions.clear()
|
|
83
|
+
},
|
|
84
|
+
schedule: (fn) => ngZone.runOutsideAngular(() => queueMicrotask(fn)),
|
|
85
|
+
createReadonlyAtom: <T>(fn: () => T, options?: TableAtomOptions<T>) => {
|
|
86
|
+
const signal = computed(() => fn(), {
|
|
87
|
+
equal: options?.compare,
|
|
88
|
+
debugName: options?.debugName,
|
|
89
|
+
})
|
|
90
|
+
return signalToReadonlyAtom(signal, injector, options?.debugName)
|
|
91
|
+
},
|
|
92
|
+
createWritableAtom: <T>(
|
|
93
|
+
value: T,
|
|
94
|
+
options?: TableAtomOptions<T>,
|
|
95
|
+
): Atom<T> => {
|
|
96
|
+
const writableSignal = signal(value, {
|
|
97
|
+
equal: options?.compare,
|
|
98
|
+
debugName: options?.debugName,
|
|
99
|
+
})
|
|
100
|
+
return signalToWritableAtom(writableSignal, injector, options?.debugName)
|
|
101
|
+
},
|
|
102
|
+
untrack: untracked,
|
|
103
|
+
batch: (fn) => fn(),
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/table-core/static-functions'
|