@tanstack/angular-table 9.0.0-alpha.9 → 9.0.0-beta.1

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.
Files changed (54) hide show
  1. package/README.md +127 -0
  2. package/dist/README.md +127 -0
  3. package/dist/fesm2022/tanstack-angular-table-static-functions.mjs +6 -0
  4. package/dist/fesm2022/tanstack-angular-table-static-functions.mjs.map +1 -0
  5. package/dist/fesm2022/tanstack-angular-table.mjs +1336 -239
  6. package/dist/fesm2022/tanstack-angular-table.mjs.map +1 -1
  7. package/dist/types/tanstack-angular-table-static-functions.d.ts +1 -0
  8. package/dist/types/tanstack-angular-table.d.ts +793 -0
  9. package/package.json +39 -19
  10. package/skills/angular/angular-rendering-directives/SKILL.md +415 -0
  11. package/skills/angular/angular-rendering-directives/references/content-shapes.md +142 -0
  12. package/skills/angular/angular-rendering-directives/references/create-table-hook-registries.md +89 -0
  13. package/skills/angular/angular-rendering-directives/references/di-tokens.md +171 -0
  14. package/skills/angular/angular-rendering-directives/references/flex-render-component-options.md +64 -0
  15. package/skills/angular/client-to-server/SKILL.md +467 -0
  16. package/skills/angular/compose-with-tanstack-query/SKILL.md +482 -0
  17. package/skills/angular/compose-with-tanstack-store/SKILL.md +397 -0
  18. package/skills/angular/compose-with-tanstack-virtual/SKILL.md +400 -0
  19. package/skills/angular/getting-started/SKILL.md +496 -0
  20. package/skills/angular/getting-started/references/feature-row-model-mapping.md +48 -0
  21. package/skills/angular/migrate-v8-to-v9/SKILL.md +419 -0
  22. package/skills/angular/migrate-v8-to-v9/references/v8-to-v9-mapping.md +261 -0
  23. package/skills/angular/production-readiness/SKILL.md +469 -0
  24. package/skills/angular/table-state/SKILL.md +429 -0
  25. package/skills/angular/table-state/references/external-atoms-and-app-hook.md +152 -0
  26. package/src/flex-render/context.ts +14 -0
  27. package/src/flex-render/flags.ts +34 -0
  28. package/src/flex-render/flexRenderComponent.ts +288 -0
  29. package/src/flex-render/flexRenderComponentFactory.ts +251 -0
  30. package/src/flex-render/renderer.ts +393 -0
  31. package/src/flex-render/view.ts +226 -0
  32. package/src/flexRender.ts +124 -0
  33. package/src/helpers/cell.ts +104 -0
  34. package/src/helpers/createTableHook.ts +499 -0
  35. package/src/helpers/flexRenderCell.ts +136 -0
  36. package/src/helpers/header.ts +99 -0
  37. package/src/helpers/table.ts +85 -0
  38. package/src/index.ts +23 -70
  39. package/src/injectTable.ts +212 -0
  40. package/src/{lazy-signal-initializer.ts → lazySignalInitializer.ts} +2 -2
  41. package/src/reactivity.ts +105 -0
  42. package/static-functions/index.ts +1 -0
  43. package/static-functions/ng-package.json +6 -0
  44. package/dist/esm2022/flex-render.mjs +0 -148
  45. package/dist/esm2022/index.mjs +0 -48
  46. package/dist/esm2022/lazy-signal-initializer.mjs +0 -43
  47. package/dist/esm2022/proxy.mjs +0 -83
  48. package/dist/esm2022/tanstack-angular-table.mjs +0 -5
  49. package/dist/flex-render.d.ts +0 -30
  50. package/dist/index.d.ts +0 -5
  51. package/dist/lazy-signal-initializer.d.ts +0 -5
  52. package/dist/proxy.d.ts +0 -3
  53. package/src/flex-render.ts +0 -184
  54. package/src/proxy.ts +0 -97
@@ -1,11 +1,1083 @@
1
1
  import * as i0 from '@angular/core';
2
- import { untracked, computed, inject, Injector, ComponentRef, ChangeDetectorRef, TemplateRef, Type, isSignal, ViewContainerRef, Directive, Inject, Input, InjectionToken, signal } from '@angular/core';
3
- import { _createTable } from '@tanstack/table-core';
2
+ import { InjectionToken, input, inject, Directive, reflectComponentType, Injectable, KeyValueDiffers, ChangeDetectorRef, OutputEmitterRef, TemplateRef, Type, runInInjectionContext, computed, effect, untracked, Injector, ViewContainerRef, DestroyRef, NgZone, signal, assertInInjectionContext } from '@angular/core';
3
+ import { constructTable, createColumnHelper } from '@tanstack/table-core';
4
4
  export * from '@tanstack/table-core';
