Open History to see the last commit
1import { constructTable } from '@tanstack/table-core'
2import {
3 createComputed,
4 getOwner,
5 mergeProps,
6 onCleanup,
7 untrack,
8} from 'solid-js'
9import { FlexRender } from './FlexRender'
10import { solidReactivity } from './reactivity'
11import type { JSX } from 'solid-js'
12import type {
13 RowData,
14 Table,
15 TableFeatures,
16 TableOptions,
17} from '@tanstack/table-core'
18β
19export type SolidTable<
20 TFeatures extends TableFeatures,
21 TData extends RowData,
22> = Table<TFeatures, TData> & {
23 /**
24 * @deprecated Read table APIs or `table.atoms` directly inside JSX,
25 * `createMemo`, or `createEffect`. Solid tracks those reads natively.
26 * This compatibility wrapper only passes atoms to its child function;
27 * it does not create a subscription or tracking scope.
28 */
29 Subscribe: (props: {
30 children: (atoms: Table<TFeatures, TData>['atoms']) => JSX.Element
31 }) => JSX.Element
32 /**
33 * Convenience FlexRender component attached to the table instance for
34 * rendering headers, cells, or footers with custom markup. Mirrors the
35 * `table.FlexRender` API exposed by `createTableHook`'s `createAppTable`.
36 *
37 * @example
38 * <table.FlexRender header={header} />
39 * <table.FlexRender cell={cell} />
40 * <table.FlexRender footer={footer} />
41 */
42 FlexRender: typeof FlexRender
43}
44β
45/**
46 * Creates a Solid table instance backed by Solid-aware TanStack Store atoms.
47 *
48 * Table APIs and atom reads participate in Solid dependency tracking, so
49 * computations that read a specific slice can update without invalidating
50 * unrelated UI. Read table APIs or atoms inside JSX, `createMemo`, or
51 * `createEffect` to track updates.
52 *
53 * @example
54 * ```tsx
55 * const table = createTable(
56 * {
57 * features,
58 * columns,
59 * data,
60 * },
61 * )
62 * ```
63 */
64export function createTable<
65 TFeatures extends TableFeatures,
66 TData extends RowData,
67>(tableOptions: TableOptions<TFeatures, TData>): SolidTable<TFeatures, TData> {
68 const owner = getOwner()!
69 const reactivity = solidReactivity(owner)
70β
71 const mergedOptions = mergeProps(tableOptions, {
72 features: {
73 coreReactivityFeature: reactivity,
74 ...tableOptions.features,
75 },
76 }) as any
77β
78 const resolvedOptions = mergeProps(
79 {
80 mergeOptions: (
81 defaultOptions: TableOptions<TFeatures, TData>,
82 options: TableOptions<TFeatures, TData>,
83 ) => {
84 return mergeProps(defaultOptions, options)
85 },
86 },
87 mergedOptions,
88 ) as TableOptions<TFeatures, TData>
89β
90 const table = constructTable(resolvedOptions) as unknown as SolidTable<
91 TFeatures,
92 TData
93 >
94β
95 createComputed(() => {
96 const userState = tableOptions.state
97 if (userState) {
98 for (const key in userState) {
99 void (userState as Record<string, unknown>)[key]
100 }
101 }
102β
103 untrack(() => {
104 table.setOptions((prev) => {
105 return mergeProps(prev, mergedOptions) as TableOptions<TFeatures, TData>
106 })
107 })
108 })
109β
110 onCleanup(() => reactivity.unmount?.())
111β
112 table.Subscribe = (props: {
113 children: (atoms: Table<TFeatures, TData>['atoms']) => JSX.Element
114 }) => {
115 return props.children(table.atoms as Table<TFeatures, TData>['atoms'])
116 }
117β
118 table.FlexRender = FlexRender
119β
120 return table
121}