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): added injector optional parameter for more flexibility #5525

Merged
merged 4 commits into from
May 7, 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
11 changes: 6 additions & 5 deletions examples/angular/grouping/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import {
ChangeDetectionStrategy,
Component,
computed,
effect,
signal,
} from '@angular/core'
import {
createAngularTable,
FlexRenderDirective,
GroupingState,
Updater,
createAngularTable,
getCoreRowModel,
getExpandedRowModel,
getFilteredRowModel,
getGroupedRowModel,
getPaginationRowModel,
GroupingState,
Updater,
} from '@tanstack/angular-table'
import { columns } from './columns'
import { makeData } from './makeData'
Expand All @@ -34,7 +33,7 @@ export class AppComponent {

stringifiedGrouping = computed(() => JSON.stringify(this.grouping(), null, 2))

table = createAngularTable(() => ({
tableOptions = computed(() => ({
data: this.data(),
columns: columns,
state: {
Expand All @@ -55,6 +54,8 @@ export class AppComponent {
debugTable: true,
}))

table = createAngularTable(this.tableOptions)

onPageInputChange(event: any): void {
const page = event.target.value ? Number(event.target.value) - 1 : 0
this.table.setPageIndex(page)
Expand Down
122 changes: 49 additions & 73 deletions packages/angular-table/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,72 @@
import { computed, signal } from '@angular/core'
import {
computed,
effect,
inject,
Injector,
runInInjectionContext,
type Signal,
signal,
untracked,
} from '@angular/core'
import {
createTable,
RowData,
type Table,
TableOptions,
TableOptionsResolved,
TableState,
createTable,
type Table,
} from '@tanstack/table-core'
import { proxifyTable } from './proxy'
import { lazyInit } from './lazy-signal-initializer'
import { proxifyTable } from './proxy'

export * from '@tanstack/table-core'

export {
FlexRenderDirective,
FlexRenderComponent,
FlexRenderDirective,
injectFlexRenderContext,
} from './flex-render'

export function createAngularTable<TData extends RowData>(
options: () => TableOptions<TData>
): Table<TData> & Signal<Table<TData>> {
const injector = inject(Injector)
): Table<TData> {
return lazyInit(() => {
const resolvedOptions = {
state: {},
onStateChange: () => {},
renderFallbackValue: null,
...options(),
}

return lazyInit(() =>
runInInjectionContext(injector, () => {
const resolvedOptionsSignal = computed<TableOptionsResolved<TData>>(
() => {
return {
state: {},
onStateChange: () => {},
renderFallbackValue: null,
...options(),
}
}
)
const table = createTable(resolvedOptions)

const notifier = signal([], { equal: () => false })
const table = createTable(untracked(resolvedOptionsSignal))
const state = signal(table.initialState)
// By default, manage table state here using the table's initial state
const state = signal<TableState>(table.initialState)

function updateOptions() {
const tableState = untracked(state)
const resolvedOptions = untracked(resolvedOptionsSignal)
untracked(() => {
table.setOptions(prev => ({
...prev,
...resolvedOptions,
state: { ...tableState, ...resolvedOptions.state },
onStateChange: updater => {
const value =
updater instanceof Function ? updater(tableState) : updater
state.set(value)
resolvedOptions.onStateChange?.(updater)
},
}))
})
// Compose table options using computed.
// This is to allow `tableSignal` to listen and set table option
const updatedOptions = computed<TableOptionsResolved<TData>>(() => {
// listen to table state changed
const tableState = state()
// listen to input options changed
const tableOptions = options()
return {
...table.options,
...resolvedOptions,
...tableOptions,
state: { ...tableState, ...tableOptions.state },
onStateChange: updater => {
const value =
updater instanceof Function ? updater(tableState) : updater
state.set(value)
resolvedOptions.onStateChange?.(updater)
},
}
})

updateOptions()

let firstRender = true
effect(() => {
void [state(), resolvedOptionsSignal()]
if (firstRender) {
return (firstRender = false)
}
untracked(() => {
updateOptions()
notifier.set([])
})
})

const tableSignal = computed(
() => {
notifier()
return table
},
{
equal: () => false,
}
)
// convert table instance to signal for proxify to listen to any table state and options changes
const tableSignal = computed(
() => {
table.setOptions(updatedOptions())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d go back with the previous implementation, updating the options inside a signal is theoretically wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree. Doing side effects not related to value being returned by the computed signal is theoritically wrong. But updating the object before it will be return inside a computed is not. IMHO

Copy link
Contributor Author

@merto20 merto20 May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to get rid of the effect from the start because of its high requirements. But was in doubt because I was not so sure if I'm doing it wrong to call updateOptions method inside a computed. Adding a updatedOptions signal simplifies that and giving more clarity that it is not wrong to update the Table object before it will be returned in the computed signal. Calling updateOptions functions inside the computed signal seems wrong because we cannot control if someone modifies that method and do something more than updating the table options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the explaination

return table
},
{
equal: () => false,
}
)

return proxifyTable(tableSignal)
merto20 marked this conversation as resolved.
Show resolved Hide resolved
})
)
// proxify Table instance to provide ability for consumer to listen to any table state changes
return proxifyTable(tableSignal)
})
}