Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(angular-table): Improve proxy signal implementation for v9 - Maybe also v8 #5816

Merged
merged 21 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions examples/angular/column-ordering/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import type {
ColumnVisibilityState,
} from '@tanstack/angular-table'

const _features = tableFeatures({
columnVisibilityFeature,
columnOrderingFeature,
})

const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
{
header: 'Name',
Expand Down Expand Up @@ -77,6 +72,11 @@ const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
},
]

const _features = tableFeatures({
columnVisibilityFeature,
columnOrderingFeature,
})

@Component({
selector: 'app-root',
standalone: true,
Expand All @@ -91,12 +91,13 @@ export class AppComponent {

readonly table = injectTable(() => ({
_features,
columns: defaultColumns,
data: this.data(),
columns: defaultColumns,
state: {
columnOrder: this.columnOrder(),
columnVisibility: this.columnVisibility(),
},
enableExperimentalReactivity: true,
onColumnVisibilityChange: (updaterOrValue) => {
typeof updaterOrValue === 'function'
? this.columnVisibility.update(updaterOrValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class AppComponent {
},
columns: this.columns(),
data: this.data(),
enableExperimentalReactivity: true,
debugTable: true,
debugHeaders: true,
debugColumns: true,
Expand Down
1 change: 1 addition & 0 deletions examples/angular/column-pinning/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export class AppComponent {
columnOrder: this.columnOrder(),
columnPinning: this.columnPinning(),
},
enableExperimentalReactivity: true,
onColumnVisibilityChange: (updaterOrValue) => {
typeof updaterOrValue === 'function'
? this.columnVisibility.update(updaterOrValue)
Expand Down
1 change: 1 addition & 0 deletions examples/angular/grouping/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class AppComponent {
paginatedRowModel: createPaginatedRowModel(),
filteredRowModel: createFilteredRowModel(),
},
enableExperimentalReactivity: true,
data: this.data(),
columns,
initialState: {
Expand Down
11 changes: 11 additions & 0 deletions examples/angular/row-selection-signal/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ export class AppComponent {
paginatedRowModel: createPaginatedRowModel(),
},
data: this.data(),
enableExperimentalReactivity: true,
_features: {
rowSelectionFeature,
rowPaginationFeature,
columnFilteringFeature,
columnVisibilityFeature,
},
_rowModels: {
filteredRowModel: createFilteredRowModel(),
paginatedRowModel: createPaginatedRowModel(),
},
columns: this.columns,
state: {
rowSelection: this.rowSelection(),
Expand Down
1 change: 1 addition & 0 deletions examples/angular/row-selection/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class AppComponent {
state: {
rowSelection: this.rowSelection(),
},
enableExperimentalReactivity: true,
enableRowSelection: true, // enable row selection for all rows
// enableRowSelection: row => row.original.age > 18, // or enable row selection conditionally per row
onRowSelectionChange: (updaterOrValue) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class PersonTableComponent {
state: {
pagination: this.pagination(),
},
enableExperimentalReactivity: true,
onPaginationChange: (updaterOrValue) => {
typeof updaterOrValue === 'function'
? this.pagination.update(updaterOrValue)
Expand Down
17 changes: 13 additions & 4 deletions packages/angular-table/src/flex-render.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { DoCheck, OnChanges, SimpleChanges } from '@angular/core'
import {
ChangeDetectorRef,
ComponentRef,
Directive,
EmbeddedViewRef,
Inject,
inject,
InjectionToken,
Injector,
Input,
isSignal,
TemplateRef,
Type,
ViewContainerRef,
inject,
isSignal,
} from '@angular/core'
import type { DoCheck, OnChanges, SimpleChanges } from '@angular/core'
import type { Table } from '@tanstack/table-core'

export type FlexRenderContent<TProps extends NonNullable<unknown>> =
Expand Down Expand Up @@ -48,6 +48,8 @@ export class FlexRenderDirective<TProps extends NonNullable<unknown>>

ref?: ComponentRef<unknown> | EmbeddedViewRef<unknown> | null = null

experimentalReactivity = false

constructor(
@Inject(ViewContainerRef)
private readonly viewContainerRef: ViewContainerRef,
Expand All @@ -56,7 +58,7 @@ export class FlexRenderDirective<TProps extends NonNullable<unknown>>
) {}

ngDoCheck(): void {
if (this.ref instanceof ComponentRef) {
if (!this.experimentalReactivity && this.ref instanceof ComponentRef) {
this.ref.injector.get(ChangeDetectorRef).markForCheck()
}
}
Expand All @@ -65,6 +67,13 @@ export class FlexRenderDirective<TProps extends NonNullable<unknown>>
if (!changes['content']) {
return
}

if ('table' in this.props) {
this.experimentalReactivity =
(this.props.table as Partial<Table<any, any>>).options
?.enableExperimentalReactivity ?? false
}

this.render()
}

Expand Down
1 change: 1 addition & 0 deletions packages/angular-table/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './proxy'
export * from './lazy-signal-initializer'
export * from './injectTable'
export * from './createTableHelper'
export * from './reactivity'
20 changes: 12 additions & 8 deletions packages/angular-table/src/injectTable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type { Signal } from '@angular/core'
import { computed, signal } from '@angular/core'
import type {
RowData,
Table,
TableFeatures,
TableOptions,
TableState,
} from '@tanstack/table-core'
import {
constructTable,
coreFeatures,
Expand All @@ -7,14 +15,7 @@ import {
} from '@tanstack/table-core'
import { lazyInit } from './lazy-signal-initializer'
import { proxifyTable } from './proxy'
import type {
RowData,
Table,
TableFeatures,
TableOptions,
TableState,
} from '@tanstack/table-core'
import type { Signal } from '@angular/core'
import { reactivityFeature } from './reactivity'

export function injectTable<
TFeatures extends TableFeatures,
Expand All @@ -27,6 +28,7 @@ export function injectTable<
return {
...coreFeatures,
...options()._features,
reactivityFeature,
}
}

Expand Down Expand Up @@ -76,6 +78,8 @@ export function injectTable<
},
)

table._setRootNotifier?.(tableSignal as any)

// proxify Table instance to provide ability for consumer to listen to any table state changes
return proxifyTable(tableSignal)
})
Expand Down
Loading
Loading