-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't include table features by default anymore
- Loading branch information
1 parent
3ce2287
commit 8b3631d
Showing
9 changed files
with
157 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,6 @@ | ||
import { type Signal, computed, signal } from '@angular/core' | ||
import { constructTable } 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' | ||
|
||
export * from '@tanstack/table-core' | ||
|
||
export { | ||
type FlexRenderContent, | ||
FlexRenderComponent, | ||
FlexRenderDirective, | ||
injectFlexRenderContext, | ||
} from './flex-render' | ||
|
||
export function injectTable< | ||
TFeatures extends TableFeatures, | ||
TData extends RowData, | ||
>( | ||
options: () => TableOptions<TFeatures, TData>, | ||
): Table<TFeatures, TData> & Signal<Table<TFeatures, TData>> { | ||
return lazyInit(() => { | ||
const resolvedOptions = { | ||
state: {}, | ||
onStateChange: () => {}, | ||
renderFallbackValue: null, | ||
...options(), | ||
} | ||
|
||
const table = constructTable(resolvedOptions) | ||
|
||
// By default, manage table state here using the table's initial state | ||
const state = signal<TableState<TFeatures>>(table.initialState as any) // TODO: fix type | ||
|
||
// Compose table options using computed. | ||
// This is to allow `tableSignal` to listen and set table option | ||
const updatedOptions = computed<TableOptions<TFeatures, TData>>(() => { | ||
// listen to table state changed | ||
const tableState = state() | ||
// listen to input options changed | ||
const tableOptions = options() | ||
return { | ||
...table.options, | ||
...resolvedOptions, | ||
...tableOptions, | ||
state: { ...tableState, ...tableOptions.state }, | ||
onStateChange: (updater) => { | ||
const value = | ||
updater instanceof Function ? updater(tableState) : updater | ||
state.set(value) | ||
resolvedOptions.onStateChange(updater) | ||
}, | ||
} | ||
}) | ||
|
||
// convert table instance to signal for proxify to listen to any table state and options changes | ||
const tableSignal = computed( | ||
() => { | ||
table.setOptions(updatedOptions()) | ||
return table | ||
}, | ||
{ | ||
equal: () => false, | ||
}, | ||
) | ||
|
||
// proxify Table instance to provide ability for consumer to listen to any table state changes | ||
return proxifyTable(tableSignal) | ||
}) | ||
} | ||
export * from './flex-render' | ||
export * from './proxy' | ||
export * from './lazy-signal-initializer' | ||
export * from './injectTable' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { type Signal, computed, signal } from '@angular/core' | ||
import { | ||
constructTable, | ||
coreFeatures, | ||
getInitialTableState, | ||
} 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' | ||
|
||
export function injectTable< | ||
TFeatures extends TableFeatures, | ||
TData extends RowData, | ||
>( | ||
options: () => TableOptions<TFeatures, TData>, | ||
): Table<TFeatures, TData> & Signal<Table<TFeatures, TData>> { | ||
return lazyInit(() => { | ||
const resolvedOptions = { | ||
...options(), | ||
_features: { | ||
...coreFeatures, | ||
...options()._features, | ||
}, | ||
} | ||
|
||
const table = constructTable(resolvedOptions) | ||
|
||
// By default, manage table state here using the table's initial state | ||
const state = signal<TableState<TFeatures>>( | ||
getInitialTableState( | ||
resolvedOptions._features, | ||
resolvedOptions.initialState, | ||
), | ||
) | ||
|
||
// Compose table options using computed. | ||
// This is to allow `tableSignal` to listen and set table option | ||
const updatedOptions = computed<TableOptions<TFeatures, TData>>(() => { | ||
// listen to table state changed | ||
const tableState = state() | ||
// listen to input options changed | ||
const tableOptions = options() | ||
return { | ||
...table.options, | ||
...resolvedOptions, | ||
...tableOptions, | ||
state: { ...tableState, ...tableOptions.state }, | ||
onStateChange: (updater) => { | ||
const value = | ||
updater instanceof Function ? updater(tableState) : updater | ||
state.set(value) | ||
resolvedOptions.onStateChange?.(updater) | ||
}, | ||
} | ||
}) | ||
|
||
// convert table instance to signal for proxify to listen to any table state and options changes | ||
const tableSignal = computed( | ||
() => { | ||
table.setOptions(updatedOptions()) | ||
return table | ||
}, | ||
{ | ||
equal: () => false, | ||
}, | ||
) | ||
|
||
// proxify Table instance to provide ability for consumer to listen to any table state changes | ||
return proxifyTable(tableSignal) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Rows } from './rows/Rows' | ||
import { Cells } from './cells/Cells' | ||
import { Columns } from './columns/Columns' | ||
import { Headers } from './headers/Headers' | ||
import { Tables } from './table/Tables' | ||
|
||
export const coreFeatures = { Tables, Rows, Headers, Columns, Cells } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.