@toolbox-web/grid-react 0.0.2

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,107 @@
1
+ import { CellRenderContext, ColumnEditorContext, ColumnEditorSpec, ColumnViewRenderer, FrameworkAdapter } from '../../../../dist/libs/grid/index.d.ts';
2
+ import { ReactNode } from 'react';
3
+ /**
4
+ * Register a React cell renderer for a column element.
5
+ * Called by GridColumn when it has a children render prop.
6
+ */
7
+ export declare function registerColumnRenderer(element: HTMLElement, renderer: (ctx: CellRenderContext<unknown, unknown>) => ReactNode): void;
8
+ /**
9
+ * Register a React cell editor for a column element.
10
+ * Called by GridColumn when it has an editor prop.
11
+ */
12
+ export declare function registerColumnEditor(element: HTMLElement, editor: (ctx: ColumnEditorContext<unknown, unknown>) => ReactNode): void;
13
+ /**
14
+ * Get the renderer registered for a column element.
15
+ * Falls back to field-based lookup if WeakMap lookup fails.
16
+ */
17
+ export declare function getColumnRenderer(element: HTMLElement): ((ctx: CellRenderContext<unknown, unknown>) => ReactNode) | undefined;
18
+ /**
19
+ * Get the editor registered for a column element.
20
+ * Falls back to field-based lookup if WeakMap lookup fails.
21
+ */
22
+ export declare function getColumnEditor(element: HTMLElement): ((ctx: ColumnEditorContext<unknown, unknown>) => ReactNode) | undefined;
23
+ /**
24
+ * Debug helper: Get list of registered fields.
25
+ * @internal
26
+ */
27
+ export declare function getRegisteredFields(): string[];
28
+ /**
29
+ * Framework adapter that enables React component integration
30
+ * with the grid's light DOM configuration API.
31
+ *
32
+ * ## Usage
33
+ *
34
+ * The adapter is automatically registered when using the DataGrid component.
35
+ * For advanced use cases, you can manually register:
36
+ *
37
+ * ```tsx
38
+ * import { GridElement } from '@toolbox-web/grid';
39
+ * import { ReactGridAdapter } from '@toolbox-web/grid-react';
40
+ *
41
+ * // One-time registration
42
+ * GridElement.registerAdapter(new ReactGridAdapter());
43
+ * ```
44
+ *
45
+ * ## Declarative usage with DataGrid:
46
+ *
47
+ * ```tsx
48
+ * <DataGrid rows={data} gridConfig={config}>
49
+ * <GridColumn field="status">
50
+ * {(ctx) => <StatusBadge value={ctx.value} />}
51
+ * </GridColumn>
52
+ * <GridColumn field="name" editor={(ctx) => (
53
+ * <NameEditor value={ctx.value} onCommit={ctx.commit} onCancel={ctx.cancel} />
54
+ * )} />
55
+ * </DataGrid>
56
+ * ```
57
+ */
58
+ export declare class ReactGridAdapter implements FrameworkAdapter {
59
+ private mountedViews;
60
+ /**
61
+ * Determines if this adapter can handle the given element.
62
+ * Checks if a renderer or editor is registered for this element.
63
+ */
64
+ canHandle(element: HTMLElement): boolean;
65
+ /**
66
+ * Creates a view renderer function that renders a React component
67
+ * and returns its container DOM element.
68
+ *
69
+ * Uses a cell cache to reuse React roots for performance - instead of
70
+ * creating new roots on each render, we reuse existing ones and just
71
+ * call root.render() with new data.
72
+ *
73
+ * Returns undefined if no renderer is registered for this element,
74
+ * allowing the grid to use its default rendering.
75
+ */
76
+ createRenderer<TRow = unknown, TValue = unknown>(element: HTMLElement): ColumnViewRenderer<TRow, TValue>;
77
+ /**
78
+ * Creates an editor spec that renders a React component
79
+ * with commit/cancel callbacks passed as props.
80
+ */
81
+ createEditor<TRow = unknown, TValue = unknown>(element: HTMLElement): ColumnEditorSpec<TRow, TValue>;
82
+ /**
83
+ * Creates a detail renderer function for MasterDetailPlugin.
84
+ * Renders React components for expandable detail rows.
85
+ */
86
+ createDetailRenderer<TRow = unknown>(gridElement: HTMLElement): ((row: TRow, rowIndex: number) => HTMLElement) | undefined;
87
+ /**
88
+ * Framework adapter hook called by MasterDetailPlugin during attach().
89
+ * Parses the <tbw-grid-detail> element and returns a React-based renderer.
90
+ */
91
+ parseDetailElement<TRow = unknown>(detailElement: Element): ((row: TRow, rowIndex: number) => HTMLElement | string) | undefined;
92
+ /**
93
+ * Creates a tool panel renderer from a light DOM element.
94
+ * Renders React components into tool panel containers.
95
+ */
96
+ createToolPanelRenderer(element: HTMLElement): ((container: HTMLElement) => void | (() => void)) | undefined;
97
+ /**
98
+ * Clean up all mounted React roots.
99
+ * Call this when the grid is unmounted.
100
+ */
101
+ destroy(): void;
102
+ /**
103
+ * Unmount a specific container (called when cell is recycled).
104
+ */
105
+ unmount(container: HTMLElement): void;
106
+ }
107
+ //# sourceMappingURL=react-grid-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react-grid-adapter.d.ts","sourceRoot":"","sources":["../../../../libs/grid-react/src/lib/react-grid-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAqBvC;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,WAAW,EACpB,QAAQ,EAAE,CAAC,GAAG,EAAE,iBAAiB,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,SAAS,GAChE,IAAI,CAaN;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,CAAC,GAAG,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,SAAS,GAChE,IAAI,CAYN;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,WAAW,GACnB,CAAC,CAAC,GAAG,EAAE,iBAAiB,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,SAAS,CAAC,GAAG,SAAS,CAYvE;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,WAAW,GACnB,CAAC,CAAC,GAAG,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,SAAS,CAAC,GAAG,SAAS,CAYzE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,EAAE,CAE9C;AAoBD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,gBAAiB,YAAW,gBAAgB;IACvD,OAAO,CAAC,YAAY,CAAqB;IAEzC;;;OAGG;IACH,SAAS,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO;IAoBxC;;;;;;;;;;OAUG;IACH,cAAc,CAAC,IAAI,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EAAE,OAAO,EAAE,WAAW,GAAG,kBAAkB,CAAC,IAAI,EAAE,MAAM,CAAC;IA2DxG;;;OAGG;IACH,YAAY,CAAC,IAAI,GAAG,OAAO,EAAE,MAAM,GAAG,OAAO,EAAE,OAAO,EAAE,WAAW,GAAG,gBAAgB,CAAC,IAAI,EAAE,MAAM,CAAC;IA0BpG;;;OAGG;IACH,oBAAoB,CAAC,IAAI,GAAG,OAAO,EACjC,WAAW,EAAE,WAAW,GACvB,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,KAAK,WAAW,CAAC,GAAG,SAAS;IAwB7D;;;OAGG;IACH,kBAAkB,CAAC,IAAI,GAAG,OAAO,EAC/B,aAAa,EAAE,OAAO,GACrB,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,KAAK,WAAW,GAAG,MAAM,CAAC,GAAG,SAAS;IAOtE;;;OAGG;IACH,uBAAuB,CAAC,OAAO,EAAE,WAAW,GAAG,CAAC,CAAC,SAAS,EAAE,WAAW,KAAK,IAAI,GAAG,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,SAAS;IAqC5G;;;OAGG;IACH,OAAO,IAAI,IAAI;IAWf;;OAEG;IACH,OAAO,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI;CAYtC"}
@@ -0,0 +1,73 @@
1
+ import { DataGridElement } from '../../../../dist/libs/grid/index.d.ts';
2
+ /**
3
+ * Type-safe grid event names and their payload types.
4
+ */
5
+ export type GridEventMap<TRow = unknown> = {
6
+ 'rows-change': CustomEvent<{
7
+ rows: TRow[];
8
+ }>;
9
+ 'cell-edit': CustomEvent<{
10
+ row: TRow;
11
+ field: string;
12
+ oldValue: unknown;
13
+ newValue: unknown;
14
+ }>;
15
+ 'row-click': CustomEvent<{
16
+ row: TRow;
17
+ rowIndex: number;
18
+ originalEvent: MouseEvent;
19
+ }>;
20
+ 'column-state-change': CustomEvent<{
21
+ columns: unknown[];
22
+ }>;
23
+ 'sort-change': CustomEvent<{
24
+ field: string;
25
+ direction: 'asc' | 'desc';
26
+ } | null>;
27
+ 'selection-change': CustomEvent<{
28
+ selectedRows: TRow[];
29
+ selectedIndices: number[];
30
+ }>;
31
+ 'filter-change': CustomEvent<{
32
+ filters: Record<string, unknown>;
33
+ }>;
34
+ 'group-toggle': CustomEvent<{
35
+ key: string;
36
+ expanded: boolean;
37
+ }>;
38
+ };
39
+ /**
40
+ * Hook for subscribing to grid events with automatic cleanup.
41
+ *
42
+ * ## Usage
43
+ *
44
+ * ```tsx
45
+ * import { DataGrid, useGridEvent, DataGridRef } from '@toolbox-web/grid-react';
46
+ * import { useRef } from 'react';
47
+ *
48
+ * function MyComponent() {
49
+ * const gridRef = useRef<DataGridRef>(null);
50
+ *
51
+ * // Subscribe to cell edit events
52
+ * useGridEvent(gridRef, 'cell-edit', (event) => {
53
+ * console.log('Cell edited:', event.detail);
54
+ * });
55
+ *
56
+ * // Subscribe to row clicks
57
+ * useGridEvent(gridRef, 'row-click', (event) => {
58
+ * console.log('Row clicked:', event.detail.row);
59
+ * });
60
+ *
61
+ * return <DataGrid ref={gridRef} rows={rows} />;
62
+ * }
63
+ * ```
64
+ *
65
+ * @param gridRef - Ref to the DataGrid component or element
66
+ * @param eventName - Name of the grid event to listen for
67
+ * @param handler - Event handler function
68
+ * @param deps - Optional dependency array (handler is stable if omitted)
69
+ */
70
+ export declare function useGridEvent<TRow = unknown, K extends keyof GridEventMap<TRow> = keyof GridEventMap<TRow>>(gridRef: React.RefObject<{
71
+ element?: DataGridElement | null;
72
+ } | DataGridElement | null>, eventName: K, handler: (event: GridEventMap<TRow>[K]) => void, deps?: unknown[]): void;
73
+ //# sourceMappingURL=use-grid-event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-grid-event.d.ts","sourceRoot":"","sources":["../../../../libs/grid-react/src/lib/use-grid-event.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGzD;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,IAAI,GAAG,OAAO,IAAI;IACzC,aAAa,EAAE,WAAW,CAAC;QAAE,IAAI,EAAE,IAAI,EAAE,CAAA;KAAE,CAAC,CAAC;IAC7C,WAAW,EAAE,WAAW,CAAC;QAAE,GAAG,EAAE,IAAI,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAC7F,WAAW,EAAE,WAAW,CAAC;QAAE,GAAG,EAAE,IAAI,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,UAAU,CAAA;KAAE,CAAC,CAAC;IACrF,qBAAqB,EAAE,WAAW,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,CAAA;KAAE,CAAC,CAAC;IAC3D,aAAa,EAAE,WAAW,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,KAAK,GAAG,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC,CAAC;IAChF,kBAAkB,EAAE,WAAW,CAAC;QAAE,YAAY,EAAE,IAAI,EAAE,CAAC;QAAC,eAAe,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;IACrF,eAAe,EAAE,WAAW,CAAC;QAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,CAAC,CAAC;IACnE,cAAc,EAAE,WAAW,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;CACjE,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAG,OAAO,EAAE,CAAC,SAAS,MAAM,YAAY,CAAC,IAAI,CAAC,GAAG,MAAM,YAAY,CAAC,IAAI,CAAC,EACxG,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC;IAAE,OAAO,CAAC,EAAE,eAAe,GAAG,IAAI,CAAA;CAAE,GAAG,eAAe,GAAG,IAAI,CAAC,EACvF,SAAS,EAAE,CAAC,EACZ,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,EAC/C,IAAI,GAAE,OAAO,EAAO,GACnB,IAAI,CA0BN"}
@@ -0,0 +1,63 @@
1
+ import { ColumnConfig, DataGridElement, GridConfig } from '../../../../dist/libs/grid/index.d.ts';
2
+ /**
3
+ * Extended interface for DataGridElement with all methods we need.
4
+ */
5
+ interface ExtendedGridElement extends DataGridElement {
6
+ toggleGroup?: (key: string) => Promise<void>;
7
+ }
8
+ /**
9
+ * Return type for useGrid hook.
10
+ */
11
+ export interface UseGridReturn<TRow = unknown> {
12
+ /** Ref to attach to the DataGrid component */
13
+ ref: React.RefObject<ExtendedGridElement | null>;
14
+ /** Whether the grid is ready */
15
+ isReady: boolean;
16
+ /** Current grid configuration */
17
+ config: GridConfig<TRow> | null;
18
+ /** Get the effective configuration */
19
+ getConfig: () => Promise<GridConfig<TRow> | null>;
20
+ /** Force a layout recalculation */
21
+ forceLayout: () => Promise<void>;
22
+ /** Toggle a group row */
23
+ toggleGroup: (key: string) => Promise<void>;
24
+ /** Register custom styles */
25
+ registerStyles: (id: string, css: string) => void;
26
+ /** Unregister custom styles */
27
+ unregisterStyles: (id: string) => void;
28
+ /** Get current visible columns */
29
+ getVisibleColumns: () => ColumnConfig<TRow>[];
30
+ }
31
+ /**
32
+ * Hook for programmatic access to a grid instance.
33
+ *
34
+ * ## Usage
35
+ *
36
+ * ```tsx
37
+ * import { DataGrid, useGrid } from '@toolbox-web/grid-react';
38
+ *
39
+ * function MyComponent() {
40
+ * const { ref, isReady, getConfig, forceLayout } = useGrid<Employee>();
41
+ *
42
+ * const handleResize = async () => {
43
+ * await forceLayout();
44
+ * };
45
+ *
46
+ * const handleExport = async () => {
47
+ * const config = await getConfig();
48
+ * console.log('Exporting with columns:', config?.columns);
49
+ * };
50
+ *
51
+ * return (
52
+ * <>
53
+ * <button onClick={handleResize}>Force Layout</button>
54
+ * <button onClick={handleExport} disabled={!isReady}>Export</button>
55
+ * <DataGrid ref={ref} rows={rows} />
56
+ * </>
57
+ * );
58
+ * }
59
+ * ```
60
+ */
61
+ export declare function useGrid<TRow = unknown>(): UseGridReturn<TRow>;
62
+ export {};
63
+ //# sourceMappingURL=use-grid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-grid.d.ts","sourceRoot":"","sources":["../../../../libs/grid-react/src/lib/use-grid.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGnF;;GAEG;AACH,UAAU,mBAAoB,SAAQ,eAAe;IACnD,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9C;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,IAAI,GAAG,OAAO;IAC3C,8CAA8C;IAC9C,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;IACjD,gCAAgC;IAChC,OAAO,EAAE,OAAO,CAAC;IACjB,iCAAiC;IACjC,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAChC,sCAAsC;IACtC,SAAS,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;IAClD,mCAAmC;IACnC,WAAW,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,yBAAyB;IACzB,WAAW,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,6BAA6B;IAC7B,cAAc,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAClD,+BAA+B;IAC/B,gBAAgB,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;IACvC,kCAAkC;IAClC,iBAAiB,EAAE,MAAM,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC;CAC/C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,OAAO,CAAC,IAAI,GAAG,OAAO,KAAK,aAAa,CAAC,IAAI,CAAC,CA6E7D"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@toolbox-web/grid-react",
3
+ "version": "0.0.2",
4
+ "description": "React adapter for @toolbox-web/grid data grid component",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ }
16
+ },
17
+ "keywords": [
18
+ "react",
19
+ "data-grid",
20
+ "web-component",
21
+ "toolbox-web"
22
+ ],
23
+ "license": "MIT",
24
+ "author": "Oystein Amundsen",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/OysteinAmundsen/toolbox.git",
28
+ "directory": "libs/grid-react"
29
+ },
30
+ "homepage": "https://github.com/OysteinAmundsen/toolbox/tree/main/libs/grid-react#readme",
31
+ "bugs": {
32
+ "url": "https://github.com/OysteinAmundsen/toolbox/issues"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "dependencies": {},
38
+ "devDependencies": {
39
+ "react": "^19.0.0",
40
+ "react-dom": "^19.0.0",
41
+ "@types/react": "^19.0.0",
42
+ "@types/react-dom": "^19.0.0",
43
+ "@toolbox-web/grid": ">=0.2.0"
44
+ },
45
+ "peerDependencies": {
46
+ "react": ">=18.0.0",
47
+ "react-dom": ">=18.0.0",
48
+ "@toolbox-web/grid": ">=0.2.0"
49
+ }
50
+ }