@colspec/react 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Om Prakash Das
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md CHANGED
@@ -1,3 +1,90 @@
1
- # Temporary Holding Version
1
+ # @colspec/react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ React bindings for [colspec](https://github.com/Om1938/colspec): build a
4
+ TanStack Table from a JSON contract your backend serves.
5
+
6
+ ```sh
7
+ npm install @colspec/react @colspec/tanstack @colspec/core @tanstack/react-table
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```tsx
13
+ import { validateContract } from "@colspec/core";
14
+ import { ColspecProvider, useContractTable } from "@colspec/react";
15
+ import { createTableRegistry } from "@colspec/tanstack";
16
+
17
+ // 1. Register the functions contracts may refer to. Do this once.
18
+ const registry = createTableRegistry<Product>({
19
+ cells: {
20
+ "inventory.statusBadge": ({ getValue }) => <Badge status={getValue()} />,
21
+ },
22
+ });
23
+
24
+ // 2. Provide the registry.
25
+ export function App() {
26
+ return (
27
+ <ColspecProvider registry={registry}>
28
+ <Products />
29
+ </ColspecProvider>
30
+ );
31
+ }
32
+
33
+ // 3. Build the table from a validated contract.
34
+ function Products({
35
+ contract,
36
+ data,
37
+ }: {
38
+ contract: TableContract;
39
+ data: Product[];
40
+ }) {
41
+ const table = useContractTable<Product>({ contract, data });
42
+
43
+ return (
44
+ <table>
45
+ <thead>
46
+ {table.getHeaderGroups().map((group) => (
47
+ <tr key={group.id}>
48
+ {group.headers.map((header) => (
49
+ <th key={header.id}>
50
+ <table.FlexRender header={header} />
51
+ </th>
52
+ ))}
53
+ </tr>
54
+ ))}
55
+ </thead>
56
+ <tbody>
57
+ {table.getRowModel().rows.map((row) => (
58
+ <tr key={row.id}>
59
+ {row.getAllCells().map((cell) => (
60
+ <td key={cell.id}>
61
+ <table.FlexRender cell={cell} />
62
+ </td>
63
+ ))}
64
+ </tr>
65
+ ))}
66
+ </tbody>
67
+ </table>
68
+ );
69
+ }
70
+ ```
71
+
72
+ Fetch and validate the contract however you like (`validateContract` from
73
+ `@colspec/core`), and keep the validated object in state or your query cache.
74
+
75
+ ## API
76
+
77
+ | Export | Purpose |
78
+ | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
79
+ | `ColspecProvider` | Makes a registry available to the tables below it. |
80
+ | `useContractTable(options)` | Returns a TanStack table. Takes `contract` plus any TanStack table option; your options override what the contract sets. Throws a `ContractError` if the contract references something the registry lacks. |
81
+ | `useHydratedContract(contract)` | Returns `{ ok, value, diagnostics }` without throwing, so you can render a fallback. |
82
+ | `useRegistry()` | The registry from the nearest provider. |
83
+
84
+ `useContractTable` returns the ordinary TanStack table instance. Markup,
85
+ styling and data fetching stay yours.
86
+
87
+ Requires React 19 and `@tanstack/react-table` 9.
88
+
89
+ [Documentation](https://github.com/Om1938/colspec/tree/main/apps/docs/guide) ·
90
+ [NestJS + MongoDB + React example](https://github.com/Om1938/colspec/tree/main/examples) · MIT
@@ -0,0 +1,36 @@
1
+ import { ReactNode } from "react";
2
+ import { Result, TableContract } from "@colspec/core";
3
+ import { DefaultFeatures, HydratedContract, TableRegistry } from "@colspec/tanstack";
4
+ import { ReactTable, RowData, TableFeatures, TableOptions } from "@tanstack/react-table";
5
+ //#region src/registry-context.d.ts
6
+ export interface ColspecProviderProps {
7
+ /** Create this once at application start-up; its identity keys the hydration cache. */
8
+ registry: TableRegistry;
9
+ children: ReactNode;
10
+ }
11
+ /** Makes the application's registry available to every contract-driven table. */
12
+ export declare function ColspecProvider({ registry, children }: ColspecProviderProps): import("react").JSX.Element;
13
+ export declare function useRegistry(): TableRegistry;
14
+ //#endregion
15
+ //#region src/use-contract-table.d.ts
16
+ export type ContractTableOptions<TFeatures extends TableFeatures, TData extends RowData> = Omit<TableOptions<TFeatures, TData>, "columns" | "features"> & {
17
+ contract: TableContract;
18
+ /** Defaults to `defaultFeatures` from `@colspec/tanstack`. */
19
+ features?: TFeatures;
20
+ };
21
+ /**
22
+ * Creates a TanStack table whose columns, initial state and manual modes come
23
+ * from a contract. Any other table option can be passed and takes precedence.
24
+ *
25
+ * Throws a `ContractError` when the contract references something the
26
+ * registry lacks; use `useHydratedContract` to handle that without throwing.
27
+ */
28
+ export declare function useContractTable<TData extends RowData, TFeatures extends TableFeatures = DefaultFeatures>({ contract, features, initialState, ...tableOptions }: ContractTableOptions<TFeatures, TData>): ReactTable<TFeatures, TData>;
29
+ //#endregion
30
+ //#region src/use-hydrated-contract.d.ts
31
+ /**
32
+ * Hydrates a validated contract with the registry from `<ColspecProvider>`.
33
+ * Returns diagnostics instead of throwing, so callers can render a fallback.
34
+ */
35
+ export declare function useHydratedContract<TFeatures extends TableFeatures = any, TData extends RowData = any>(contract: TableContract): Result<HydratedContract<TFeatures, TData>>;
36
+ //#endregion
package/dist/index.mjs ADDED
@@ -0,0 +1,55 @@
1
+ import { createContext, useContext } from "react";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import { ContractError } from "@colspec/core";
4
+ import { defaultFeatures, hydrateContract } from "@colspec/tanstack";
5
+ import { useTable } from "@tanstack/react-table";
6
+ //#region src/registry-context.tsx
7
+ const RegistryContext = createContext(void 0);
8
+ /** Makes the application's registry available to every contract-driven table. */
9
+ function ColspecProvider({ registry, children }) {
10
+ return /* @__PURE__ */ jsx(RegistryContext, {
11
+ value: registry,
12
+ children
13
+ });
14
+ }
15
+ function useRegistry() {
16
+ const registry = useContext(RegistryContext);
17
+ if (!registry) throw new Error("useRegistry must be used inside a <ColspecProvider>.");
18
+ return registry;
19
+ }
20
+ //#endregion
21
+ //#region src/use-hydrated-contract.ts
22
+ /**
23
+ * Hydrates a validated contract with the registry from `<ColspecProvider>`.
24
+ * Returns diagnostics instead of throwing, so callers can render a fallback.
25
+ */
26
+ function useHydratedContract(contract) {
27
+ return hydrateContract(contract, useRegistry());
28
+ }
29
+ //#endregion
30
+ //#region src/use-contract-table.ts
31
+ /**
32
+ * Creates a TanStack table whose columns, initial state and manual modes come
33
+ * from a contract. Any other table option can be passed and takes precedence.
34
+ *
35
+ * Throws a `ContractError` when the contract references something the
36
+ * registry lacks; use `useHydratedContract` to handle that without throwing.
37
+ */
38
+ function useContractTable({ contract, features = defaultFeatures, initialState, ...tableOptions }) {
39
+ const hydrated = useHydratedContract(contract);
40
+ if (!hydrated.ok) throw new ContractError(hydrated.diagnostics);
41
+ const { columns, options } = hydrated.value;
42
+ const merged = {
43
+ ...options,
44
+ ...tableOptions,
45
+ initialState: {
46
+ ...options.initialState,
47
+ ...initialState
48
+ },
49
+ features,
50
+ columns
51
+ };
52
+ return useTable(merged);
53
+ }
54
+ //#endregion
55
+ export { ColspecProvider, useContractTable, useHydratedContract, useRegistry };
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@colspec/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.1",
4
+ "peerDependencies": {
5
+ "@tanstack/react-table": "^9.2.6",
6
+ "react": "^19.3.0"
7
+ },
8
+ "dependencies": {
9
+ "@colspec/core": "0.1.1",
10
+ "@colspec/tanstack": "0.1.1"
11
+ },
12
+ "devDependencies": {
13
+ "@repo/eslint-config": "0.0.0",
14
+ "@repo/typescript-config": "0.0.0",
15
+ "@tanstack/react-table": "^9.2.6",
16
+ "react": "^19.3.0",
17
+ "react-dom": "^19.3.0"
18
+ },
19
+ "type": "module",
20
+ "license": "MIT",
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "sideEffects": false,
28
+ "exports": {
29
+ ".": "./dist/index.mjs",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "description": "React provider and hooks for building TanStack Table instances from colspec JSON contracts.",
33
+ "author": "Om Prakash Das",
34
+ "homepage": "https://github.com/Om1938/colspec/tree/main/packages/react#readme",
35
+ "bugs": {
36
+ "url": "https://github.com/Om1938/colspec/issues"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/Om1938/colspec.git",
41
+ "directory": "packages/react"
42
+ },
43
+ "keywords": [
44
+ "tanstack-table",
45
+ "tanstack",
46
+ "table",
47
+ "datagrid",
48
+ "dynamic-columns",
49
+ "colspec",
50
+ "react hooks react-table"
51
+ ],
52
+ "scripts": {
53
+ "lint": "eslint . --max-warnings 0",
54
+ "build": "tsdown",
55
+ "test": "vitest run",
56
+ "check-types": "tsc --noEmit"
57
+ }
6
58
  }