@open-grid/react 0.1.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/LICENSE +21 -0
- package/README.md +39 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +57 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Open Grid contributors
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @open-grid/react
|
|
2
|
+
|
|
3
|
+
React adapter for Open Grid.
|
|
4
|
+
|
|
5
|
+
Use this package when you want React bindings for the framework-agnostic core without the optional styled UI.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @open-grid/react react
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import { createColumnHelper, useGrid, useGridSelector } from "@open-grid/react";
|
|
17
|
+
|
|
18
|
+
const column = createColumnHelper<{ id: string; name: string }>();
|
|
19
|
+
|
|
20
|
+
function PeopleModel({ data }: { data: Array<{ id: string; name: string }> }) {
|
|
21
|
+
const grid = useGrid({
|
|
22
|
+
data,
|
|
23
|
+
columns: [column.accessor("name", { header: "Name" })],
|
|
24
|
+
getRowId: (row) => row.id,
|
|
25
|
+
});
|
|
26
|
+
const selectedRowCount = useGridSelector(
|
|
27
|
+
grid,
|
|
28
|
+
(currentGrid) => Object.keys(currentGrid.getState().rowSelection).length,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return <>{grid.getRowModel().rows.length} rows, {selectedRowCount} selected</>;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`useGridSelector` subscribes through the core selector contract and rerenders only
|
|
36
|
+
when the selected value changes. Pass a custom equality function as the third
|
|
37
|
+
argument when a selector returns a derived object.
|
|
38
|
+
|
|
39
|
+
Use [`@open-grid/react-ui`](https://www.npmjs.com/package/@open-grid/react-ui) when you want the maintained styled grid instead of a custom renderer.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GridOptions, Grid, GridSelector, GridSelectorSubscriptionOptions } from '@open-grid/core';
|
|
2
|
+
export { AccessorColumnOptions, AccessorFnColumnDef, AccessorKey, AccessorKeyColumnDef, AnyColumnDef, CellContext, CellCoordinate, CellEditEvent, CellEditEventParams, CellEditHistoryAction, CellEditHistoryState, CellEditPhase, CellFillOptions, CellInteractionEvent, CellInteractionEventParams, ClipboardCellContext, ClipboardCopyOptions, ClipboardPasteOptions, Column, ColumnDef, ColumnFilter, ColumnFiltersState, ColumnHelper, ColumnId, ColumnMovePosition, ColumnOrderState, ColumnPinningPosition, ColumnPinningState, ColumnResizeEvent, ColumnResizeEventParams, ColumnResizePhase, ColumnSizingState, ColumnVisibilityState, DisplayColumnDef, ExpandedState, FilterFn, FitColumnsToWidthOptions, Grid, GridCacheDiagnostics, GridCacheDiagnosticsEntry, GridCacheKey, GridOptions, GridSelector, GridSelectorListener, GridSelectorSubscriptionOptions, GridState, GroupColumnDef, GroupingState, Header, HeaderContext, HeaderGroup, PaginationState, Row, RowId, RowInteractionEvent, RowInteractionEventParams, RowModel, RowSelectionCleanupScope, RowSelectionState, SortFn, SortingRule, SortingState, Updater, createColumnHelper, createGrid, fitColumnsToWidth } from '@open-grid/core';
|
|
3
|
+
|
|
4
|
+
type UseGridOptions<TData> = GridOptions<TData>;
|
|
5
|
+
declare function useGrid<TData>(options: UseGridOptions<TData>): Grid<TData>;
|
|
6
|
+
declare function useGridSelector<TData, TSelected>(grid: Grid<TData>, selector: GridSelector<TData, TSelected>, equalityFn?: NonNullable<GridSelectorSubscriptionOptions<TSelected>["equalityFn"]>): TSelected;
|
|
7
|
+
|
|
8
|
+
export { type UseGridOptions, useGrid, useGridSelector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
createGrid
|
|
4
|
+
} from "@open-grid/core";
|
|
5
|
+
import { useCallback, useRef, useSyncExternalStore } from "react";
|
|
6
|
+
import { createColumnHelper, fitColumnsToWidth } from "@open-grid/core";
|
|
7
|
+
function useGrid(options) {
|
|
8
|
+
const gridRef = useRef(null);
|
|
9
|
+
if (!gridRef.current) {
|
|
10
|
+
gridRef.current = createGrid(options);
|
|
11
|
+
}
|
|
12
|
+
const grid = gridRef.current;
|
|
13
|
+
grid.setOptions(options, { notify: false });
|
|
14
|
+
useSyncExternalStore(
|
|
15
|
+
grid.subscribe,
|
|
16
|
+
() => grid.getState(),
|
|
17
|
+
() => grid.getState()
|
|
18
|
+
);
|
|
19
|
+
return grid;
|
|
20
|
+
}
|
|
21
|
+
function useGridSelector(grid, selector, equalityFn = Object.is) {
|
|
22
|
+
const selectorRef = useRef(selector);
|
|
23
|
+
const equalityRef = useRef(equalityFn);
|
|
24
|
+
const cacheRef = useRef(null);
|
|
25
|
+
selectorRef.current = selector;
|
|
26
|
+
equalityRef.current = equalityFn;
|
|
27
|
+
const selectedValue = selector(grid);
|
|
28
|
+
const cached = cacheRef.current;
|
|
29
|
+
if (!cached || cached.grid !== grid || !equalityFn(cached.value, selectedValue)) {
|
|
30
|
+
cacheRef.current = { grid, value: selectedValue };
|
|
31
|
+
}
|
|
32
|
+
const subscribe = useCallback(
|
|
33
|
+
(notify) => grid.subscribeSelector(
|
|
34
|
+
(currentGrid) => selectorRef.current(currentGrid),
|
|
35
|
+
(nextValue) => {
|
|
36
|
+
const currentCache = cacheRef.current;
|
|
37
|
+
if (!currentCache || currentCache.grid !== grid || !equalityRef.current(currentCache.value, nextValue)) {
|
|
38
|
+
cacheRef.current = { grid, value: nextValue };
|
|
39
|
+
notify();
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
equalityFn: (previous, next) => equalityRef.current(previous, next)
|
|
44
|
+
}
|
|
45
|
+
),
|
|
46
|
+
[grid]
|
|
47
|
+
);
|
|
48
|
+
const getSnapshot = useCallback(() => cacheRef.current?.value, [grid]);
|
|
49
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
createColumnHelper,
|
|
53
|
+
createGrid,
|
|
54
|
+
fitColumnsToWidth,
|
|
55
|
+
useGrid,
|
|
56
|
+
useGridSelector
|
|
57
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@open-grid/react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React adapter for Open Grid.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"data-grid",
|
|
7
|
+
"grid",
|
|
8
|
+
"table",
|
|
9
|
+
"react",
|
|
10
|
+
"adapter"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://goatshave.github.io/open-grid/packages#react",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Goatshave/open-grid/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsup src/index.ts --format esm --dts --clean --external react",
|
|
34
|
+
"check": "tsc --noEmit",
|
|
35
|
+
"test": "vitest run --passWithNoTests",
|
|
36
|
+
"lint": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@open-grid/core": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/react": "^18.3.12",
|
|
46
|
+
"react": "^18.3.1",
|
|
47
|
+
"tsup": "^8.3.5",
|
|
48
|
+
"typescript": "^5.7.2",
|
|
49
|
+
"vitest": "^2.1.8"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/Goatshave/open-grid.git",
|
|
54
|
+
"directory": "packages/react"
|
|
55
|
+
}
|
|
56
|
+
}
|