@barqjs/table 0.0.0-stage → 0.2.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,431 @@
1
+ import {
2
+ type JSXElement,
3
+ cell as constant,
4
+ createContext,
5
+ provide,
6
+ read,
7
+ readSlot,
8
+ } from "@barqjs/core";
9
+ import { createColumnHelper as coreCreateColumnHelper } from "@tanstack/table-core";
10
+ import type {
11
+ AccessorFn,
12
+ AccessorFnColumnDef,
13
+ AccessorKeyColumnDef,
14
+ Cell,
15
+ CellContext,
16
+ CellData,
17
+ Column,
18
+ ColumnDef,
19
+ ColumnHelper,
20
+ DeepKeys,
21
+ DeepValue,
22
+ DisplayColumnDef,
23
+ GroupColumnDef,
24
+ Header,
25
+ IdentifiedColumnDef,
26
+ NoInfer,
27
+ Row,
28
+ RowData,
29
+ Table,
30
+ TableFeatures,
31
+ TableOptions,
32
+ } from "@tanstack/table-core";
33
+
34
+ import {
35
+ type BarqTable,
36
+ type MaybeAccessor,
37
+ createTable,
38
+ mergeDescriptors,
39
+ } from "./createTable.ts";
40
+ import { FlexRender, renderSlots } from "./FlexRender.ts";
41
+
42
+ export type ComponentType<TProps> = (props: TProps) => JSXElement;
43
+
44
+ // `never` props: a registry holds components of unrelated props, and every
45
+ // component accepts being typed as one that is never called.
46
+ type Components = Record<string, ComponentType<never>>;
47
+
48
+ export type AppCellContext<
49
+ TFeatures extends TableFeatures,
50
+ TData extends RowData,
51
+ TValue extends CellData,
52
+ TCellComponents extends Components,
53
+ > = {
54
+ cell: Cell<TFeatures, TData, TValue> & TCellComponents & { FlexRender: () => JSXElement };
55
+ column: Column<TFeatures, TData, TValue>;
56
+ getValue: CellContext<TFeatures, TData, TValue>["getValue"];
57
+ renderValue: CellContext<TFeatures, TData, TValue>["renderValue"];
58
+ row: Row<TFeatures, TData>;
59
+ table: Table<TFeatures, TData>;
60
+ };
61
+
62
+ export type AppHeaderContext<
63
+ TFeatures extends TableFeatures,
64
+ TData extends RowData,
65
+ TValue extends CellData,
66
+ THeaderComponents extends Components,
67
+ > = {
68
+ column: Column<TFeatures, TData, TValue>;
69
+ header: Header<TFeatures, TData, TValue> & THeaderComponents & { FlexRender: () => JSXElement };
70
+ table: Table<TFeatures, TData>;
71
+ };
72
+
73
+ export type AppColumnDefTemplate<TProps extends object> = string | ((props: TProps) => unknown);
74
+
75
+ export type AppColumnDefBase<
76
+ TFeatures extends TableFeatures,
77
+ TData extends RowData,
78
+ TValue extends CellData,
79
+ TCellComponents extends Components,
80
+ THeaderComponents extends Components,
81
+ > = Omit<IdentifiedColumnDef<TFeatures, TData, TValue>, "cell" | "header" | "footer"> & {
82
+ cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, TValue, TCellComponents>>;
83
+ header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>>;
84
+ footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, TValue, THeaderComponents>>;
85
+ };
86
+
87
+ export type AppDisplayColumnDef<
88
+ TFeatures extends TableFeatures,
89
+ TData extends RowData,
90
+ TCellComponents extends Components,
91
+ THeaderComponents extends Components,
92
+ > = Omit<DisplayColumnDef<TFeatures, TData>, "cell" | "header" | "footer"> & {
93
+ cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, unknown, TCellComponents>>;
94
+ header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
95
+ footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
96
+ };
97
+
98
+ export type AppGroupColumnDef<
99
+ TFeatures extends TableFeatures,
100
+ TData extends RowData,
101
+ TCellComponents extends Components,
102
+ THeaderComponents extends Components,
103
+ > = Omit<GroupColumnDef<TFeatures, TData>, "cell" | "header" | "footer" | "columns"> & {
104
+ cell?: AppColumnDefTemplate<AppCellContext<TFeatures, TData, unknown, TCellComponents>>;
105
+ header?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
106
+ footer?: AppColumnDefTemplate<AppHeaderContext<TFeatures, TData, unknown, THeaderComponents>>;
107
+ columns?: ReadonlyArray<ColumnDef<TFeatures, TData>>;
108
+ };
109
+
110
+ export type AppColumnHelper<
111
+ TFeatures extends TableFeatures,
112
+ TData extends RowData,
113
+ TCellComponents extends Components,
114
+ THeaderComponents extends Components,
115
+ > = {
116
+ accessor: <
117
+ TAccessor extends AccessorFn<TData> | DeepKeys<TData>,
118
+ TValue extends (TAccessor extends AccessorFn<TData, infer TReturn>
119
+ ? TReturn
120
+ : TAccessor extends DeepKeys<TData>
121
+ ? DeepValue<TData, TAccessor>
122
+ : never),
123
+ >(
124
+ accessor: TAccessor,
125
+ column: TAccessor extends AccessorFn<TData>
126
+ ? AppColumnDefBase<TFeatures, TData, TValue, TCellComponents, THeaderComponents> & {
127
+ id: string;
128
+ }
129
+ : AppColumnDefBase<TFeatures, TData, TValue, TCellComponents, THeaderComponents>,
130
+ ) => TAccessor extends AccessorFn<TData>
131
+ ? AccessorFnColumnDef<TFeatures, TData, TValue>
132
+ : AccessorKeyColumnDef<TFeatures, TData, TValue>;
133
+
134
+ columns: ColumnHelper<TFeatures, TData>["columns"];
135
+
136
+ display: (
137
+ column: AppDisplayColumnDef<TFeatures, TData, TCellComponents, THeaderComponents>,
138
+ ) => DisplayColumnDef<TFeatures, TData>;
139
+
140
+ group: (
141
+ column: AppGroupColumnDef<TFeatures, TData, TCellComponents, THeaderComponents>,
142
+ ) => GroupColumnDef<TFeatures, TData>;
143
+ };
144
+
145
+ /**
146
+ * `TRow` is inferred from the default options that take a row, such as
147
+ * `getRowId: (row: Person) => row.id`, and bounds the row type of every table
148
+ * the hook makes. With no such option it is `RowData` and bounds nothing.
149
+ */
150
+ export type CreateTableHookOptions<
151
+ TFeatures extends TableFeatures,
152
+ TTableComponents extends Components,
153
+ TCellComponents extends Components,
154
+ THeaderComponents extends Components,
155
+ TRow extends RowData = RowData,
156
+ > = Omit<TableOptions<TFeatures, TRow>, "columns" | "data" | "store" | "state" | "initialState"> & {
157
+ /** Reached as `table.Name` on a table made by `createAppTable`. */
158
+ tableComponents?: TTableComponents;
159
+ /** Reached as `cell.Name` inside `AppCell` and in a column's `cell` template. */
160
+ cellComponents?: TCellComponents;
161
+ /** Reached as `header.Name` inside `AppHeader`/`AppFooter` and in a column's templates. */
162
+ headerComponents?: THeaderComponents;
163
+ };
164
+
165
+ export interface AppTableProps {
166
+ children: JSXElement;
167
+ }
168
+
169
+ export interface AppCellProps<
170
+ TFeatures extends TableFeatures,
171
+ TData extends RowData,
172
+ TValue extends CellData,
173
+ TCellComponents extends Components,
174
+ > {
175
+ cell: Cell<TFeatures, TData, TValue>;
176
+ children: (
177
+ cell: Cell<TFeatures, TData, TValue> & TCellComponents & { FlexRender: () => JSXElement },
178
+ ) => JSXElement;
179
+ }
180
+
181
+ export interface AppHeaderProps<
182
+ TFeatures extends TableFeatures,
183
+ TData extends RowData,
184
+ TValue extends CellData,
185
+ THeaderComponents extends Components,
186
+ > {
187
+ header: Header<TFeatures, TData, TValue>;
188
+ children: (
189
+ header: Header<TFeatures, TData, TValue> & THeaderComponents & { FlexRender: () => JSXElement },
190
+ ) => JSXElement;
191
+ }
192
+
193
+ export interface AppCellComponent<
194
+ TFeatures extends TableFeatures,
195
+ TData extends RowData,
196
+ TCellComponents extends Components,
197
+ > {
198
+ <TValue extends CellData = CellData>(
199
+ props: AppCellProps<TFeatures, TData, TValue, TCellComponents>,
200
+ ): JSXElement;
201
+ }
202
+
203
+ export interface AppHeaderComponent<
204
+ TFeatures extends TableFeatures,
205
+ TData extends RowData,
206
+ THeaderComponents extends Components,
207
+ > {
208
+ <TValue extends CellData = CellData>(
209
+ props: AppHeaderProps<TFeatures, TData, TValue, THeaderComponents>,
210
+ ): JSXElement;
211
+ }
212
+
213
+ export type AppTableComponent = (props: AppTableProps) => JSXElement;
214
+
215
+ export type AppBarqTable<
216
+ TFeatures extends TableFeatures,
217
+ TData extends RowData,
218
+ TTableComponents extends Components,
219
+ TCellComponents extends Components,
220
+ THeaderComponents extends Components,
221
+ > = BarqTable<TFeatures, TData> &
222
+ NoInfer<TTableComponents> & {
223
+ /** Provides the table to `useTableContext` below it. */
224
+ AppTable: AppTableComponent;
225
+ /** Provides the cell to `useCellContext` and hands it over with the cell components on it. */
226
+ AppCell: AppCellComponent<TFeatures, TData, NoInfer<TCellComponents>>;
227
+ /** Provides the header to `useHeaderContext` and hands it over with the header components on it. */
228
+ AppHeader: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>;
229
+ /** `AppHeader`, with `FlexRender` rendering the column's footer template. */
230
+ AppFooter: AppHeaderComponent<TFeatures, TData, NoInfer<THeaderComponents>>;
231
+ FlexRender: typeof FlexRender;
232
+ };
233
+
234
+ export interface CreateTableHookResult<
235
+ TFeatures extends TableFeatures,
236
+ TTableComponents extends Components,
237
+ TCellComponents extends Components,
238
+ THeaderComponents extends Components,
239
+ TRow extends RowData = RowData,
240
+ > {
241
+ appFeatures: TFeatures;
242
+ createAppColumnHelper: <TData extends TRow>() => AppColumnHelper<
243
+ TFeatures,
244
+ TData,
245
+ TCellComponents,
246
+ THeaderComponents
247
+ >;
248
+ createAppTable: <TData extends TRow>(
249
+ tableOptions: MaybeAccessor<Omit<TableOptions<TFeatures, TData>, "features">>,
250
+ ) => AppBarqTable<TFeatures, TData, TTableComponents, TCellComponents, THeaderComponents>;
251
+ useTableContext: <TData extends TRow = TRow>() => AppBarqTable<
252
+ TFeatures,
253
+ TData,
254
+ TTableComponents,
255
+ TCellComponents,
256
+ THeaderComponents
257
+ >;
258
+ useCellContext: <TValue extends CellData = CellData>() => Cell<TFeatures, RowData, TValue> &
259
+ TCellComponents & { FlexRender: () => JSXElement };
260
+ useHeaderContext: <TValue extends CellData = CellData>() => Header<TFeatures, RowData, TValue> &
261
+ THeaderComponents & { FlexRender: () => JSXElement };
262
+ }
263
+
264
+ /**
265
+ * Binds features, default options and component registries once, and returns
266
+ * the table factory, column helper and context readers typed against them.
267
+ */
268
+ export function createTableHook<
269
+ TFeatures extends TableFeatures,
270
+ const TTableComponents extends Components,
271
+ const TCellComponents extends Components,
272
+ const THeaderComponents extends Components,
273
+ TRow extends RowData = RowData,
274
+ >({
275
+ tableComponents,
276
+ cellComponents,
277
+ headerComponents,
278
+ ...defaultTableOptions
279
+ }: CreateTableHookOptions<
280
+ TFeatures,
281
+ TTableComponents,
282
+ TCellComponents,
283
+ THeaderComponents,
284
+ TRow
285
+ >): CreateTableHookResult<TFeatures, TTableComponents, TCellComponents, THeaderComponents, TRow> {
286
+ // Typed on the way out: a table is invariant in its row type, so no one
287
+ // context type holds the table of every `createAppTable` call.
288
+ const TableContext = createContext<unknown>(undefined, "barq-table");
289
+ const CellContext = createContext<unknown>(undefined, "barq-table-cell");
290
+ const HeaderContext = createContext<unknown>(undefined, "barq-table-header");
291
+
292
+ function createAppColumnHelper<TData extends TRow>(): AppColumnHelper<
293
+ TFeatures,
294
+ TData,
295
+ TCellComponents,
296
+ THeaderComponents
297
+ > {
298
+ return coreCreateColumnHelper<TFeatures, TData>() as AppColumnHelper<
299
+ TFeatures,
300
+ TData,
301
+ TCellComponents,
302
+ THeaderComponents
303
+ >;
304
+ }
305
+
306
+ function useTableContext<TData extends TRow = TRow>(): AppBarqTable<
307
+ TFeatures,
308
+ TData,
309
+ TTableComponents,
310
+ TCellComponents,
311
+ THeaderComponents
312
+ > {
313
+ try {
314
+ return read(TableContext)() as AppBarqTable<
315
+ TFeatures,
316
+ TData,
317
+ TTableComponents,
318
+ TCellComponents,
319
+ THeaderComponents
320
+ >;
321
+ } catch (cause) {
322
+ throw new Error(
323
+ "`useTableContext` must be used within an `AppTable` component. " +
324
+ "Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.",
325
+ { cause },
326
+ );
327
+ }
328
+ }
329
+
330
+ function useCellContext<TValue extends CellData = CellData>(): Cell<TFeatures, RowData, TValue> &
331
+ TCellComponents & { FlexRender: () => JSXElement } {
332
+ try {
333
+ return read(CellContext)() as Cell<TFeatures, RowData, TValue> &
334
+ TCellComponents & { FlexRender: () => JSXElement };
335
+ } catch (cause) {
336
+ throw new Error(
337
+ "`useCellContext` must be used within an `AppCell` component. " +
338
+ "Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.",
339
+ { cause },
340
+ );
341
+ }
342
+ }
343
+
344
+ function useHeaderContext<TValue extends CellData = CellData>(): Header<
345
+ TFeatures,
346
+ RowData,
347
+ TValue
348
+ > &
349
+ THeaderComponents & { FlexRender: () => JSXElement } {
350
+ try {
351
+ return read(HeaderContext)() as Header<TFeatures, RowData, TValue> &
352
+ THeaderComponents & { FlexRender: () => JSXElement };
353
+ } catch (cause) {
354
+ throw new Error(
355
+ "`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.",
356
+ { cause },
357
+ );
358
+ }
359
+ }
360
+
361
+ const CellFlexRender = (): JSXElement => renderSlots({ cell: constant(useCellContext()) });
362
+ const HeaderFlexRender = (): JSXElement => renderSlots({ header: constant(useHeaderContext()) });
363
+ const FooterFlexRender = (): JSXElement => renderSlots({ footer: constant(useHeaderContext()) });
364
+
365
+ function createAppTable<TData extends TRow>(
366
+ tableOptions: MaybeAccessor<Omit<TableOptions<TFeatures, TData>, "features">>,
367
+ ): AppBarqTable<TFeatures, TData, TTableComponents, TCellComponents, THeaderComponents> {
368
+ const withDefaults = (options: object): TableOptions<TFeatures, TData> =>
369
+ mergeDescriptors(defaultTableOptions, options) as TableOptions<TFeatures, TData>;
370
+
371
+ const table = createTable<TFeatures, TData>(
372
+ typeof tableOptions === "function"
373
+ ? () => withDefaults(tableOptions())
374
+ : withDefaults(tableOptions),
375
+ );
376
+
377
+ const AppTable = (props: { children?: unknown }): unknown =>
378
+ provide(TableContext, constant(table), () => {
379
+ const children = props.children;
380
+ return typeof children === "function" ? (children as () => unknown)() : children;
381
+ });
382
+
383
+ const AppCell = (props: {
384
+ cell: () => Cell<TFeatures, TData>;
385
+ children: AppCellProps<TFeatures, TData, CellData, TCellComponents>["children"];
386
+ }): unknown => {
387
+ const extended = Object.assign(
388
+ readSlot(props.cell, "AppCell.cell") as Cell<TFeatures, TData>,
389
+ { FlexRender: CellFlexRender, ...cellComponents },
390
+ ) as Cell<TFeatures, TData> & TCellComponents & { FlexRender: () => JSXElement };
391
+ return provide(CellContext, constant(extended), () => props.children(extended));
392
+ };
393
+
394
+ const headerSlot =
395
+ (origin: string, Render: () => JSXElement) =>
396
+ (props: {
397
+ header: () => Header<TFeatures, TData>;
398
+ children: AppHeaderProps<TFeatures, TData, CellData, THeaderComponents>["children"];
399
+ }): unknown => {
400
+ const extended = Object.assign(readSlot(props.header, origin) as Header<TFeatures, TData>, {
401
+ FlexRender: Render,
402
+ ...headerComponents,
403
+ }) as Header<TFeatures, TData> & THeaderComponents & { FlexRender: () => JSXElement };
404
+ return provide(HeaderContext, constant(extended), () => props.children(extended));
405
+ };
406
+
407
+ return Object.assign(table, {
408
+ AppTable,
409
+ AppCell,
410
+ AppHeader: headerSlot("AppHeader.header", HeaderFlexRender),
411
+ AppFooter: headerSlot("AppFooter.header", FooterFlexRender),
412
+ FlexRender,
413
+ ...tableComponents,
414
+ }) as unknown as AppBarqTable<
415
+ TFeatures,
416
+ TData,
417
+ TTableComponents,
418
+ TCellComponents,
419
+ THeaderComponents
420
+ >;
421
+ }
422
+
423
+ return {
424
+ appFeatures: defaultTableOptions.features,
425
+ createAppColumnHelper,
426
+ createAppTable,
427
+ useTableContext,
428
+ useCellContext,
429
+ useHeaderContext,
430
+ };
431
+ }
@@ -0,0 +1 @@
1
+ export * from "@tanstack/table-core/experimental-worker-plugin";
@@ -0,0 +1 @@
1
+ export { type FlexRenderProps, FlexRender, flexRender } from "./FlexRender.ts";
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "@tanstack/table-core";
2
+
3
+ export { type BarqTable, type MaybeAccessor, createTable } from "./createTable.ts";
4
+ export { type FlexRenderProps, FlexRender, flexRender } from "./FlexRender.ts";
5
+ export * from "./createTableHook.ts";
@@ -0,0 +1,62 @@
1
+ import { createSignal, untrack } from "@barqjs/core";
2
+ import { batch, createAtom } from "@tanstack/store";
3
+ import type { Atom, ReadonlyAtom, Subscription } from "@tanstack/store";
4
+ import type { TableAtomOptions, TableReactivityBindings } from "@tanstack/table-core/reactivity";
5
+
6
+ /**
7
+ * table-core writes an atom and reads it back in the same call (`setPageSize`
8
+ * derives the next page index from the state it has just written), and a barq
9
+ * signal write is staged until `flush()`. So the value lives in a TanStack
10
+ * Store atom, which is synchronous, and barq only carries a version signal per
11
+ * atom: `get()` reads the version to subscribe the running computation and
12
+ * returns the atom's own value.
13
+ */
14
+ export function barqReactivity(): TableReactivityBindings {
15
+ const subscriptions = new Set<Subscription>();
16
+
17
+ const tracked = <T>(atom: ReadonlyAtom<T>): (() => T) => {
18
+ // The first notification can arrive while the component that owns the
19
+ // table is still being built, which the engine's development build treats
20
+ // as a write inside an owned scope unless the signal opts in.
21
+ const [version, setVersion] = createSignal(0, { ownedWrite: true });
22
+ subscriptions.add(atom.subscribe(() => setVersion((count) => count + 1)));
23
+ return () => {
24
+ version();
25
+ return atom.get();
26
+ };
27
+ };
28
+
29
+ return {
30
+ createOptionsStore: true,
31
+ wrapExternalAtoms: false,
32
+ addSubscription: (subscription) => {
33
+ subscriptions.add(subscription);
34
+ },
35
+ unmount: () => {
36
+ subscriptions.forEach((subscription) => subscription.unsubscribe());
37
+ subscriptions.clear();
38
+ },
39
+ schedule: (fn) => queueMicrotask(fn),
40
+ batch,
41
+ untrack,
42
+ createReadonlyAtom: <T>(fn: () => T, options?: TableAtomOptions<T>): ReadonlyAtom<T> => {
43
+ // Untracked on the barq side: a computation that reads this atom is
44
+ // woken by this atom's version, not by every atom the resolver read.
45
+ const atom = createAtom(() => untrack(fn), { compare: options?.compare });
46
+ return { get: tracked(atom), subscribe: atom.subscribe };
47
+ },
48
+ createWritableAtom: <T>(value: T, options?: TableAtomOptions<T>): Atom<T> => {
49
+ const atom = createAtom(value, { compare: options?.compare });
50
+ return {
51
+ get: tracked(atom),
52
+ // A Store atom drops `set(undefined)`, and table-core resets a slice
53
+ // whose initial state is `undefined` (the global filter) that way. An
54
+ // updater that returns the value is written whatever it is.
55
+ set: (next: T | ((previous: T) => T)) => {
56
+ atom.set(typeof next === "function" ? (next as (previous: T) => T) : () => next);
57
+ },
58
+ subscribe: atom.subscribe,
59
+ };
60
+ },
61
+ };
62
+ }
@@ -0,0 +1 @@
1
+ export * from "@tanstack/table-core/static-functions";