Skip to content

Commit e468632

Browse files
committed
feat: add existingTable param to other frameworks
this enables framework agnostic table adapters to create a table outside of the framework context, but then use the existing packages to interact with it as a regular table
1 parent 90eddf6 commit e468632

File tree

6 files changed

+33
-22
lines changed

6 files changed

+33
-22
lines changed

packages/angular-table/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export {
2626
} from './flex-render/flex-render-component'
2727

2828
export function createAngularTable<TData extends RowData>(
29-
options: () => TableOptions<TData>
29+
options: () => TableOptions<TData>,
30+
existingTable?: Table<TData>
3031
): Table<TData> & Signal<Table<TData>> {
3132
return lazyInit(() => {
3233
const resolvedOptions = {
@@ -36,7 +37,7 @@ export function createAngularTable<TData extends RowData>(
3637
...options(),
3738
}
3839

39-
const table = createTable(resolvedOptions)
40+
const table = existingTable ?? createTable(resolvedOptions)
4041

4142
// By default, manage table state here using the table's initial state
4243
const state = signal<TableState>(table.initialState)

packages/lit-table/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class TableController<TData extends RowData>
3636
;(this.host = host).addController(this)
3737
}
3838

39-
public table(options: TableOptions<TData>) {
39+
public table(options: TableOptions<TData>, existingTable?: Table<TData>) {
4040
if (!this.tableInstance) {
4141
const resolvedOptions: TableOptionsResolved<TData> = {
4242
state: {},
@@ -45,7 +45,7 @@ export class TableController<TData extends RowData>
4545
...options,
4646
}
4747

48-
this.tableInstance = createTable(resolvedOptions)
48+
this.tableInstance = existingTable ?? createTable(resolvedOptions)
4949
this._tableState = {
5050
...this.tableInstance.initialState,
5151
...options.state,

packages/qwik-table/src/index.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export function flexRender<TProps extends object>(
3131
}
3232

3333
export function useQwikTable<TData extends RowData>(
34-
options: TableOptions<TData>
34+
options: TableOptions<TData>,
35+
existingTable?: Table<TData>
3536
) {
3637
// Compose in the generic options to the user options
3738
const resolvedOptions: TableOptionsResolved<TData> = {
@@ -41,32 +42,38 @@ export function useQwikTable<TData extends RowData>(
4142
...options,
4243
}
4344

44-
// Create a new table instance and store it in a Qwik store
45-
const table = Qwik.useStore<{
46-
instance: Qwik.NoSerialize<Table<TData>>
45+
// Create or reuse the table instance and store it.
46+
const tableStore = Qwik.useStore<{
47+
instance: Qwik.NoSerialize<Table<TData>> | null
4748
}>({
48-
instance: Qwik.noSerialize(createTable(resolvedOptions)),
49+
instance: null,
4950
})
5051

52+
if (!tableStore.instance) {
53+
tableStore.instance = Qwik.noSerialize(
54+
existingTable ?? createTable(resolvedOptions)
55+
)
56+
}
57+
58+
const table = tableStore.instance!
59+
5160
// By default, manage table state here using the table's initial state
52-
const state = Qwik.useSignal(table.instance!.initialState)
61+
const state = Qwik.useSignal(table.initialState)
5362

5463
// Compose the default state above with any user state. This will allow the user
5564
// to only control a subset of the state if desired.
56-
table.instance!.setOptions(prev => ({
65+
table.setOptions(prev => ({
5766
...prev,
5867
...options,
5968
state: {
6069
...state.value,
6170
...options.state,
6271
},
63-
// Similarly, we'll maintain both our internal state and any user-provided
64-
// state.
6572
onStateChange: updater => {
6673
state.value = updater instanceof Function ? updater(state.value) : updater
6774
options.onStateChange?.(updater)
6875
},
6976
}))
7077

71-
return table.instance!
78+
return table
7279
}

packages/solid-table/src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export function flexRender<TProps>(
2525
}
2626

2727
export function createSolidTable<TData extends RowData>(
28-
options: TableOptions<TData>
28+
options: TableOptions<TData>,
29+
existingTable?: import('@tanstack/table-core').Table<TData>
2930
) {
3031
const resolvedOptions: TableOptionsResolved<TData> = mergeProps(
3132
{
@@ -42,7 +43,7 @@ export function createSolidTable<TData extends RowData>(
4243
options
4344
)
4445

45-
const table = createTable<TData>(resolvedOptions)
46+
const table = existingTable ?? createTable<TData>(resolvedOptions)
4647
const [state, setState] = createStore(table.initialState)
4748

4849
createComputed(() => {

packages/svelte-table/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export function flexRender(component: any, props: any): ComponentType | null {
6969
type ReadableOrVal<T> = T | Readable<T>
7070

7171
export function createSvelteTable<TData extends RowData>(
72-
options: ReadableOrVal<TableOptions<TData>>
72+
options: ReadableOrVal<TableOptions<TData>>,
73+
existingTable?: import('@tanstack/table-core').Table<TData>
7374
) {
7475
let optionsStore: Readable<TableOptions<TData>>
7576

@@ -86,7 +87,7 @@ export function createSvelteTable<TData extends RowData>(
8687
...get(optionsStore),
8788
}
8889

89-
let table = createTable(resolvedOptions)
90+
let table = existingTable ?? createTable(resolvedOptions)
9091

9192
let stateStore = writable(/** @type {number} */ table.initialState)
9293
// combine stores

packages/vue-table/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ function getOptionsWithReactiveData<TData extends RowData>(
5151
}
5252

5353
export function useVueTable<TData extends RowData>(
54-
initialOptions: TableOptionsWithReactiveData<TData>
54+
initialOptions: TableOptionsWithReactiveData<TData>,
55+
existingTable?: import('@tanstack/table-core').Table<TData>
5556
) {
5657
const IS_REACTIVE = isRef(initialOptions.data)
5758

@@ -75,9 +76,9 @@ export function useVueTable<TData extends RowData>(
7576
IS_REACTIVE ? getOptionsWithReactiveData(initialOptions) : initialOptions
7677
)
7778

78-
const table = createTable<TData>(
79-
resolvedOptions as TableOptionsResolved<TData>
80-
)
79+
const table =
80+
existingTable ??
81+
createTable<TData>(resolvedOptions as TableOptionsResolved<TData>)
8182

8283
// Add reactivity support
8384
if (IS_REACTIVE) {

0 commit comments

Comments
 (0)