forked from gridstack/gridstack.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-hooks.html
189 lines (166 loc) · 4.97 KB
/
react-hooks.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gridstack.js React integration example</title>
<link rel="stylesheet" href="demo.css" />
<script src="../dist/gridstack-h5.js"></script>
<!-- Scripts to use react inside html -->
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
</head>
<body>
<div>
<h1>Using GridStack.js with React hooks</h1>
<p>
As with any virtualDOM-based framework, you need to check if Reacthas rendered the DOM (or any updates to it)
<strong>before</strong> you initialize GridStack or call its methods. This example shows how to make rendered
components widgets:
</p>
<ol>
<li>Render items, each with a reference</li>
<li>Convert each rendered item to a widget using the reference and the <a
href="https://github.com/gridstack/gridstack.js/tree/master/doc#makewidgetel">
makeWidget</a> function</li>
</ol>
</div>
<div>
<h2>Controlled stack</h2>
<div id="controlled-stack"></div>
</div>
<div>
<h2>Uncontrolled stack</h2>
<div id="uncontrolled-stack"></div>
</div>
</body>
<script type="text/babel">
const { useState, useEffect, createRef, useRef } = React
const Item = ({ id }) => <div>I am item: {id}</div>
//
// Controlled example
//
const ControlledStack = ({ items, addItem }) => {
const refs = useRef({})
const gridRef = useRef()
if (Object.keys(refs.current).length !== items.length) {
items.forEach(({ id }) => {
refs.current[id] = refs.current[id] || createRef()
})
}
useEffect(() => {
gridRef.current =
gridRef.current ||
GridStack.init(
{
float: true,
},
'.controlled'
)
const grid = gridRef.current
grid.batchUpdate()
grid.removeAll(false)
items.forEach(({ id }) => grid.makeWidget(refs.current[id].current))
grid.commit()
}, [items])
return (
<div>
<button onClick={addItem}>Add new widget</button>
<div className={`grid-stack controlled`}>
{items.map((item, i) => {
return (
<div ref={refs.current[item.id]} key={item.id} className={'grid-stack-item'}>
<div className="grid-stack-item-content">
<Item {...item} />
</div>
</div>
)
})}
</div>
</div>
)
}
const ControlledExample = () => {
const [items, setItems] = useState([{ id: 'item-1' }, { id: 'item-2' }])
return (
<ControlledStack
items={items}
addItem={() => setItems([...items, { id: `item-${items.length + 1}` }])}
/>
)
}
//
// Uncontrolled example
//
const UncontrolledExample = () => {
const gridRef = useRef()
const [state, setState] = useState({
count: 0,
info: '',
items: [
{ x: 2, y: 1, h: 2 },
{ x: 2, y: 4, w: 3 },
{ x: 4, y: 2 },
{ x: 3, y: 1, h: 2 },
{ x: 0, y: 6, w: 2, h: 2 },
],
})
useEffect(() => {
gridRef.current =
gridRef.current ||
GridStack.init(
{
float: true,
cellHeight: '70px',
minRow: 1,
},
'.uncontrolled'
)
const grid = gridRef.current
grid.on('dragstop', (event, element) => {
const node = element.gridstackNode
setState(prevState => ({
...prevState,
info: `you just dragged node #${node.id} to ${node.x},${node.y} – good job!`,
}))
let timerId
window.clearTimeout(timerId)
timerId = window.setTimeout(() => {
setState(prevState => ({
...prevState,
info: '',
}))
}, 2000)
})
}, [])
return (
<div>
<button
onClick={() => {
const grid = gridRef.current
const node = state.items[state.count] || {
x: Math.round(12 * Math.random()),
y: Math.round(5 * Math.random()),
w: Math.round(1 + 3 * Math.random()),
h: Math.round(1 + 3 * Math.random()),
}
node.id = node.content = String(state.count)
setState(prevState => ({
...prevState,
count: prevState.count + 1,
}))
grid.addWidget(node)
}}
>
Add Widget
</button>
<div>{JSON.stringify(state)}</div>
<section class="grid-stack uncontrolled"></section>
</div>
)
}
ReactDOM.render(<ControlledExample />, document.getElementById('controlled-stack'))
ReactDOM.render(<UncontrolledExample />, document.getElementById('uncontrolled-stack'))
</script>
</html>