|
1 | | -# React + TypeScript + Vite |
| 1 | +# React GridStack Wrapper Demo |
2 | 2 |
|
3 | | -This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. |
| 3 | +A React wrapper component for GridStack that provides better TypeScript support and React integration experience. |
4 | 4 |
|
5 | | -Currently, two official plugins are available: |
| 5 | +## TODO |
6 | 6 |
|
7 | | -- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh |
8 | | -- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh |
| 7 | +- [x] Component mapping |
| 8 | +- [x] SubGrid support |
| 9 | +- [ ] Save and restore layout |
| 10 | +- [ ] Publish to npm |
9 | 11 |
|
10 | | -## Expanding the ESLint configuration |
| 12 | +## Basic Usage |
11 | 13 |
|
12 | | -If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: |
| 14 | +This is not an npm package, it's just a demo project. Please copy the relevant code to your project to use it. |
13 | 15 |
|
14 | | -- Configure the top-level `parserOptions` property like this: |
| 16 | +```tsx |
| 17 | +import { |
| 18 | + GridStackProvider, |
| 19 | + GridStackRender, |
| 20 | + GridStackRenderProvider, |
| 21 | +} from "path/to/lib"; |
| 22 | +import "gridstack/dist/gridstack.css"; |
| 23 | +import "gridstack/dist/gridstack-extra.css"; |
| 24 | +import "path/to/demo.css"; |
15 | 25 |
|
16 | | -```js |
17 | | -export default tseslint.config({ |
18 | | - languageOptions: { |
19 | | - // other options... |
20 | | - parserOptions: { |
21 | | - project: ['./tsconfig.node.json', './tsconfig.app.json'], |
22 | | - tsconfigRootDir: import.meta.dirname, |
| 26 | +function Text({ content }: { content: string }) { |
| 27 | + return <div>{content}</div>; |
| 28 | +} |
| 29 | + |
| 30 | +const COMPONENT_MAP = { |
| 31 | + Text, |
| 32 | + // ... other components |
| 33 | +}; |
| 34 | + |
| 35 | +// Grid options |
| 36 | +const gridOptions = { |
| 37 | + acceptWidgets: true, |
| 38 | + margin: 8, |
| 39 | + cellHeight: 50, |
| 40 | + children: [ |
| 41 | + { |
| 42 | + id: "item1", |
| 43 | + h: 2, |
| 44 | + w: 2, |
| 45 | + content: JSON.stringify({ |
| 46 | + name: "Text", |
| 47 | + props: { content: "Item 1" }, |
| 48 | + }), |
23 | 49 | }, |
24 | | - }, |
25 | | -}) |
| 50 | + // ... other grid items |
| 51 | + ], |
| 52 | +}; |
| 53 | + |
| 54 | +function App() { |
| 55 | + return ( |
| 56 | + <GridStackProvider initialOptions={gridOptions}> |
| 57 | + <!-- Maybe a toolbar here. Access to addWidget and addSubGrid by useGridStackContext() --> |
| 58 | + |
| 59 | + <!-- Grid Stack Root Element --> |
| 60 | + <GridStackRenderProvider> |
| 61 | + <!-- Grid Stack Default Render --> |
| 62 | + <GridStackRender componentMap={COMPONENT_MAP} /> |
| 63 | + </GridStackRenderProvider> |
| 64 | + |
| 65 | + <!-- Maybe other UI here --> |
| 66 | + </GridStackProvider> |
| 67 | + ); |
| 68 | +} |
26 | 69 | ``` |
27 | 70 |
|
28 | | -- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked` |
29 | | -- Optionally add `...tseslint.configs.stylisticTypeChecked` |
30 | | -- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config: |
31 | | - |
32 | | -```js |
33 | | -// eslint.config.js |
34 | | -import react from 'eslint-plugin-react' |
35 | | - |
36 | | -export default tseslint.config({ |
37 | | - // Set the react version |
38 | | - settings: { react: { version: '18.3' } }, |
39 | | - plugins: { |
40 | | - // Add the react plugin |
41 | | - react, |
42 | | - }, |
43 | | - rules: { |
44 | | - // other rules... |
45 | | - // Enable its recommended rules |
46 | | - ...react.configs.recommended.rules, |
47 | | - ...react.configs['jsx-runtime'].rules, |
48 | | - }, |
49 | | -}) |
| 71 | +## Advanced Features |
| 72 | + |
| 73 | +### Toolbar Operations |
| 74 | + |
| 75 | +Provide APIs to add new components and sub-grids: |
| 76 | + |
| 77 | +```tsx |
| 78 | +function Toolbar() { |
| 79 | + const { addWidget, addSubGrid } = useGridStackContext(); |
| 80 | + |
| 81 | + return ( |
| 82 | + <div> |
| 83 | + <button onClick={() => addWidget(/* ... */)}>Add Component</button> |
| 84 | + <button onClick={() => addSubGrid(/* ... */)}>Add SubGrid</button> |
| 85 | + </div> |
| 86 | + ); |
| 87 | +} |
50 | 88 | ``` |
| 89 | + |
| 90 | +### Layout Saving |
| 91 | + |
| 92 | +Get the current layout: |
| 93 | + |
| 94 | +```tsx |
| 95 | +const { saveOptions } = useGridStackContext(); |
| 96 | + |
| 97 | +const currentLayout = saveOptions(); |
| 98 | +``` |
| 99 | + |
| 100 | +## API Reference |
| 101 | + |
| 102 | +### GridStackProvider |
| 103 | + |
| 104 | +The main context provider, accepts the following properties: |
| 105 | + |
| 106 | +- `initialOptions`: Initial configuration options for GridStack |
| 107 | + |
| 108 | +### GridStackRender |
| 109 | + |
| 110 | +The core component for rendering the grid, accepts the following properties: |
| 111 | + |
| 112 | +- `componentMap`: A mapping from component names to actual React components |
| 113 | + |
| 114 | +### Hooks |
| 115 | + |
| 116 | +- `useGridStackContext()`: Access GridStack context and operations |
| 117 | + - `addWidget`: Add a new component |
| 118 | + - `addSubGrid`: Add a new sub-grid |
| 119 | + - `saveOptions`: Save current layout |
| 120 | + - `initialOptions`: Initial configuration options |
0 commit comments