5
+ import { toObservable } from '@angular/core/rxjs-interop';
6
+ export { shallow } from '@tanstack/angular-store';
7
+
8
+ /**
9
+ * Injection token that provides access to the current cell.
10
+ *
11
+ * This token is provided by the {@link TanStackTableCell} directive.
12
+ */
13
+ const TanStackTableCellToken = new InjectionToken('[TanStack Table] CellContext');
14
+ /**
15
+ * Provides a TanStack Table `Cell` instance in Angular DI.
16
+ *
17
+ * The cell can be injected by:
18
+ * - any descendant of an element using `[tanStackTableCell]="..."`
19
+ * - any component instantiated by `*flexRender` when the render props contains `cell`
20
+ *
21
+ * @example
22
+ * Inject from the nearest `[tanStackTableCell]`:
23
+ * ```html
24
+ * <td [tanStackTableCell]="cell">
25
+ * <app-cell-actions />
26
+ * </td>
27
+ * ```
28
+ *
29
+ * ```ts
30
+ * @Component({
31
+ * selector: 'app-cell-actions',
32
+ * template: `{{ cell().id }}`,
33
+ * })
34
+ * export class CellActionsComponent {
35
+ * readonly cell = injectTableCellContext()
36
+ * }
37
+ * ```
38
+ *
39
+ * @example
40
+ * Inject inside a component rendered via `flexRender`:
41
+ * ```ts
42
+ * @Component({
43
+ * selector: 'app-price-cell',
44
+ * template: `{{ cell().getValue() }}`,
45
+ * })
46
+ * export class PriceCellComponent {
47
+ * readonly cell = injectTableCellContext()
48
+ * }
49
+ * ```
50
+ */
51
+ class TanStackTableCell {
52
+ /**
53
+ * The current TanStack Table cell.
54
+ *
55
+ * Provided as a required signal input so DI consumers always read the latest value.
56
+ */
57
+ cell = input.required({ ...(ngDevMode ? { debugName: "cell" } : /* istanbul ignore next */ {}), alias: 'tanStackTableCell' });
58
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTableCell, deps: [], target: i0.ɵɵFactoryTarget.Directive });
59
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.0", type: TanStackTableCell, isStandalone: true, selector: "[tanStackTableCell]", inputs: { cell: { classPropertyName: "cell", publicName: "tanStackTableCell", isSignal: true, isRequired: true, transformFunction: null } }, providers: [
60
+ {
61
+ provide: TanStackTableCellToken,
62
+ useFactory: () => inject(TanStackTableCell).cell,
63
+ },
64
+ ], exportAs: ["cell"], ngImport: i0 });
65
+ }
66
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTableCell, decorators: [{
67
+ type: Directive,
68
+ args: [{
69
+ selector: '[tanStackTableCell]',
70
+ exportAs: 'cell',
71
+ providers: [
72
+ {
73
+ provide: TanStackTableCellToken,
74
+ useFactory: () => inject(TanStackTableCell).cell,
75
+ },
76
+ ],
77
+ }]
78
+ }], propDecorators: { cell: [{ type: i0.Input, args: [{ isSignal: true, alias: "tanStackTableCell", required: true }] }] } });
79
+ /**
80
+ * Injects the current TanStack Table cell signal.
81
+ *
82
+ * Available when:
83
+ * - there is a nearest `[tanStackTableCell]` directive in the DI tree, or
84
+ * - the caller is rendered via `*flexRender` with render props containing `cell`
85
+ */
86
+ function injectTableCellContext() {
87
+ return inject(TanStackTableCellToken);
88
+ }
89
+
90
+ /**
91
+ * Injection token that provides access to the current header.
92
+ *
93
+ * This token is provided by the {@link TanStackTableHeader} directive.
94
+ */
95
+ const TanStackTableHeaderToken = new InjectionToken('[TanStack Table] HeaderContext');
96
+ /**
97
+ * Provides a TanStack Table `Header` instance in Angular DI.
98
+ *
99
+ * The header can be injected by:
100
+ * - any descendant of an element using `[tanStackTableHeader]="..."`
101
+ * - any component instantiated by `*flexRender` when the render props contains `header`
102
+ *
103
+ * @example
104
+ * ```html
105
+ * <th [tanStackTableHeader]="header">
106
+ * <app-sort-indicator />
107
+ * </th>
108
+ * ```
109
+ *
110
+ * ```ts
111
+ * @Component({
112
+ * selector: 'app-sort-indicator',
113
+ * template: `
114
+ * <button (click)="toggle()">
115
+ * {{ header().column.id }}
116
+ * </button>
117
+ * `,
118
+ * })
119
+ * export class SortIndicatorComponent {
120
+ * readonly header = injectTableHeaderContext()
121
+ *
122
+ * toggle() {
123
+ * this.header().column.toggleSorting()
124
+ * }
125
+ * }
126
+ * ```
127
+ */
128
+ class TanStackTableHeader {
129
+ /**
130
+ * The current TanStack Table header.
131
+ *
132
+ * Provided as a required signal input so DI consumers always read the latest value.
133
+ */
134
+ header = input.required({ ...(ngDevMode ? { debugName: "header" } : /* istanbul ignore next */ {}), alias: 'tanStackTableHeader' });
135
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTableHeader, deps: [], target: i0.ɵɵFactoryTarget.Directive });
136
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.0", type: TanStackTableHeader, isStandalone: true, selector: "[tanStackTableHeader]", inputs: { header: { classPropertyName: "header", publicName: "tanStackTableHeader", isSignal: true, isRequired: true, transformFunction: null } }, providers: [
137
+ {
138
+ provide: TanStackTableHeaderToken,
139
+ useFactory: () => inject(TanStackTableHeader).header,
140
+ },
141
+ ], exportAs: ["header"], ngImport: i0 });
142
+ }
143
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTableHeader, decorators: [{
144
+ type: Directive,
145
+ args: [{
146
+ selector: '[tanStackTableHeader]',
147
+ exportAs: 'header',
148
+ providers: [
149
+ {
150
+ provide: TanStackTableHeaderToken,
151
+ useFactory: () => inject(TanStackTableHeader).header,
152
+ },
153
+ ],
154
+ }]
155
+ }], propDecorators: { header: [{ type: i0.Input, args: [{ isSignal: true, alias: "tanStackTableHeader", required: true }] }] } });
156
+ /**
157
+ * Injects the current TanStack Table header signal.
158
+ *
159
+ * Available when:
160
+ * - there is a nearest `[tanStackTableHeader]` directive in the DI tree, or
161
+ * - the caller is rendered via `*flexRender` with render props containing `header`
162
+ */
163
+ function injectTableHeaderContext() {
164
+ return inject(TanStackTableHeaderToken);
165
+ }
166
+
167
+ /**
168
+ * Injection token that provides access to the current {@link AngularTable} instance.
169
+ *
170
+ * This token is provided by the {@link TanStackTable} directive.
171
+ */
172
+ const TanStackTableToken = new InjectionToken('[TanStack Table] Table Context');
173
+ /**
174
+ * Provides a TanStack Table instance (`AngularTable`) in Angular DI.
175
+ *
176
+ * The table can be injected by:
177
+ * - any descendant of an element using `[tanStackTable]="..."`
178
+ * - any component instantiated by `*flexRender` when the render props contains `table`
179
+ *
180
+ * @example
181
+ * ```html
182
+ * <div [tanStackTable]="table">
183
+ * <app-pagination />
184
+ * </div>
185
+ * ```
186
+ *
187
+ * ```ts
188
+ * @Component({
189
+ * selector: 'app-pagination',
190
+ * template: `
191
+ * <button (click)="prev()" [disabled]="!table().getCanPreviousPage()">Prev</button>
192
+ * <button (click)="next()" [disabled]="!table().getCanNextPage()">Next</button>
193
+ * `,
194
+ * })
195
+ * export class PaginationComponent {
196
+ * readonly table = injectTableContext()
197
+ *
198
+ * prev() {
199
+ * this.table().previousPage()
200
+ * }
201
+ * next() {
202
+ * this.table().nextPage()
203
+ * }
204
+ * }
205
+ * ```
206
+ */
207
+ class TanStackTable {
208
+ /**
209
+ * The current TanStack Table instance.
210
+ *
211
+ * Provided as a required signal input so DI consumers always read the latest value.
212
+ */
213
+ table = input.required({ ...(ngDevMode ? { debugName: "table" } : /* istanbul ignore next */ {}), alias: 'tanStackTable' });
214
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTable, deps: [], target: i0.ɵɵFactoryTarget.Directive });
215
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.0", type: TanStackTable, isStandalone: true, selector: "[tanStackTable]", inputs: { table: { classPropertyName: "table", publicName: "tanStackTable", isSignal: true, isRequired: true, transformFunction: null } }, providers: [
216
+ {
217
+ provide: TanStackTableToken,
218
+ useFactory: () => inject(TanStackTable).table,
219
+ },
220
+ ], exportAs: ["table"], ngImport: i0 });
221
+ }
222
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: TanStackTable, decorators: [{
223
+ type: Directive,
224
+ args: [{
225
+ selector: '[tanStackTable]',
226
+ exportAs: 'table',
227
+ providers: [
228
+ {
229
+ provide: TanStackTableToken,
230
+ useFactory: () => inject(TanStackTable).table,
231
+ },
232
+ ],
233
+ }]
234
+ }], propDecorators: { table: [{ type: i0.Input, args: [{ isSignal: true, alias: "tanStackTable", required: true }] }] } });
235
+ /**
236
+ * Injects the current TanStack Table instance signal.
237
+ *
238
+ * Available when:
239
+ * - there is a nearest `[tanStackTable]` directive in the DI tree, or
240
+ * - the caller is rendered via `*flexRender` with render props containing `table`
241
+ */
242
+ function injectTableContext() {
243
+ return inject(TanStackTableToken);
244
+ }
245
+
246
+ const FlexRenderComponentProps = new InjectionToken('[@tanstack/angular-table] Flex render component context props');
247
+ /**
248
+ * Inject the flex render context props.
249
+ *
250
+ * Can be used in components rendered via FlexRender directives.
251
+ */
252
+ function injectFlexRenderContext() {
253
+ return inject(FlexRenderComponentProps);
254
+ }
255
+
256
+ /**
257
+ * Flags used to manage and optimize the rendering lifecycle of the content of the cell
258
+ * while using {@link FlexViewRenderer}.
259
+ */
260
+ const FlexRenderFlags = {
261
+ /**
262
+ * Indicates that the view is being created for the first time or will be cleared during the next update phase.
263
+ * This is the initial state and will transition after the first ngDoCheck.
264
+ */
265
+ ViewFirstRender: 1 << 0,
266
+ /**
267
+ * Indicates the `content` property has been modified or the view requires a complete re-render.
268
+ * When this flag is enabled, the view will be cleared and recreated from scratch.
269
+ */
270
+ ContentChanged: 1 << 1,
271
+ /**
272
+ * Indicates that the `props` property reference has changed.
273
+ * When this flag is enabled, the view context is updated based on the type of the content.
274
+ *
275
+ * For Component view, inputs will be updated and view will be marked as dirty.
276
+ * For TemplateRef and primitive values, view will be marked as dirty
277
+ */
278
+ PropsReferenceChanged: 1 << 2,
279
+ /**
280
+ * Indicates that the current rendered view needs to be checked for changes.
281
+ * This will be set to true when `content(props)` result has changed or during
282
+ * forced update
283
+ */
284
+ Dirty: 1 << 3,
285
+ /**
286
+ * Indicates that the first render effect has been checked at least one time.
287
+ */
288
+ RenderEffectChecked: 1 << 4,
289
+ };
290
+
291
+ /**
292
+ * Helper function to create a {@link FlexRenderComponent} instance, with better type-safety.
293
+ *
294
+ * @example
295
+ * ```ts
296
+ * import {flexRenderComponent} from '@tanstack/angular-table'
297
+ * import {inputBinding, outputBinding} from '@angular/core';
298
+ *
299
+ * const columns = [
300
+ * {
301
+ * cell: ({ row }) => {
302
+ * return flexRenderComponent(MyComponent, {
303
+ * inputs: { value: mySignalValue() },
304
+ * outputs: { valueChange: (val) => {} }
305
+ * // or using angular native createComponent#binding api
306
+ * bindings: [
307
+ * inputBinding('value', mySignalValue),
308
+ * outputBinding('valueChange', value => {
309
+ * console.log("my value changed to", value)
310
+ * })
311
+ * ]
312
+ * })
313
+ * },
314
+ * },
315
+ * ]
316
+ * ```
317
+ */
318
+ function flexRenderComponent(component, options) {
319
+ const { inputs, injector, outputs, directives, bindings } = options ?? {};
320
+ return new FlexRenderComponentInstance(component, inputs, injector, outputs, directives, bindings);
321
+ }
322
+ /**
323
+ * Wrapper class for a component that will be used as content for {@link FlexRenderDirective}
324
+ *
325
+ * Prefer {@link flexRenderComponent} helper for better type-safety
326
+ */
327
+ class FlexRenderComponentInstance {
328
+ component;
329
+ inputs;
330
+ injector;
331
+ outputs;
332
+ directives;
333
+ bindings;
334
+ mirror;
335
+ allowedInputNames = [];
336
+ allowedOutputNames = [];
337
+ constructor(component, inputs, injector, outputs, directives, bindings) {
338
+ this.component = component;
339
+ this.inputs = inputs;
340
+ this.injector = injector;
341
+ this.outputs = outputs;
342
+ this.directives = directives;
343
+ this.bindings = bindings;
344
+ const mirror = reflectComponentType(component);
345
+ if (!mirror) {
346
+ throw new Error(`[@tanstack-table/angular] The provided symbol is not a component`);
347
+ }
348
+ this.mirror = mirror;
349
+ for (const input of this.mirror.inputs) {
350
+ this.allowedInputNames.push(input.propName);
351
+ }
352
+ for (const output of this.mirror.outputs) {
353
+ this.allowedOutputNames.push(output.propName);
354
+ }
355
+ }
356
+ }
357
+
358
+ /**
359
+ * Creates and manages Angular component instances used by flex-rendered table
360
+ * content.
361
+ */
362
+ class FlexRenderComponentFactory {
363
+ #viewContainerRef;
364
+ constructor(viewContainerRef) {
365
+ this.#viewContainerRef = viewContainerRef;
366
+ }
367
+ createComponent(flexRenderComponent, componentInjector) {
368
+ const componentRef = this.#viewContainerRef.createComponent(flexRenderComponent.component, {
369
+ injector: componentInjector,
370
+ directives: flexRenderComponent.directives,
371
+ bindings: flexRenderComponent.bindings ?? [],
372
+ });
373
+ const view = new FlexRenderComponentRef(componentRef, flexRenderComponent, componentInjector);
374
+ const { inputs, outputs } = flexRenderComponent;
375
+ if (inputs)
376
+ view.setInputs(inputs);
377
+ if (outputs)
378
+ view.setOutputs(outputs);
379
+ return view;
380
+ }
381
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderComponentFactory, deps: [{ token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Injectable });
382
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderComponentFactory });
383
+ }
384
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderComponentFactory, decorators: [{
385
+ type: Injectable
386
+ }], ctorParameters: () => [{ type: i0.ViewContainerRef }] });
387
+ /**
388
+ * Runtime wrapper around an Angular component rendered by `FlexRenderDirective`.
389
+ *
390
+ * It diffs inputs and outputs across table updates so component renderers can
391
+ * be reused instead of recreated on every cell/header render.
392
+ */
393
+ class FlexRenderComponentRef {
394
+ componentRef;
395
+ componentInjector;
396
+ #keyValueDiffersFactory;
397
+ #componentData;
398
+ #inputValueDiffer;
399
+ #outputRegistry;
400
+ constructor(componentRef, componentData, componentInjector) {
401
+ this.componentRef = componentRef;
402
+ this.componentInjector = componentInjector;
403
+ this.#componentData = componentData;
404
+ this.#keyValueDiffersFactory = componentInjector.get(KeyValueDiffers);
405
+ this.#outputRegistry = new FlexRenderComponentOutputManager(this.#keyValueDiffersFactory, this.outputs);
406
+ this.#inputValueDiffer = this.#keyValueDiffersFactory
407
+ .find(this.inputs)
408
+ .create();
409
+ this.#inputValueDiffer.diff(this.inputs);
410
+ this.componentRef.onDestroy(() => this.#outputRegistry.unsubscribeAll());
411
+ }
412
+ get component() {
413
+ return this.#componentData.component;
414
+ }
415
+ get inputs() {
416
+ return this.#componentData.inputs ?? {};
417
+ }
418
+ get outputs() {
419
+ return this.#componentData.outputs ?? {};
420
+ }
421
+ /**
422
+ * Get component input and output diff by the given item
423
+ */
424
+ diff(item) {
425
+ return {
426
+ inputDiff: this.#inputValueDiffer.diff(item.inputs ?? {}),
427
+ outputDiff: this.#outputRegistry.diff(item.outputs ?? {}),
428
+ };
429
+ }
430
+ /**
431
+ *
432
+ * @param compare Whether the current ref component instance is the same as the given one
433
+ */
434
+ eqType(compare) {
435
+ return compare.component === this.component;
436
+ }
437
+ /**
438
+ * Tries to update current component refs input by the new given content component.
439
+ */
440
+ update(content) {
441
+ const eq = this.eqType(content);
442
+ if (!eq)
443
+ return;
444
+ const { inputDiff, outputDiff } = this.diff(content);
445
+ if (inputDiff) {
446
+ inputDiff.forEachAddedItem((item) => this.setInput(item.key, item.currentValue));
447
+ inputDiff.forEachChangedItem((item) => this.setInput(item.key, item.currentValue));
448
+ inputDiff.forEachRemovedItem((item) => this.setInput(item.key, undefined));
449
+ }
450
+ if (outputDiff) {
451
+ outputDiff.forEachAddedItem((item) => {
452
+ this.setOutput(item.key, item.currentValue);
453
+ });
454
+ outputDiff.forEachChangedItem((item) => {
455
+ if (item.currentValue) {
456
+ this.#outputRegistry.setListener(item.key, item.currentValue);
457
+ }
458
+ else {
459
+ this.#outputRegistry.unsubscribe(item.key);
460
+ }
461
+ });
462
+ outputDiff.forEachRemovedItem((item) => {
463
+ this.#outputRegistry.unsubscribe(item.key);
464
+ });
465
+ }
466
+ this.#componentData = content;
467
+ }
468
+ markAsDirty() {
469
+ this.componentRef.injector.get(ChangeDetectorRef).markForCheck();
470
+ }
471
+ setInputs(inputs) {
472
+ for (const prop in inputs) {
473
+ this.setInput(prop, inputs[prop]);
474
+ }
475
+ }
476
+ setInput(key, value) {
477
+ if (this.#componentData.allowedInputNames.includes(key)) {
478
+ this.componentRef.setInput(key, value);
479
+ }
480
+ }
481
+ setOutputs(outputs) {
482
+ this.#outputRegistry.unsubscribeAll();
483
+ for (const prop in outputs) {
484
+ this.setOutput(prop, outputs[prop]);
485
+ }
486
+ }
487
+ setOutput(outputName, emit) {
488
+ if (!this.#componentData.allowedOutputNames.includes(outputName))
489
+ return;
490
+ if (!emit) {
491
+ this.#outputRegistry.unsubscribe(outputName);
492
+ return;
493
+ }
494
+ const hasListener = this.#outputRegistry.hasListener(outputName);
495
+ this.#outputRegistry.setListener(outputName, emit);
496
+ if (hasListener) {
497
+ return;
498
+ }
499
+ const instance = this.componentRef.instance;
500
+ const output = instance[outputName];
501
+ if (output && output instanceof OutputEmitterRef) {
502
+ output.subscribe((value) => {
503
+ this.#outputRegistry.getListener(outputName)?.(value);
504
+ });
505
+ }
506
+ }
507
+ }
508
+ class FlexRenderComponentOutputManager {
509
+ #outputSubscribers = {};
510
+ #outputListeners = {};
511
+ #valueDiffer;
512
+ constructor(keyValueDiffers, initialOutputs) {
513
+ this.#valueDiffer = keyValueDiffers.find(initialOutputs).create();
514
+ if (initialOutputs) {
515
+ this.#valueDiffer.diff(initialOutputs);
516
+ }
517
+ }
518
+ hasListener(outputName) {
519
+ return outputName in this.#outputListeners;
520
+ }
521
+ setListener(outputName, callback) {
522
+ this.#outputListeners[outputName] = callback;
523
+ }
524
+ getListener(outputName) {
525
+ return this.#outputListeners[outputName];
526
+ }
527
+ unsubscribeAll() {
528
+ for (const prop in this.#outputSubscribers) {
529
+ this.unsubscribe(prop);
530
+ }
531
+ }
532
+ unsubscribe(outputName) {
533
+ if (outputName in this.#outputSubscribers) {
534
+ this.#outputSubscribers[outputName]?.unsubscribe();
535
+ delete this.#outputSubscribers[outputName];
536
+ delete this.#outputListeners[outputName];
537
+ }
538
+ }
539
+ diff(outputs) {
540
+ return this.#valueDiffer.diff(outputs);
541
+ }
542
+ }
543
+
544
+ /**
545
+ * Normalizes arbitrary Angular flex-render content into the renderer's internal
546
+ * tagged representation.
547
+ *
548
+ * This lets the directive decide whether to reuse, update, or recreate an
549
+ * embedded view or component view.
550
+ */
551
+ function mapToFlexRenderTypedContent(content) {
552
+ if (content === null || content === undefined) {
553
+ return { kind: 'null' };
554
+ }
555
+ if (typeof content === 'string' || typeof content === 'number') {
556
+ return { kind: 'primitive', content };
557
+ }
558
+ if (content instanceof FlexRenderComponentInstance) {
559
+ return { kind: 'flexRenderComponent', content };
560
+ }
561
+ else if (content instanceof TemplateRef) {
562
+ return { kind: 'templateRef', content };
563
+ }
564
+ else if (content instanceof Type) {
565
+ return { kind: 'component', content };
566
+ }
567
+ else {
568
+ return { kind: 'primitive', content };
569
+ }
570
+ }
571
+ class FlexRenderView {
572
+ view;
573
+ #previousContent;
574
+ #content;
575
+ constructor(initialContent, view) {
576
+ this.#content = initialContent;
577
+ this.view = view;
578
+ }
579
+ get previousContent() {
580
+ return this.#previousContent ?? { kind: 'null' };
581
+ }
582
+ get content() {
583
+ return this.#content;
584
+ }
585
+ set content(content) {
586
+ this.#previousContent = this.#content;
587
+ this.#content = content;
588
+ }
589
+ }
590
+ /**
591
+ * Tracks an Angular embedded template view rendered by `FlexRenderDirective`.
592
+ *
593
+ * Template views receive updated props through their proxied context and can be
594
+ * reused while the rendered content kind stays compatible.
595
+ */
596
+ class FlexRenderTemplateView extends FlexRenderView {
597
+ constructor(initialContent, view) {
598
+ super(initialContent, view);
599
+ }
600
+ updateProps(props) {
601
+ this.view.markForCheck();
602
+ }
603
+ dirtyCheck() {
604
+ // Basically a no-op. When the view is created via EmbeddedViewRef, we don't need to do any manual update
605
+ // since this type of content has a proxy as a context, then every time the root component is checked for changes,
606
+ // the property getter will be re-evaluated.
607
+ //
608
+ // If in a future we need to manually mark the view as dirty, just uncomment next line
609
+ // this.view.markForCheck()
610
+ }
611
+ unmount() {
612
+ this.view.destroy();
613
+ }
614
+ onDestroy(callback) {
615
+ this.view.onDestroy(callback);
616
+ }
617
+ eq(compare) {
618
+ return ((this.content.kind === 'primitive' &&
619
+ compare.kind === 'primitive' &&
620
+ this.content.content === compare.content) ||
621
+ (this.content.kind === 'templateRef' &&
622
+ compare.kind === 'templateRef' &&
623
+ this.content.content === compare.content));
624
+ }
625
+ }
626
+ /**
627
+ * Tracks an Angular component view rendered by `FlexRenderDirective`.
628
+ *
629
+ * Component views own input/output updates for `flexRenderComponent(...)`
630
+ * results and component classes rendered directly from column definitions.
631
+ */
632
+ class FlexRenderComponentView extends FlexRenderView {
633
+ constructor(initialContent, view) {
634
+ super(initialContent, view);
635
+ }
636
+ updateProps(props) {
637
+ switch (this.content.kind) {
638
+ case 'component': {
639
+ this.view.setInputs(props);
640
+ break;
641
+ }
642
+ case 'flexRenderComponent': {
643
+ // No-op. When FlexRenderFlags.PropsReferenceChanged is set,
644
+ // FlexRenderComponent will be updated into `dirtyCheck`.
645
+ break;
646
+ }
647
+ }
648
+ }
649
+ dirtyCheck() {
650
+ switch (this.content.kind) {
651
+ case 'component': {
652
+ // Component context is currently valuated with the cell context. Since it's reference
653
+ // shouldn't change, we force mark the component as dirty in order to re-evaluate function invocation in view.
654
+ // NOTE: this should behave like having a component with ChangeDetectionStrategy.Default
655
+ this.view.markAsDirty();
656
+ break;
657
+ }
658
+ case 'flexRenderComponent': {
659
+ // Given context instance will always have a different reference than the previous one,
660
+ // so instead of recreating the entire view, we will only update the current view
661
+ if (this.view.eqType(this.content.content)) {
662
+ this.view.update(this.content.content);
663
+ }
664
+ this.view.markAsDirty();
665
+ break;
666
+ }
667
+ }
668
+ }
669
+ unmount() {
670
+ this.view.componentRef.destroy();
671
+ }
672
+ onDestroy(callback) {
673
+ this.view.componentRef.onDestroy(callback);
674
+ }
675
+ eq(compare) {
676
+ return ((this.content.kind === 'component' &&
677
+ compare.kind === 'component' &&
678
+ this.content.content === compare.content) ||
679
+ (this.content.kind === 'flexRenderComponent' &&
680
+ compare.kind === 'flexRenderComponent' &&
681
+ this.content.content.component === compare.content.component));
682
+ }
683
+ }
684
+
685
+ /**
686
+ * Internal view renderer used by Angular TanStack Table to implement `flexRender` directives.
687
+ *
688
+ * @internal Use FlexRender directives instead.
689
+ */
690
+ class FlexViewRenderer {
691
+ #renderFlags = FlexRenderFlags.ViewFirstRender;
692
+ #renderView = null;
693
+ #currentRenderEffectRef = null;
694
+ #content;
695
+ #props;
696
+ #injector;
697
+ #viewContainerRef;
698
+ #templateRef;
699
+ #flexRenderComponentFactory;
700
+ #getLatestContentValue = () => {
701
+ const content = this.#content();
702
+ const props = this.#props();
703
+ return typeof content !== 'function'
704
+ ? content
705
+ : runInInjectionContext(this.#injector(), () => content(props));
706
+ };
707
+ #latestContent = computed(() => this.#getLatestContentValue(), /* @ts-ignore */
708
+ ...(ngDevMode ? [{ debugName: "#latestContent" }] : /* istanbul ignore next */ []));
709
+ #getContentValue = computed(() => {
710
+ const latestContent = this.#latestContent();
711
+ return mapToFlexRenderTypedContent(latestContent);
712
+ }, /* @ts-ignore */
713
+ ...(ngDevMode ? [{ debugName: "#getContentValue" }] : /* istanbul ignore next */ []));
714
+ constructor(options) {
715
+ this.#content = options.content;
716
+ this.#props = options.props;
717
+ this.#injector = options.injector;
718
+ this.#templateRef = options.templateRef;
719
+ this.#viewContainerRef = options.viewContainerRef;
720
+ this.#flexRenderComponentFactory = new FlexRenderComponentFactory(this.#viewContainerRef);
721
+ }
722
+ mount() {
723
+ let previousContent;
724
+ let previousProps;
725
+ return effect(() => {
726
+ const props = this.#props();
727
+ const content = this.#content();
728
+ if (!(this.#renderFlags & FlexRenderFlags.ViewFirstRender)) {
729
+ if (previousContent !== content) {
730
+ this.#renderFlags |= FlexRenderFlags.ContentChanged;
731
+ }
732
+ if (previousProps !== props) {
733
+ this.#renderFlags |= FlexRenderFlags.PropsReferenceChanged;
734
+ }
735
+ }
736
+ untracked(() => this.#update());
737
+ if (FlexRenderFlags.ViewFirstRender & this.#renderFlags) {
738
+ this.#renderFlags &= ~FlexRenderFlags.ViewFirstRender;
739
+ }
740
+ previousContent = content;
741
+ previousProps = props;
742
+ });
743
+ }
744
+ destroy() {
745
+ if (this.#currentRenderEffectRef) {
746
+ this.#currentRenderEffectRef.destroy();
747
+ this.#currentRenderEffectRef = null;
748
+ }
749
+ if (this.#renderView) {
750
+ this.#renderView.unmount();
751
+ this.#renderView = null;
752
+ }
753
+ }
754
+ #update() {
755
+ if (this.#renderFlags &
756
+ (FlexRenderFlags.ContentChanged | FlexRenderFlags.ViewFirstRender)) {
757
+ this.#render();
758
+ return;
759
+ }
760
+ if (this.#renderFlags & FlexRenderFlags.PropsReferenceChanged) {
761
+ if (this.#renderView)
762
+ this.#renderView.updateProps(this.#props());
763
+ this.#renderFlags &= ~FlexRenderFlags.PropsReferenceChanged;
764
+ }
765
+ if (this.#renderFlags & FlexRenderFlags.Dirty) {
766
+ if (this.#renderView)
767
+ this.#renderView.dirtyCheck();
768
+ this.#renderFlags &= ~FlexRenderFlags.Dirty;
769
+ }
770
+ }
771
+ #render() {
772
+ // When the view is recreated from scratch (content change or first render),
773
+ // we have to destroy the current effect listener since it will be recreated
774
+ // skipping the first call (FlexRenderFlags.RenderEffectChecked)
775
+ if (this.#shouldRecreateEntireView() && this.#currentRenderEffectRef) {
776
+ this.#currentRenderEffectRef.destroy();
777
+ this.#currentRenderEffectRef = null;
778
+ this.#renderFlags &= ~FlexRenderFlags.RenderEffectChecked;
779
+ }
780
+ this.#viewContainerRef.clear();
781
+ if (this.#renderView) {
782
+ this.#renderView.unmount();
783
+ this.#renderView = null;
784
+ }
785
+ this.#renderFlags =
786
+ (this.#renderFlags & FlexRenderFlags.ViewFirstRender) |
787
+ (this.#renderFlags & FlexRenderFlags.RenderEffectChecked);
788
+ const resolvedContent = this.#getContentValue();
789
+ this.#renderView = this.#renderViewByContent(resolvedContent);
790
+ // If the content is a function `content(props)`, we initialize an effect
791
+ // to react to changes. If the current fn uses signals, we will set the DirtySignal flag
792
+ // to re-schedule the component updates
793
+ if (!this.#currentRenderEffectRef &&
794
+ typeof untracked(this.#content) === 'function') {
795
+ this.#currentRenderEffectRef = effect(() => {
796
+ this.#latestContent();
797
+ if (!(this.#renderFlags & FlexRenderFlags.RenderEffectChecked)) {
798
+ this.#renderFlags |= FlexRenderFlags.RenderEffectChecked;
799
+ return;
800
+ }
801
+ this.#renderFlags |= FlexRenderFlags.Dirty;
802
+ this.#doCheck();
803
+ }, { ...(ngDevMode ? { debugName: "#currentRenderEffectRef" } : /* istanbul ignore next */ {}), injector: this.#viewContainerRef.injector });
804
+ }
805
+ }
806
+ #shouldRecreateEntireView() {
807
+ return (this.#renderFlags &
808
+ FlexRenderFlags.ContentChanged &
809
+ FlexRenderFlags.ViewFirstRender);
810
+ }
811
+ #doCheck() {
812
+ const latestContent = this.#getContentValue();
813
+ if (latestContent.kind === 'null' || !this.#renderView) {
814
+ this.#renderFlags |= FlexRenderFlags.ContentChanged;
815
+ }
816
+ else {
817
+ const { kind: currentKind } = this.#renderView.content;
818
+ if (latestContent.kind !== currentKind ||
819
+ !this.#renderView.eq(latestContent)) {
820
+ this.#renderFlags |= FlexRenderFlags.ContentChanged;
821
+ }
822
+ this.#renderView.content = latestContent;
823
+ }
824
+ this.#update();
825
+ }
826
+ #renderViewByContent(content) {
827
+ if (content.kind === 'primitive') {
828
+ return this.#renderStringContent(content);
829
+ }
830
+ else if (content.kind === 'templateRef') {
831
+ return this.#renderTemplateRefContent(content);
832
+ }
833
+ else if (content.kind === 'flexRenderComponent') {
834
+ return this.#renderComponent(content);
835
+ }
836
+ else if (content.kind === 'component') {
837
+ return this.#renderCustomComponent(content);
838
+ }
839
+ else {
840
+ return null;
841
+ }
842
+ }
843
+ #renderStringContent(template) {
844
+ const context = () => {
845
+ const content = this.#content();
846
+ return typeof content === 'string' || typeof content === 'number'
847
+ ? content
848
+ : runInInjectionContext(this.#injector(), () => content?.(this.#props()));
849
+ };
850
+ const ref = this.#viewContainerRef.createEmbeddedView(this.#templateRef, {
851
+ get $implicit() {
852
+ return context();
853
+ },
854
+ });
855
+ return new FlexRenderTemplateView(template, ref);
856
+ }
857
+ #renderTemplateRefContent(template) {
858
+ const latestContext = () => this.#props();
859
+ const view = this.#viewContainerRef.createEmbeddedView(template.content, {
860
+ get $implicit() {
861
+ return latestContext();
862
+ },
863
+ }, { injector: this.#getInjector() });
864
+ return new FlexRenderTemplateView(template, view);
865
+ }
866
+ #renderComponent(flexRenderComponent) {
867
+ const { injector } = flexRenderComponent.content;
868
+ const componentInjector = this.#getInjector(injector);
869
+ const view = this.#flexRenderComponentFactory.createComponent(flexRenderComponent.content, componentInjector);
870
+ return new FlexRenderComponentView(flexRenderComponent, view);
871
+ }
872
+ #renderCustomComponent(component) {
873
+ const instance = flexRenderComponent(component.content, {
874
+ inputs: this.#props(),
875
+ });
876
+ const injector = this.#getInjector(instance.injector);
877
+ const view = this.#flexRenderComponentFactory.createComponent(instance, injector);
878
+ return new FlexRenderComponentView(component, view);
879
+ }
880
+ #getInjector(parentInjector) {
881
+ const getContext = () => this.#props();
882
+ const proxy = new Proxy(this.#props(), {
883
+ get: (_, key) => getContext()[key],
884
+ });
885
+ const staticProviders = [];
886
+ if ('table' in proxy) {
887
+ staticProviders.push({
888
+ provide: TanStackTableToken,
889
+ useValue: () => proxy.table,
890
+ });
891
+ }
892
+ if ('cell' in proxy) {
893
+ staticProviders.push({
894
+ provide: TanStackTableCellToken,
895
+ useValue: () => proxy.cell,
896
+ });
897
+ }
898
+ if ('header' in proxy) {
899
+ staticProviders.push({
900
+ provide: TanStackTableHeaderToken,
901
+ useValue: () => proxy.header,
902
+ });
903
+ }
904
+ return Injector.create({
905
+ parent: parentInjector ?? this.#injector(),
906
+ providers: [
907
+ ...staticProviders,
908
+ { provide: FlexRenderComponentProps, useValue: proxy },
909
+ ],
910
+ });
911
+ }
912
+ }
913
+
914
+ /**
915
+ * Simplified directive wrapper of `*flexRender`.
916
+ *
917
+ * Use this utility component to render headers, cells, or footers with custom markup.
918
+ *
919
+ * Only one prop (`cell`, `header`, or `footer`) may be passed based on the used selector.
920
+ *
921
+ * @example
922
+ * ```html
923
+ * <td *flexRenderCell="cell; let cell">{{cell}}</td>
924
+ * <th *flexRenderHeader="header; let header">{{header}}</th>
925
+ * <th *flexRenderFooter="footer; let footer">{{footer}}</th>
926
+ * ```
927
+ *
928
+ * This replaces calling `*flexRender` directly like this:
929
+ * ```html
930
+ * <td *flexRender="cell.column.columnDef.cell; props: cell.getContext(); let cell">{{cell}}</td>
931
+ * <td *flexRender="header.column.columnDef.header; props: header.getContext(); let header">{{header}}</td>
932
+ * <td *flexRender="footer.column.columnDef.footer; props: footer.getContext(); let footer">{{footer}}</td>
933
+ * ```
934
+ *
935
+ * Can be imported through {@link FlexRenderCell} or {@link FlexRender} import,
936
+ * which the latter is preferred.
937
+ *
938
+ * @example
939
+ * ```ts
940
+ * import {FlexRender} from '@tanstack/angular-table
941
+ *
942
+ * @Component({
943
+ * // ...
944
+ * imports: [
945
+ * FlexRender
946
+ * ]
947
+ * })
948
+ * ```
949
+ */
950
+ class FlexRenderCell {
951
+ cell = input(undefined, { ...(ngDevMode ? { debugName: "cell" } : /* istanbul ignore next */ {}), alias: 'flexRenderCell' });
952
+ header = input(undefined, { ...(ngDevMode ? { debugName: "header" } : /* istanbul ignore next */ {}), alias: 'flexRenderHeader' });
953
+ footer = input(undefined, { ...(ngDevMode ? { debugName: "footer" } : /* istanbul ignore next */ {}), alias: 'flexRenderFooter' });
954
+ #renderData = computed(() => {
955
+ const cell = this.cell();
956
+ const header = this.header();
957
+ const footer = this.footer();
958
+ if (cell) {
959
+ return [cell.column.columnDef.cell, cell.getContext()];
960
+ }
961
+ if (header) {
962
+ return [header.column.columnDef.header, header.getContext()];
963
+ }
964
+ if (footer) {
965
+ return [footer.column.columnDef.footer, footer.getContext()];
966
+ }
967
+ return [null, null];
968
+ }, { ...(ngDevMode ? { debugName: "#renderData" } : /* istanbul ignore next */ {}), equal: (a, b) => {
969
+ return a[0] === b[0] && a[1] === b[1];
970
+ } });
971
+ #injector = inject(Injector);
972
+ #templateRef = inject(TemplateRef);
973
+ #viewContainerRef = inject(ViewContainerRef);
974
+ constructor() {
975
+ const content = computed(() => this.#renderData()[0], /* @ts-ignore */
976
+ ...(ngDevMode ? [{ debugName: "content" }] : /* istanbul ignore next */ []));
977
+ const props = computed(() => this.#renderData()[1], /* @ts-ignore */
978
+ ...(ngDevMode ? [{ debugName: "props" }] : /* istanbul ignore next */ []));
979
+ const renderer = new FlexViewRenderer({
980
+ content: content,
981
+ props: props,
982
+ injector: () => this.#injector,
983
+ templateRef: this.#templateRef,
984
+ viewContainerRef: this.#viewContainerRef,
985
+ });
986
+ renderer.mount();
987
+ inject(DestroyRef).onDestroy(() => {
988
+ renderer.destroy();
989
+ });
990
+ }
991
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderCell, deps: [], target: i0.ɵɵFactoryTarget.Directive });
992
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.0", type: FlexRenderCell, isStandalone: true, selector: "ng-template[flexRenderCell], ng-template[flexRenderFooter], ng-template[flexRenderHeader]", inputs: { cell: { classPropertyName: "cell", publicName: "flexRenderCell", isSignal: true, isRequired: false, transformFunction: null }, header: { classPropertyName: "header", publicName: "flexRenderHeader", isSignal: true, isRequired: false, transformFunction: null }, footer: { classPropertyName: "footer", publicName: "flexRenderFooter", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 });
993
+ }
994
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderCell, decorators: [{
995
+ type: Directive,
996
+ args: [{
997
+ selector: 'ng-template[flexRenderCell], ng-template[flexRenderFooter], ng-template[flexRenderHeader]',
998
+ }]
999
+ }], ctorParameters: () => [], propDecorators: { cell: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRenderCell", required: false }] }], header: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRenderHeader", required: false }] }], footer: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRenderFooter", required: false }] }] } });
1000
+
1001
+ /**
1002
+ * Use this utility directive to render headers, cells, or footers with custom markup.
1003
+ *
1004
+ * Note: If you are rendering cell, header, or footer without custom context or other props,
1005
+ * you can use the {@link FlexRenderCell} directive as shorthand instead .
1006
+ *
1007
+ * @example
1008
+ * ```ts
1009
+ * import {FlexRender} from '@tanstack/angular-table';
1010
+ *
1011
+ * @Component({
1012
+ * imports: [FlexRender],
1013
+ * template: `
1014
+ * <td
1015
+ * *flexRender="
1016
+ * cell.column.columnDef.cell;
1017
+ * props: cell.getContext();
1018
+ * let cell"
1019
+ * >
1020
+ * {{cell}}
1021
+ * </td>
1022
+ *
1023
+ * <th
1024
+ * *flexRender="
1025
+ * header.column.columnDef.header;
1026
+ * props: header.getContext();
1027
+ * let header"
1028
+ * >
1029
+ * {{header}}
1030
+ * </td>
1031
+ *
1032
+ * <td
1033
+ * *flexRender="
1034
+ * footer.column.columnDef.footer;
1035
+ * props: footer.getContext();
1036
+ * let footer"
1037
+ * >
1038
+ * {{footer}}
1039
+ * </td>
1040
+ * `,
1041
+ * })
1042
+ * class App {
1043
+ * }
1044
+ * ```
1045
+ *
1046
+ * Can be imported through {@link FlexRenderDirective} or {@link FlexRender} import,
1047
+ * which the latter is preferred.
1048
+ */
1049
+ class FlexRenderDirective {
1050
+ content = input(undefined, { ...(ngDevMode ? { debugName: "content" } : /* istanbul ignore next */ {}), alias: 'flexRender' });
1051
+ props = input({}, { ...(ngDevMode ? { debugName: "props" } : /* istanbul ignore next */ {}), alias: 'flexRenderProps' });
1052
+ injector = input(inject(Injector), { ...(ngDevMode ? { debugName: "injector" } : /* istanbul ignore next */ {}), alias: 'flexRenderInjector' });
1053
+ #viewContainerRef = inject(ViewContainerRef);
1054
+ #templateRef = inject(TemplateRef);
1055
+ constructor() {
1056
+ const renderer = new FlexViewRenderer({
1057
+ content: this.content,
1058
+ props: this.props,
1059
+ injector: this.injector,
1060
+ templateRef: this.#templateRef,
1061
+ viewContainerRef: this.#viewContainerRef,
1062
+ });
1063
+ renderer.mount();
1064
+ inject(DestroyRef).onDestroy(() => {
1065
+ renderer.destroy();
1066
+ });
1067
+ }
1068
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1069
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.0.0", type: FlexRenderDirective, isStandalone: true, selector: "ng-template[flexRender]", inputs: { content: { classPropertyName: "content", publicName: "flexRender", isSignal: true, isRequired: false, transformFunction: null }, props: { classPropertyName: "props", publicName: "flexRenderProps", isSignal: true, isRequired: false, transformFunction: null }, injector: { classPropertyName: "injector", publicName: "flexRenderInjector", isSignal: true, isRequired: false, transformFunction: null } }, ngImport: i0 });
1070
+ }
1071
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.0", ngImport: i0, type: FlexRenderDirective, decorators: [{
1072
+ type: Directive,
1073
+ args: [{
1074
+ selector: 'ng-template[flexRender]',
1075
+ }]
1076
+ }], ctorParameters: () => [], propDecorators: { content: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRender", required: false }] }], props: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRenderProps", required: false }] }], injector: [{ type: i0.Input, args: [{ isSignal: true, alias: "flexRenderInjector", required: false }] }] } });
5
1077
 
