Skip to content

Commit 5066145

Browse files
committed
Add the GridStackContainer and update docs
1 parent 0525d4c commit 5066145

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

react/README.md

+43-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Open in [CodeSandbox](https://codesandbox.io/p/sandbox/github/gridstack/gridstac
1414
- [x] Custom handle
1515
- [x] Drag between two grid stacks
1616

17-
Welcome to give any suggestions and ideas, you can submit an issue or contact me by email. :)
18-
1917
## Usage
2018

2119
This is not an npm package, it's just a demo project. Please copy the `src/lib` code to your project to use it.
@@ -24,6 +22,37 @@ This is not an npm package, it's just a demo project. Please copy the `src/lib`
2422

2523
Render item with widget id selector.
2624

25+
Code here: [src/examples/000-simple/index.tsx](src/examples/000-simple/index.tsx)
26+
27+
```tsx
28+
function Simple() {
29+
const [uncontrolledInitialOptions] = useState<GridStackOptions>(() => ({
30+
...defaultGridOptions,
31+
children: [
32+
{ id: "item1", h: 2, w: 2, x: 0, y: 0 },
33+
{ id: "item2", h: 2, w: 2, x: 2, y: 0 },
34+
],
35+
}));
36+
37+
return (
38+
<GridStackContainer
39+
initialOptions={uncontrolledInitialOptions}
40+
renderRawContent
41+
>
42+
<GridStackItem id="item1">
43+
<div style={{ color: "yellow" }}>hello</div>
44+
</GridStackItem>
45+
46+
<GridStackItem id="item2">
47+
<div style={{ color: "blue" }}>grid</div>
48+
</GridStackItem>
49+
</GridStackContainer>
50+
);
51+
}
52+
```
53+
54+
Or split the grid stack container to provide grid stack context and render component for access to grid stack context.
55+
2756
Code here: [src/examples/001-simple/index.tsx](src/examples/001-simple/index.tsx)
2857

2958
```tsx
@@ -38,6 +67,7 @@ function Simple() {
3867

3968
return (
4069
<GridStackProvider initialOptions={uncontrolledInitialOptions}>
70+
{/* Custom toolbar component. Access to grid stack context by useGridStackContext hook. */}
4171
<Toolbar />
4272

4373
<GridStackRender renderRawContent>
@@ -210,6 +240,17 @@ function CustomHandle() {
210240

211241
### Components
212242

243+
#### GridStackContainer
244+
245+
Top-level component that provides GridStack context and GridStack root container. Equivalent to `GridStackProvider` and `GridStackRender` combined.
246+
247+
```typescript
248+
type GridStackContainerProps = {
249+
initialOptions: GridStackOptions; // GridStack initialization options
250+
children: React.ReactNode;
251+
};
252+
```
253+
213254
#### GridStackProvider
214255

215256
Top-level component that provides GridStack context.

react/src/App.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "./demo.css";
2+
import { Simple0 } from "./examples/000-simple";
23
import { Simple } from "./examples/001-simple";
34
import { Nested } from "./examples/002-nested";
45
import { CustomHandle } from "./examples/003-custom-handle";
@@ -7,8 +8,11 @@ import { Advanced } from "./examples/009-advanced";
78
export default function App() {
89
return (
910
<div>
10-
<h2 id="simple">Simple</h2>
11+
<h2 id="simple0">Simple</h2>
1112
<p>Render content by GridStackItem with id selector.</p>
13+
<Simple0 />
14+
<h2 id="simple">Simple With Toolbar</h2>
15+
<p>With toolbar</p>
1216
<Simple />
1317
<h2 id="nested">Nested</h2>
1418
<p>Only use gridstack.js native subGridOpts.</p>
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { GridStackOptions } from "gridstack";
2+
import { useState } from "react";
3+
import { defaultGridOptions } from "../../default-grid-options";
4+
import { GridStackItem } from "../../lib";
5+
import { GridStackContainer } from "../../lib/grid-stack-container";
6+
7+
export function Simple0() {
8+
const [uncontrolledInitialOptions] = useState<GridStackOptions>(() => ({
9+
...defaultGridOptions,
10+
children: [
11+
{ id: "item1", h: 2, w: 2, x: 0, y: 0 },
12+
{ id: "item2", h: 2, w: 2, x: 2, y: 0 },
13+
],
14+
}));
15+
16+
return (
17+
<GridStackContainer
18+
initialOptions={uncontrolledInitialOptions}
19+
renderRawContent
20+
>
21+
<GridStackItem id="item1">
22+
<div style={{ color: "yellow" }}>hello</div>
23+
</GridStackItem>
24+
25+
<GridStackItem id="item2">
26+
<div style={{ color: "blue" }}>grid</div>
27+
</GridStackItem>
28+
</GridStackContainer>
29+
);
30+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { GridStackOptions } from "gridstack";
2+
import { PropsWithChildren } from "react";
3+
import { GridStackProvider } from "./grid-stack-provider";
4+
import { GridStackRender } from "./grid-stack-render";
5+
6+
export type GridStackContainerProps = PropsWithChildren<{
7+
initialOptions: GridStackOptions;
8+
renderRawContent?: boolean;
9+
}>;
10+
11+
export function GridStackContainer({
12+
children,
13+
initialOptions,
14+
renderRawContent,
15+
}: GridStackContainerProps) {
16+
return (
17+
<GridStackProvider initialOptions={initialOptions}>
18+
<GridStackRender renderRawContent={renderRawContent}>
19+
{children}
20+
</GridStackRender>
21+
</GridStackProvider>
22+
);
23+
}

react/src/lib/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export type { GridStackContainerProps } from "./grid-stack-container";
2+
export { GridStackContainer } from "./grid-stack-container";
3+
14
export type { GridStackContextType } from "./grid-stack-context";
25
export { GridStackContext, useGridStackContext } from "./grid-stack-context";
36

0 commit comments

Comments
 (0)