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

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.
package/dist/index.js ADDED
@@ -0,0 +1,176 @@
1
+ import { n as flexRender, r as renderSlots, t as FlexRender } from "./FlexRender-CdwT-Pu7.js";
2
+ import { constructTable, createColumnHelper } from "@tanstack/table-core";
3
+ import { cell, createContext, createMemo, createRenderEffect, createSignal, onCleanup, provide, read, readSlot, untrack } from "@barqjs/core";
4
+ import { batch, createAtom } from "@tanstack/store";
5
+ export * from "@tanstack/table-core";
6
+ //#region src/reactivity.ts
7
+ /**
8
+ * table-core writes an atom and reads it back in the same call (`setPageSize`
9
+ * derives the next page index from the state it has just written), and a barq
10
+ * signal write is staged until `flush()`. So the value lives in a TanStack
11
+ * Store atom, which is synchronous, and barq only carries a version signal per
12
+ * atom: `get()` reads the version to subscribe the running computation and
13
+ * returns the atom's own value.
14
+ */
15
+ function barqReactivity() {
16
+ const subscriptions = /* @__PURE__ */ new Set();
17
+ const tracked = (atom) => {
18
+ const [version, setVersion] = createSignal(0, { ownedWrite: true });
19
+ subscriptions.add(atom.subscribe(() => setVersion((count) => count + 1)));
20
+ return () => {
21
+ version();
22
+ return atom.get();
23
+ };
24
+ };
25
+ return {
26
+ createOptionsStore: true,
27
+ wrapExternalAtoms: false,
28
+ addSubscription: (subscription) => {
29
+ subscriptions.add(subscription);
30
+ },
31
+ unmount: () => {
32
+ subscriptions.forEach((subscription) => subscription.unsubscribe());
33
+ subscriptions.clear();
34
+ },
35
+ schedule: (fn) => queueMicrotask(fn),
36
+ batch,
37
+ untrack,
38
+ createReadonlyAtom: (fn, options) => {
39
+ const atom = createAtom(() => untrack(fn), { compare: options?.compare });
40
+ return {
41
+ get: tracked(atom),
42
+ subscribe: atom.subscribe
43
+ };
44
+ },
45
+ createWritableAtom: (value, options) => {
46
+ const atom = createAtom(value, { compare: options?.compare });
47
+ return {
48
+ get: tracked(atom),
49
+ set: (next) => {
50
+ atom.set(typeof next === "function" ? next : () => next);
51
+ },
52
+ subscribe: atom.subscribe
53
+ };
54
+ }
55
+ };
56
+ }
57
+ //#endregion
58
+ //#region src/createTable.ts
59
+ function mergeDescriptors(...sources) {
60
+ const merged = {};
61
+ for (const source of sources) Object.defineProperties(merged, Object.getOwnPropertyDescriptors(source));
62
+ return merged;
63
+ }
64
+ /**
65
+ * Creates a table whose APIs and atoms are tracked reads: call them inside JSX,
66
+ * `createMemo` or an effect and that computation follows the slice it read.
67
+ *
68
+ * Pass the options as an accessor to make them reactive. An options object is
69
+ * also accepted, and a getter on it (`get data() { return rows() }`) is read
70
+ * live, the way the Solid adapter's options are.
71
+ */
72
+ function createTable(tableOptions) {
73
+ const reactivity = barqReactivity();
74
+ const read = typeof tableOptions === "function" ? tableOptions : () => tableOptions;
75
+ const resolved = createMemo(() => {
76
+ const options = read();
77
+ const state = options.state;
78
+ if (state !== void 0) for (const key in state) state[key];
79
+ return mergeDescriptors(options, { features: {
80
+ coreReactivityFeature: reactivity,
81
+ ...options.features
82
+ } });
83
+ });
84
+ let applied = untrack(resolved);
85
+ const merge = (defaults, options) => mergeDescriptors(defaults, options);
86
+ const table = constructTable(mergeDescriptors({ mergeOptions: merge }, applied));
87
+ table.setOptions((previous) => merge(previous, applied));
88
+ createRenderEffect(resolved, (next) => {
89
+ if (next === applied) return;
90
+ applied = next;
91
+ untrack(() => {
92
+ table.setOptions((previous) => merge(previous, next));
93
+ });
94
+ });
95
+ onCleanup(() => reactivity.unmount?.());
96
+ table.FlexRender = FlexRender;
97
+ return table;
98
+ }
99
+ //#endregion
100
+ //#region src/createTableHook.ts
101
+ /**
102
+ * Binds features, default options and component registries once, and returns
103
+ * the table factory, column helper and context readers typed against them.
104
+ */
105
+ function createTableHook({ tableComponents, cellComponents, headerComponents, ...defaultTableOptions }) {
106
+ const TableContext = createContext(void 0, "barq-table");
107
+ const CellContext = createContext(void 0, "barq-table-cell");
108
+ const HeaderContext = createContext(void 0, "barq-table-header");
109
+ function createAppColumnHelper() {
110
+ return createColumnHelper();
111
+ }
112
+ function useTableContext() {
113
+ try {
114
+ return read(TableContext)();
115
+ } catch (cause) {
116
+ throw new Error("`useTableContext` must be used within an `AppTable` component. Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.", { cause });
117
+ }
118
+ }
119
+ function useCellContext() {
120
+ try {
121
+ return read(CellContext)();
122
+ } catch (cause) {
123
+ throw new Error("`useCellContext` must be used within an `AppCell` component. Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.", { cause });
124
+ }
125
+ }
126
+ function useHeaderContext() {
127
+ try {
128
+ return read(HeaderContext)();
129
+ } catch (cause) {
130
+ throw new Error("`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.", { cause });
131
+ }
132
+ }
133
+ const CellFlexRender = () => renderSlots({ cell: cell(useCellContext()) });
134
+ const HeaderFlexRender = () => renderSlots({ header: cell(useHeaderContext()) });
135
+ const FooterFlexRender = () => renderSlots({ footer: cell(useHeaderContext()) });
136
+ function createAppTable(tableOptions) {
137
+ const withDefaults = (options) => mergeDescriptors(defaultTableOptions, options);
138
+ const table = createTable(typeof tableOptions === "function" ? () => withDefaults(tableOptions()) : withDefaults(tableOptions));
139
+ const AppTable = (props) => provide(TableContext, cell(table), () => {
140
+ const children = props.children;
141
+ return typeof children === "function" ? children() : children;
142
+ });
143
+ const AppCell = (props) => {
144
+ const extended = Object.assign(readSlot(props.cell, "AppCell.cell"), {
145
+ FlexRender: CellFlexRender,
146
+ ...cellComponents
147
+ });
148
+ return provide(CellContext, cell(extended), () => props.children(extended));
149
+ };
150
+ const headerSlot = (origin, Render) => (props) => {
151
+ const extended = Object.assign(readSlot(props.header, origin), {
152
+ FlexRender: Render,
153
+ ...headerComponents
154
+ });
155
+ return provide(HeaderContext, cell(extended), () => props.children(extended));
156
+ };
157
+ return Object.assign(table, {
158
+ AppTable,
159
+ AppCell,
160
+ AppHeader: headerSlot("AppHeader.header", HeaderFlexRender),
161
+ AppFooter: headerSlot("AppFooter.header", FooterFlexRender),
162
+ FlexRender,
163
+ ...tableComponents
164
+ });
165
+ }
166
+ return {
167
+ appFeatures: defaultTableOptions.features,
168
+ createAppColumnHelper,
169
+ createAppTable,
170
+ useTableContext,
171
+ useCellContext,
172
+ useHeaderContext
173
+ };
174
+ }
175
+ //#endregion
176
+ export { FlexRender, createTable, createTableHook, flexRender };
@@ -0,0 +1 @@
1
+ export * from "@tanstack/table-core/static-functions";
@@ -0,0 +1,2 @@
1
+ export * from "@tanstack/table-core/static-functions";
2
+ export {};
package/package.json CHANGED
@@ -1,6 +1,87 @@
1
1
  {
2
2
  "name": "@barqjs/table",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0-next.0",
4
+ "description": "Headless tables for barq: createTable, FlexRender and createTableHook, on @tanstack/table-core",
5
+ "keywords": [
6
+ "barq",
7
+ "datagrid",
8
+ "headless",
9
+ "table",
10
+ "tanstack"
11
+ ],
12
+ "homepage": "https://github.com/salamaashoush/barq#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/salamaashoush/barq/issues"
15
+ },
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/salamaashoush/barq.git",
20
+ "directory": "packages/table"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "src",
25
+ "!src/**/*.test.ts",
26
+ "!src/**/*.test.tsx",
27
+ "!src/**/test-*.ts",
28
+ "!src/**/*-harness.ts",
29
+ "!src/**/*-probe.ts",
30
+ "!src/**/fixtures",
31
+ "!src/**/fixtures.ts",
32
+ "!src/**/ssr-fixtures.ts"
33
+ ],
34
+ "type": "module",
35
+ "sideEffects": false,
36
+ "main": "./dist/index.js",
37
+ "types": "./dist/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "bun": "./src/index.ts",
42
+ "import": "./dist/index.js"
43
+ },
44
+ "./experimental-worker-plugin": {
45
+ "types": "./dist/experimental-worker-plugin.d.ts",
46
+ "bun": "./src/experimental-worker-plugin.ts",
47
+ "import": "./dist/experimental-worker-plugin.js"
48
+ },
49
+ "./flex-render": {
50
+ "types": "./dist/flex-render.d.ts",
51
+ "bun": "./src/flex-render.ts",
52
+ "import": "./dist/flex-render.js"
53
+ },
54
+ "./static-functions": {
55
+ "types": "./dist/static-functions.d.ts",
56
+ "bun": "./src/static-functions.ts",
57
+ "import": "./dist/static-functions.js"
58
+ }
59
+ },
60
+ "publishConfig": {
61
+ "access": "public",
62
+ "provenance": true
63
+ },
64
+ "scripts": {
65
+ "build": "tsdown",
66
+ "dev": "tsdown --watch",
67
+ "test": "bun test --timeout 10000 && bun test --timeout 10000 --conditions=development",
68
+ "typecheck": "tsc --noEmit"
69
+ },
70
+ "dependencies": {
71
+ "@tanstack/store": "^0.11.2",
72
+ "@tanstack/table-core": "^9.3.1"
73
+ },
74
+ "devDependencies": {
75
+ "@barqjs/compiler": "0.2.0-next.0",
76
+ "@barqjs/core": "0.2.0-next.0",
77
+ "@barqjs/vite": "0.2.0-next.0",
78
+ "@barqjs/web": "0.2.0-next.0",
79
+ "@happy-dom/global-registrator": "^21.0.1",
80
+ "@types/bun": "^1.4.3",
81
+ "typescript": "^7.0.2"
82
+ },
83
+ "peerDependencies": {
84
+ "@barqjs/core": ">=0.2.0-next.0",
85
+ "@barqjs/web": ">=0.2.0-next.0"
86
+ }
87
+ }
@@ -0,0 +1,110 @@
1
+ import { type JSXElement, createMemo, untrack } from "@barqjs/core";
2
+ import type { Cell, CellData, Header, RowData, TableFeatures } from "@tanstack/table-core";
3
+
4
+ /**
5
+ * Renders a column template with its context: a function is called with the
6
+ * context, anything else is rendered as it is.
7
+ *
8
+ * The template runs untracked, as a component body does. What it reads while
9
+ * building is read once; a hole inside the markup it returns (or a function it
10
+ * returns) is what stays live.
11
+ */
12
+ export function flexRender<TProps>(
13
+ Comp: ((props: TProps) => JSXElement) | JSXElement | undefined,
14
+ props: TProps,
15
+ ): JSXElement {
16
+ if (Comp === null || Comp === undefined) return null;
17
+ if (typeof Comp === "function") return untrack(() => Comp(props));
18
+ return Comp;
19
+ }
20
+
21
+ export type FlexRenderProps<
22
+ TFeatures extends TableFeatures,
23
+ TData extends RowData,
24
+ TValue extends CellData = CellData,
25
+ > =
26
+ | { cell: Cell<TFeatures, TData, TValue>; header?: never; footer?: never }
27
+ | { header: Header<TFeatures, TData, TValue>; cell?: never; footer?: never }
28
+ | { footer: Header<TFeatures, TData, TValue>; cell?: never; header?: never };
29
+
30
+ /** What a call site's props arrive as: each one a read. */
31
+ export type FlexRenderSlots<
32
+ TFeatures extends TableFeatures,
33
+ TData extends RowData,
34
+ TValue extends CellData,
35
+ > = {
36
+ cell?: () => Cell<TFeatures, TData, TValue> | undefined;
37
+ header?: () => Header<TFeatures, TData, TValue> | undefined;
38
+ footer?: () => Header<TFeatures, TData, TValue> | undefined;
39
+ };
40
+
41
+ type GroupingCell = { getIsAggregated: () => boolean; getIsPlaceholder: () => boolean };
42
+
43
+ const NORMAL = 0;
44
+ const AGGREGATED = 1;
45
+ const PLACEHOLDER = 2;
46
+
47
+ export function renderSlots<
48
+ TFeatures extends TableFeatures,
49
+ TData extends RowData,
50
+ TValue extends CellData,
51
+ >(props: FlexRenderSlots<TFeatures, TData, TValue>): JSXElement {
52
+ const readCell = props.cell;
53
+ // Its own memo so a grouping change that leaves the mode where it was does
54
+ // not rebuild the cell's markup.
55
+ const mode =
56
+ readCell === undefined
57
+ ? undefined
58
+ : createMemo(() => {
59
+ // Both methods exist only with the grouping feature registered.
60
+ const cell: Partial<GroupingCell> | undefined = readCell();
61
+ if (cell === undefined) return NORMAL;
62
+ if (cell.getIsAggregated?.() === true) return AGGREGATED;
63
+ if (cell.getIsPlaceholder?.() === true) return PLACEHOLDER;
64
+ return NORMAL;
65
+ });
66
+
67
+ const content = createMemo((): JSXElement => {
68
+ const cell = readCell?.();
69
+ if (cell) {
70
+ const current = mode?.() ?? NORMAL;
71
+ if (current === PLACEHOLDER) return null;
72
+ const def = cell.column.columnDef as typeof cell.column.columnDef & {
73
+ aggregatedCell?: typeof cell.column.columnDef.cell;
74
+ };
75
+ const template = current === AGGREGATED ? (def.aggregatedCell ?? def.cell) : def.cell;
76
+ return flexRender(template as never, cell.getContext());
77
+ }
78
+
79
+ const header = props.header?.();
80
+ if (header) {
81
+ return flexRender(header.column.columnDef.header as never, header.getContext());
82
+ }
83
+
84
+ const footer = props.footer?.();
85
+ if (footer) {
86
+ return flexRender(footer.column.columnDef.footer as never, footer.getContext());
87
+ }
88
+
89
+ return null;
90
+ });
91
+
92
+ // A function child is a live hole; `JSXElement` has no member that says so.
93
+ return content as unknown as JSXElement;
94
+ }
95
+
96
+ /**
97
+ * `<FlexRender cell={cell} />`, `<FlexRender header={header} />` or
98
+ * `<FlexRender footer={footer} />`: the column's template for that position.
99
+ *
100
+ * With the grouping feature registered a cell renders `aggregatedCell` while it
101
+ * is aggregated and nothing while it is a placeholder, and switches when the
102
+ * grouping changes.
103
+ */
104
+ export const FlexRender = renderSlots as unknown as <
105
+ TFeatures extends TableFeatures,
106
+ TData extends RowData,
107
+ TValue extends CellData = CellData,
108
+ >(
109
+ props: FlexRenderProps<TFeatures, TData, TValue>,
110
+ ) => JSXElement;
@@ -0,0 +1,87 @@
1
+ import { createMemo, createRenderEffect, onCleanup, untrack } from "@barqjs/core";
2
+ import { constructTable } from "@tanstack/table-core";
3
+ import type { RowData, Table, TableFeatures, TableOptions } from "@tanstack/table-core";
4
+
5
+ import { FlexRender } from "./FlexRender.ts";
6
+ import { barqReactivity } from "./reactivity.ts";
7
+
8
+ export type BarqTable<TFeatures extends TableFeatures, TData extends RowData> = Table<
9
+ TFeatures,
10
+ TData
11
+ > & {
12
+ /** `<table.FlexRender cell={cell} />`, the same component `FlexRender` is. */
13
+ FlexRender: typeof FlexRender;
14
+ };
15
+
16
+ /** Options as they are, or an accessor that is re-applied when what it reads changes. */
17
+ export type MaybeAccessor<T> = T | (() => T);
18
+
19
+ // Descriptors, not values: an option written as a getter stays a live read on
20
+ // the merged object instead of being frozen at the value it had when merged.
21
+ export function mergeDescriptors(...sources: Array<object>): object {
22
+ const merged = {};
23
+ for (const source of sources) {
24
+ Object.defineProperties(merged, Object.getOwnPropertyDescriptors(source));
25
+ }
26
+ return merged;
27
+ }
28
+
29
+ /**
30
+ * Creates a table whose APIs and atoms are tracked reads: call them inside JSX,
31
+ * `createMemo` or an effect and that computation follows the slice it read.
32
+ *
33
+ * Pass the options as an accessor to make them reactive. An options object is
34
+ * also accepted, and a getter on it (`get data() { return rows() }`) is read
35
+ * live, the way the Solid adapter's options are.
36
+ */
37
+ export function createTable<TFeatures extends TableFeatures, TData extends RowData>(
38
+ tableOptions: MaybeAccessor<TableOptions<TFeatures, TData>>,
39
+ ): BarqTable<TFeatures, TData> {
40
+ const reactivity = barqReactivity();
41
+ const read =
42
+ typeof tableOptions === "function" ? tableOptions : (): typeof tableOptions => tableOptions;
43
+
44
+ const resolved = createMemo(() => {
45
+ const options = read();
46
+ // table-core resolves controlled state inside its own atoms, which a barq
47
+ // signal cannot invalidate. Reading every controlled slice here is what
48
+ // brings the options back through `setOptions` when one of them moves.
49
+ const state = options.state as Record<string, unknown> | undefined;
50
+ if (state !== undefined) {
51
+ for (const key in state) void state[key];
52
+ }
53
+ return mergeDescriptors(options, {
54
+ features: { coreReactivityFeature: reactivity, ...options.features },
55
+ }) as TableOptions<TFeatures, TData>;
56
+ });
57
+
58
+ let applied = untrack(resolved);
59
+
60
+ const merge = (
61
+ defaults: TableOptions<TFeatures, TData>,
62
+ options: TableOptions<TFeatures, TData>,
63
+ ): TableOptions<TFeatures, TData> =>
64
+ mergeDescriptors(defaults, options) as TableOptions<TFeatures, TData>;
65
+
66
+ const table = constructTable(
67
+ mergeDescriptors({ mergeOptions: merge }, applied) as TableOptions<TFeatures, TData>,
68
+ ) as BarqTable<TFeatures, TData>;
69
+
70
+ // table-core builds its first options by spreading, which reads every
71
+ // getter once. Merging again by descriptor puts the live reads back.
72
+ table.setOptions((previous) => merge(previous, applied));
73
+
74
+ createRenderEffect(resolved, (next) => {
75
+ if (next === applied) return;
76
+ applied = next;
77
+ untrack(() => {
78
+ table.setOptions((previous) => merge(previous, next));
79
+ });
80
+ });
81
+
82
+ onCleanup(() => reactivity.unmount?.());
83
+
84
+ table.FlexRender = FlexRender;
85
+
86
+ return table;
87
+ }