6
1078
  /**
7
1079
  * Implementation from @tanstack/angular-query
8
- * {@link https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/util/lazy-init/lazy-init.ts}
1080
+ * {https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/util/lazy-init/lazy-init.ts}
9
1081
  */
10
1082
  function lazyInit(initializer) {
11
1083
  let object = null;
@@ -45,44 +1117,106 @@ function lazyInit(initializer) {
45
1117
  });
46
1118
  }
47
1119
 
48
- function proxifyTable(tableSignal) {
49
- const internalState = tableSignal;
50
- return new Proxy(internalState, {
51
- apply() {
52
- return tableSignal();
1120
+ function signalToReadonlyAtom(signal, injector, debugName) {
1121
+ const _signal = Object.assign(signal, {
1122
+ get: () => signal(),
1123
+ subscribe: (observer) => {
1124
+ return untracked(() => toObservable(computed(signal), { injector: injector }).subscribe(observer));
1125
+ },
1126
+ });
1127
+ if (debugName) {
1128
+ _signal.toString = () => debugName;
1129
+ }
1130
+ return _signal;
1131
+ }
1132
+ function signalToWritableAtom(signal, injector, debugName) {
1133
+ const _signal = Object.assign(signal.asReadonly(), {
1134
+ set: (updater) => {
1135
+ typeof updater === 'function'
1136
+ ? signal.update(updater)
1137
+ : signal.set(updater);
1138
+ },
1139
+ get: () => signal(),
1140
+ subscribe: (observer) => {
1141
+ return untracked(() => toObservable(computed(signal), { injector: injector }).subscribe(observer));
53
1142
  },
54
- get(target, property) {
55
- if (target[property]) {
56
- return target[property];
1143
+ });
1144
+ if (debugName) {
1145
+ _signal.toString = () => debugName;
1146
+ }
1147
+ return _signal;
1148
+ }
1149
+ /**
1150
+ * Creates the table-core reactivity bindings used by the Angular adapter.
1151
+ *
1152
+ * Table state atoms are backed by TanStack Store atoms. The options store stays
1153
+ * framework-native because row-model APIs read `table.options` directly during
1154
+ * render. Readonly table atoms bridge Store dependency tracking into Angular
1155
+ * computed signals.
1156
+ */
1157
+ function angularReactivity(injector) {
1158
+ const ngZone = injector.get(NgZone);
1159
+ const subscriptions = new Set();
1160
+ return {
1161
+ createOptionsStore: true,
1162
+ wrapExternalAtoms: true,
1163
+ addSubscription: (subscription) => {
1164
+ subscriptions.add(subscription);
1165
+ },
1166
+ unmount: () => {
1167
+ subscriptions.forEach((s) => s.unsubscribe());
1168
+ subscriptions.clear();
1169
+ },
1170
+ schedule: (fn) => ngZone.runOutsideAngular(() => queueMicrotask(fn)),
1171
+ createReadonlyAtom: (fn, options) => {
1172
+ const signal = computed(() => fn(), {
1173
+ equal: options?.compare,
1174
+ debugName: options?.debugName,
1175
+ });
1176
+ return signalToReadonlyAtom(signal, injector, options?.debugName);
1177
+ },
1178
+ createWritableAtom: (value, options) => {
1179
+ const writableSignal = signal(value, {
1180
+ equal: options?.compare,
1181
+ debugName: options?.debugName,
1182
+ });
1183
+ return signalToWritableAtom(writableSignal, injector, options?.debugName);
1184
+ },
1185
+ untrack: untracked,
1186
+ batch: (fn) => fn(),
1187
+ };
1188
+ }
1189
+
1190
+ function createStateProxy(table) {
1191
+ const getSnapshot = () => {
1192
+ const snapshot = {};
1193
+ const stateKeys = Object.keys(table.initialState);
1194
+ for (const key of stateKeys) {
1195
+ snapshot[key] = table.atoms[key].get();
1196
+ }
1197
+ return snapshot;
1198
+ };
1199
+ const target = {};
1200
+ return new Proxy(target, {
1201
+ get(target, prop, receiver) {
1202
+ if (prop === 'toJSON') {
1203
+ return getSnapshot;
57
1204
  }
58
- const table = untracked(tableSignal);
59
- /**
60
- * Attempt to convert all accessors into computed ones,
61
- * excluding handlers as they do not retain any reactive value
62
- */
63
- if (property.startsWith('get') &&
64
- !property.endsWith('Handler') &&
65
- !property.endsWith('Model')) {
66
- const maybeFn = table[property];
67
- if (typeof maybeFn === 'function') {
68
- Object.defineProperty(target, property, {
69
- value: toComputed(tableSignal, maybeFn),
70
- configurable: true,
71
- enumerable: true,
72
- });
73
- return target[property];
74
- }
1205
+ if (typeof prop === 'string' && prop in table.initialState) {
1206
+ return table.atoms[prop]?.get();
75
1207
  }
76
- // @ts-expect-error
77
- return (target[property] = table[property]);
1208
+ return Reflect.get(target, prop, receiver);
78
1209
  },
79
1210
  has(_, prop) {
80
- return !!untracked(tableSignal)[prop];
1211
+ return typeof prop === 'string' && prop in table.initialState;
81
1212
  },
82
1213
  ownKeys() {
83
- return Reflect.ownKeys(untracked(tableSignal));
1214
+ return Reflect.ownKeys(table.initialState);
84
1215
  },
85
- getOwnPropertyDescriptor() {
1216
+ getOwnPropertyDescriptor(_, prop) {
1217
+ if (typeof prop !== 'string' || !(prop in table.initialState)) {
1218
+ return undefined;
1219
+ }
86
1220
  return {
87
1221
  enumerable: true,
88
1222
  configurable: true,
@@ -91,233 +1225,196 @@ function proxifyTable(tableSignal) {
91
1225
  });
92
1226
  }
93
1227
  /**
94
- * Here we'll handle all type of accessors:
95
- * - 0 argument -> e.g. table.getCanNextPage())
96
- * - 0~1 arguments -> e.g. table.getIsSomeRowsPinned(position?)
97
- * - 1 required argument -> e.g. table.getColumn(columnId)
98
- * - 1+ argument -> e.g. table.getRow(id, searchAll?)
99
- *
100
- * Since we are not able to detect automatically the accessors parameters,
101
- * we'll wrap all accessors into a cached function wrapping a computed
102
- * that return it's value based on the given parameters
1228
+ * Creates and returns an Angular-reactive table instance.
1229
+ *
1230
+ * The initializer is intentionally re-evaluated whenever any signal read inside it changes.
1231
+ * This is how the adapter keeps the table in sync with Angular's reactivity model.
1232
+ *
1233
+ * Because of that behavior, keep expensive/static values (for example `columns`, feature setup, row models)
1234
+ * as stable references outside the initializer, and only read reactive state (`data()`, pagination/filter/sorting signals, etc.)
1235
+ * inside it.
1236
+ *
1237
+ * 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(...)`.
1238
+ *
1239
+ * @example
1240
+ * 1. Register the table features you need
1241
+ * ```ts
1242
+ * // Register only the features you need
1243
+ * import {tableFeatures, rowPaginationFeature} from '@tanstack/angular-table';
1244
+ * const features = tableFeatures({
1245
+ * rowPaginationFeature,
1246
+ * // ...all other features you need
1247
+ * })
1248
+ *
1249
+ * // Use all table core features
1250
+ * import {stockFeatures} from '@tanstack/angular-table';
1251
+ * const features = tableFeatures(stockFeatures);
1252
+ * ```
1253
+ * 2. Prepare the table columns
1254
+ * ```ts
1255
+ * import {ColumnDef} from '@tanstack/angular-table';
1256
+ *
1257
+ * type MyData = {}
1258
+ *
1259
+ * const columns: ColumnDef<typeof features, MyData>[] = [
1260
+ * // ...column definitions
1261
+ * ]
1262
+ *
1263
+ * // or using createColumnHelper
1264
+ * import {createColumnHelper} from '@tanstack/angular-table';
1265
+ * const columnHelper = createColumnHelper<typeof features, MyData>();
1266
+ * const columns = columnHelper.columns([
1267
+ * columnHelper.accessor(...),
1268
+ * // ...other columns
1269
+ * ])
1270
+ * ```
1271
+ * 3. Create the table instance with `injectTable`
1272
+ * ```ts
1273
+ * const table = injectTable(() => {
1274
+ * // ...table options,
1275
+ * features,
1276
+ * columns: columns,
1277
+ * data: myDataSignal(),
1278
+ * })
1279
+ * ```
1280
+ *
1281
+ * @returns An Angular-reactive TanStack Table instance.
103
1282
  */
104
- function toComputed(signal, fn) {
105
- const hasArgs = fn.length > 0;
106
- if (!hasArgs) {
107
- return computed(() => {
108
- void signal();
109
- return fn();
1283
+ function injectTable(options) {
1284
+ assertInInjectionContext(injectTable);
1285
+ const injector = inject(Injector);
1286
+ const ngZone = inject(NgZone);
1287
+ return ngZone.runOutsideAngular(() => lazyInit(() => {
1288
+ const table = constructTable({
1289
+ ...options(),
1290
+ features: {
1291
+ coreReativityFeature: angularReactivity(injector),
1292
+ ...options().features,
1293
+ },
110
1294
  });
111
- }
112
- const computedCache = {};
113
- return (...argsArray) => {
114
- const serializedArgs = serializeArgs(...argsArray);
115
- if (computedCache.hasOwnProperty(serializedArgs)) {
116
- return computedCache[serializedArgs]?.();
117
- }
118
- const computedSignal = computed(() => {
119
- void signal();
120
- return fn(...argsArray);
1295
+ injector.get(DestroyRef).onDestroy(() => {
1296
+ table._reactivity.unmount?.();
121
1297
  });
122
- computedCache[serializedArgs] = computedSignal;
123
- return computedSignal();
124
- };
125
- }
126
- function serializeArgs(...args) {
127
- return JSON.stringify(args);
1298
+ const stateProxy = createStateProxy(table);
1299
+ Object.defineProperty(table, 'state', {
1300
+ get() {
1301
+ return stateProxy;
1302
+ },
1303
+ configurable: true,
1304
+ enumerable: true,
1305
+ });
1306
+ let isMount = true;
1307
+ effect(() => {
1308
+ const newOptions = options();
1309
+ if (isMount) {
1310
+ isMount = false;
1311
+ return;
1312
+ }
1313
+ untracked(() => table.setOptions((previous) => ({
1314
+ ...previous,
1315
+ ...newOptions,
1316
+ })));
1317
+ }, { injector, debugName: 'tableOptionsUpdate' });
1318
+ return table;
1319
+ }));
128
1320
  }
129
1321
 
130
- class FlexRenderDirective {
131
- viewContainerRef;
132
- templateRef;
133
- content = undefined;
134
- props = {};
135
- injector = inject(Injector);
136
- constructor(viewContainerRef, templateRef) {
137
- this.viewContainerRef = viewContainerRef;
138
- this.templateRef = templateRef;
139
- }
140
- ref = null;
141
- ngOnChanges(changes) {
142
- if (this.ref instanceof ComponentRef) {
143
- this.ref.injector.get(ChangeDetectorRef).markForCheck();
144
- }
145
- if (!changes['content']) {
146
- return;
147
- }
148
- this.render();
1322
+ /**
1323
+ * Creates app-scoped Angular table helpers with features, row models, and
1324
+ * renderable component maps pre-bound.
1325
+ *
1326
+ * Use this when an app or design system wants typed `injectAppTable`,
1327
+ * pre-bound column helpers, and typed table/cell/header context injection
1328
+ * helpers without repeating the same feature and component generics.
1329
+ *
1330
+ * @example
1331
+ * ```ts
1332
+ * const { injectAppTable, createAppColumnHelper } = createTableHook({
1333
+ * features,
1334
+ * rowModels: {},
1335
+ * tableComponents: {},
1336
+ * cellComponents: {},
1337
+ * headerComponents: {},
1338
+ * })
1339
+ * ```
1340
+ */
1341
+ function createTableHook({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }) {
1342
+ function injectTableContext$1() {
1343
+ return injectTableContext();
149
1344
  }
150
- render() {
151
- this.viewContainerRef.clear();
152
- const { content, props } = this;
153
- if (content === null || content === undefined) {
154
- this.ref = null;
155
- return;
156
- }
157
- if (typeof content === 'function') {
158
- return this.renderContent(content(props));
159
- }
160
- else {
161
- return this.renderContent(content);
162
- }
1345
+ function injectTableHeaderContext$1() {
1346
+ return injectTableHeaderContext();
163
1347
  }
164
- renderContent(content) {
165
- if (typeof content === 'string' || typeof content === 'number') {
166
- return this.renderStringContent();
167
- }
168
- if (content instanceof TemplateRef) {
169
- return this.viewContainerRef.createEmbeddedView(content, this.getTemplateRefContext());
170
- }
171
- else if (content instanceof FlexRenderComponent) {
172
- return this.renderComponent(content);
173
- }
174
- else if (content instanceof Type) {
175
- return this.renderCustomComponent(content);
176
- }
177
- else {
178
- return null;
179
- }
1348
+ function injectTableCellContext$1() {
1349
+ return injectTableCellContext();
180
1350
  }
181
- renderStringContent() {
182
- const context = () => {
183
- return typeof this.content === 'string' ||
184
- typeof this.content === 'number'
185
- ? this.content
186
- : this.content?.(this.props);
187
- };
188
- return this.viewContainerRef.createEmbeddedView(this.templateRef, {
189
- get $implicit() {
190
- return context();
191
- },
192
- });
1351
+ function injectFlexRenderHeaderContext() {
1352
+ return injectFlexRenderContext();
193
1353
  }
194
- renderComponent(flexRenderComponent) {
195
- const { component, inputs, injector } = flexRenderComponent;
196
- const getContext = () => this.props;
197
- const proxy = new Proxy(this.props, {
198
- get: (_, key) => getContext()?.[key],
199
- });
200
- const componentInjector = Injector.create({
201
- parent: injector ?? this.injector,
202
- providers: [{ provide: FlexRenderComponentProps, useValue: proxy }],
203
- });
204
- const componentRef = this.viewContainerRef.createComponent(component, {
205
- injector: componentInjector,
206
- });
207
- for (const prop in inputs) {
208
- if (componentRef.instance?.hasOwnProperty(prop)) {
209
- componentRef.setInput(prop, inputs[prop]);
210
- }
211
- }
212
- return componentRef;
1354
+ function injectFlexRenderCellContext() {
1355
+ return injectFlexRenderContext();
213
1356
  }
214
- renderCustomComponent(component) {
215
- const componentRef = this.viewContainerRef.createComponent(component, {
216
- injector: this.injector,
217
- });
218
- for (const prop in this.props) {
219
- // Only signal based input can be added here
220
- if (componentRef.instance?.hasOwnProperty(prop) &&
221
- // @ts-ignore
222
- isSignal(componentRef.instance[prop])) {
223
- componentRef.setInput(prop, this.props[prop]);
224
- }
1357
+ function injectAppTable(tableOptions) {
1358
+ function appCell(cell) {
1359
+ return cell;
225
1360
  }
226
- return componentRef;
227
- }
228
- getTemplateRefContext() {
229
- const getContext = () => this.props;
230
- return {
231
- get $implicit() {
232
- return getContext();
1361
+ function appHeader(header) {
1362
+ return header;
1363
+ }
1364
+ function appFooter(footer) {
1365
+ return footer;
1366
+ }
1367
+ const appTableFeatures = {
1368
+ constructTableAPIs: (table) => {
1369
+ Object.assign(table, tableComponents, { appCell, appHeader, appFooter });
1370
+ },
1371
+ assignCellPrototype(prototype) {
1372
+ Object.assign(prototype, cellComponents);
1373
+ },
1374
+ assignHeaderPrototype(prototype) {
1375
+ Object.assign(prototype, headerComponents);
233
1376
  },
234
1377
  };
235
- }
236
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.5", ngImport: i0, type: FlexRenderDirective, deps: [{ token: ViewContainerRef }, { token: TemplateRef }], target: i0.ɵɵFactoryTarget.Directive });
237
- static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.0.5", type: FlexRenderDirective, isStandalone: true, selector: "[flexRender]", inputs: { content: ["flexRender", "content"], props: ["flexRenderProps", "props"], injector: ["flexRenderInjector", "injector"] }, usesOnChanges: true, ngImport: i0 });
238
- }
239
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.5", ngImport: i0, type: FlexRenderDirective, decorators: [{
240
- type: Directive,
241
- args: [{
242
- selector: '[flexRender]',
243
- standalone: true,
244
- }]
245
- }], ctorParameters: () => [{ type: i0.ViewContainerRef, decorators: [{
246
- type: Inject,
247
- args: [ViewContainerRef]
248
- }] }, { type: i0.TemplateRef, decorators: [{
249
- type: Inject,
250
- args: [TemplateRef]
251
- }] }], propDecorators: { content: [{
252
- type: Input,
253
- args: [{ required: true, alias: 'flexRender' }]
254
- }], props: [{
255
- type: Input,
256
- args: [{ required: true, alias: 'flexRenderProps' }]
257
- }], injector: [{
258
- type: Input,
259
- args: [{ required: false, alias: 'flexRenderInjector' }]
260
- }] } });
261
- class FlexRenderComponent {
262
- component;
263
- inputs;
264
- injector;
265
- constructor(component, inputs = {}, injector) {
266
- this.component = component;
267
- this.inputs = inputs;
268
- this.injector = injector;
269
- }
270
- }
271
- const FlexRenderComponentProps = new InjectionToken('[@tanstack/angular-table] Flex render component context props');
272
- function injectFlexRenderContext() {
273
- return inject(FlexRenderComponentProps);
274
- }
275
-
276
- function injectTable(options) {
277
- return lazyInit(() => {
278
- const resolvedOptions = {
279
- state: {},
280
- onStateChange: () => { },
281
- renderFallbackValue: null,
282
- ...options(),
283
- };
284
- const table = _createTable(resolvedOptions);
285
- // By default, manage table state here using the table's initial state
286
- const state = signal(table.initialState);
287
- // Compose table options using computed.
288
- // This is to allow `tableSignal` to listen and set table option
289
- const updatedOptions = computed(() => {
290
- // listen to table state changed
291
- const tableState = state();
292
- // listen to input options changed
293
- const tableOptions = options();
1378
+ return injectTable(() => {
294
1379
  return {
295
- ...table.options,
296
- ...resolvedOptions,
297
- ...tableOptions,
298
- state: { ...tableState, ...tableOptions.state },
299
- onStateChange: updater => {
300
- const value = updater instanceof Function ? updater(tableState) : updater;
301
- state.set(value);
302
- resolvedOptions.onStateChange?.(updater);
1380
+ ...defaultTableOptions,
1381
+ ...tableOptions(),
1382
+ features: {
1383
+ ...defaultTableOptions.features,
1384
+ appTableFeatures,
303
1385
  },
304
1386
  };
305
1387
  });
306
- // convert table instance to signal for proxify to listen to any table state and options changes
307
- const tableSignal = computed(() => {
308
- table.setOptions(updatedOptions());
309
- return table;
310
- }, {
311
- equal: () => false,
312
- });
313
- // proxify Table instance to provide ability for consumer to listen to any table state changes
314
- return proxifyTable(tableSignal);
315
- });
1388
+ }
1389
+ function createAppColumnHelper() {
1390
+ // The runtime implementation is the same - components are attached at render time
1391
+ // This cast provides the enhanced types for column definitions
1392
+ return createColumnHelper();
1393
+ }
1394
+ return {
1395
+ createAppColumnHelper,
1396
+ injectTableContext: injectTableContext$1,
1397
+ injectTableHeaderContext: injectTableHeaderContext$1,
1398
+ injectTableCellContext: injectTableCellContext$1,
1399
+ injectFlexRenderHeaderContext,
1400
+ injectFlexRenderCellContext,
1401
+ injectAppTable,
1402
+ };
316
1403
  }
317
1404
 
1405
+ /**
1406
+ * Constant helper to import FlexRender directives.
1407
+ *
1408
+ * You should prefer to use this constant over importing the directives separately,
1409
+ * as it ensures you always have the correct set of directives over library updates.
1410
+ *
1411
+ * @see {@link FlexRenderDirective} and {@link FlexRenderCell} for more details on the directives included in this export.
1412
+ */
1413
+ const FlexRender = [FlexRenderDirective, FlexRenderCell];
1414
+
318
1415
  /**
319
1416
  * Generated bundle index. Do not edit.
320
1417
  */
321
1418
 
322
- export { FlexRenderComponent, FlexRenderDirective, injectFlexRenderContext, injectTable };
1419
+ export { FlexRender, FlexRenderCell, FlexRenderComponentInstance, FlexRenderDirective, TanStackTable, TanStackTableCell, TanStackTableCellToken, TanStackTableHeader, TanStackTableHeaderToken, TanStackTableToken, createTableHook, flexRenderComponent, injectFlexRenderContext, injectTable, injectTableCellContext, injectTableContext, injectTableHeaderContext };
323
1420
  //# sourceMappingURL=tanstack-angular-table.mjs.map