Skip to content

Commit 6508df3

Browse files
authored
Merge pull request #2165 from adumesny/master
more react demo example fixes
2 parents 168b944 + aa7355e commit 6508df3

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

demo/react-hooks-controlled-multiple.html

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ <h2>Controlled stack</h2>
2424

2525
<script type="text/babel">
2626
/***********************************************************************************************************/
27-
/********************************* NOT IDEAL - see comments below line 111) ********************************/
27+
/********************************* NOT IDEAL - see comments below line ~96) ********************************/
2828
/***********************************************************************************************************/
2929

3030
const { useState, useEffect, useLayoutEffect, createRef, useRef } = React
@@ -33,7 +33,7 @@ <h2>Controlled stack</h2>
3333
//
3434
// Controlled example
3535
//
36-
const ControlledStack = ({ items, addItem, removeItem, changeItems }) => {
36+
const ControlledStack = ({ items, addItem, changeItems }) => {
3737
const refs = useRef({})
3838
const gridRef = useRef()
3939
const gridContainerRef = useRef(null)
@@ -61,34 +61,28 @@ <h2>Controlled stack</h2>
6161
.on('added', (ev, gsItems) => {
6262
if (grid._ignoreCB) return;
6363
// remove the new element as React will re-create it again (dup) once we add to the list or we get 2 of them with same ids but different DOM el!
64-
// TODO: this is really not ideal - we shouldn't mix React templating with GS making it own edits as those get out of sync! see comment below @111.
64+
// TODO: this is really not ideal - we shouldn't mix React templating with GS making it own edits as those get out of sync! see comment below ~96.
6565
gsItems.forEach(n => {
6666
grid.removeWidget(n.el, true, false); // true=remove DOM, false=don't call use back!
6767
// can't pass n directly even though similar structs as it has n.el.gridstackNode which gives JSON error for circular write.
6868
addItem({id:n.id, x:n.x, y:n.y, w:n.w, h:n.h});
6969
});
7070
})
71-
.on('removed', (ev, gsItems) => {
72-
/* Looks like a bug in GridStack */
73-
const dirtyNodes = grid.engine.getDirtyNodes();
74-
if (dirtyNodes !== undefined && dirtyNodes.length !== 0) {
75-
const newItems = grid.save(false);
76-
changeItems(newItems);
77-
}
78-
79-
if (grid._ignoreCB) return;
80-
gsItems.forEach(n => removeItem(n.id));
81-
})
82-
.on('change', (ev, gsItems) => {
83-
const newItems = grid.save(false);
71+
.on('removed change', (ev, gsItems) => {
72+
// synch our version from GS....
73+
// Note: we could just update those few items passed but save() is fast and it's easier to just set an entire new list
74+
// and since we have the same ids, React will not re-create anything...
75+
const newItems = grid.save(false); // saveContent=false
8476
changeItems(newItems);
8577
})
78+
// addEvents(grid, i);
8679
} else {
8780
//
88-
// update existing grid layout, which is optimized to updates only diffs (will add new/delete items for examples)
81+
// update existing GS layout, which is optimized to updates only diffs and add new/delete items as well
8982
//
9083
const grid = gridRef.current;
9184
const layout = items.map((a) =>
85+
// use exiting nodes (which will skip diffs being the same) else new elements Widget but passing the React dom .el so we know what to makeWidget() on!
9286
refs.current[a.id].current.gridstackNode || {...a, el: refs.current[a.id].current}
9387
);
9488
grid._ignoreCB = true; // hack: ignore added/removed since we're the one doing the update
@@ -100,14 +94,15 @@ <h2>Controlled stack</h2>
10094

10195
return (
10296
// ********************
103-
// NOTE: constructing DOM grid items in template when gridstack is also allowed editing (dragging between grids, or adding/removing from say a toolbar)
97+
// NOTE: constructing DOM grid items in template when gridstack is also allowed creating (dragging between grids, or adding/removing from say a toolbar)
10498
// is NOT A GOOD IDEA as you end up fighting between gridstack users' edits and your template items structure which are not in sync.
10599
// At best, you end up re-creating widgets DOM (from React template) and all their content & state after a widget was inserted/re-parented by the user.
106100
// a MUCH better way is to let GS create React components using it's API/user interactions, with only initial load() of a stored layout.
107-
// see the upcoming Angular component wrapper that does that instead (lib author uses Angular). TBD creating React equivalent...
101+
// See the Angular component wrapper that does that: https://github.com/gridstack/gridstack.js/tree/master/demo/angular/src/app (lib author uses Angular)
102+
// ...TBD creating React equivalent...
108103
//
109-
// Also templating forces you to spell out the 12+ attributes GS supports (only x,w,w,h done below) instead of passing a option structure that supports everything
110-
// is not robust as things get added, and pollutes the DOM attr for default/missing entries, vs optimized code in GS.
104+
// Also templating forces you to spell out the 15+ GridStackWidget attributes (only x,y,w,h done below), instead of passing an option structure that
105+
// supports everything, is not robust as things get added and pollutes the DOM attr for default/missing entries, vs the optimized code in GS.
111106
// ********************
112107
<div style={{ width: '100%', marginRight: '10px' }}>
113108
<div className="grid-stack" ref={gridContainerRef}>
@@ -130,7 +125,7 @@ <h2>Controlled stack</h2>
130125

131126
const ControlledExample = () => {
132127
const [items1, setItems1] = useState([{ id: 'item-1-1', x: 0, y: 0, w: 2, h: 2 }, { id: 'item-1-2', x: 2, y: 0, w: 2, h: 2 }])
133-
const [items2, setItems2] = useState([{ id: 'item-2-1', x: 0, y: 0, w: 1, h: 1 }, { id: 'item-2-2', x: 0, y: 1, w: 1, h: 1 }, { id: 'item-2-3', x: 1, y: 0, w: 1, h: 1 }])
128+
const [items2, setItems2] = useState([{ id: 'item-2-1', x: 0, y: 0 }, { id: 'item-2-2', x: 0, y: 1 }, { id: 'item-2-3', x: 1, y: 0 }])
134129

135130
return (
136131
<div>
@@ -149,9 +144,6 @@ <h2>Controlled stack</h2>
149144
addItem={(item) => {
150145
setItems1(items => [...items, item])
151146
}}
152-
removeItem={(id) => {
153-
setItems1(items => items.filter(i => i.id !== id))
154-
}}
155147
changeItems={(items) => setItems1(items)}
156148
/>
157149
</div >
@@ -161,9 +153,6 @@ <h2>Controlled stack</h2>
161153
addItem={(item) => {
162154
setItems2(items => [...items, item])
163155
}}
164-
removeItem={(id) => {
165-
setItems2(items => items.filter(i => i.id !== id))
166-
}}
167156
changeItems={(items) => setItems2(items)}
168157
/>
169158
</div>

0 commit comments

Comments
 (0)