@@ -33,9 +33,10 @@ <h2>Controlled stack</h2>
3333 //
3434 // Controlled example
3535 //
36- const ControlledStack = ( { items, addItem, removeItem, id } ) => {
36+ const ControlledStack = ( { items, addItem, removeItem, changeItems } ) => {
3737 const refs = useRef ( { } )
3838 const gridRef = useRef ( )
39+ const gridContainerRef = useRef ( null )
3940 refs . current = { }
4041
4142 if ( Object . keys ( refs . current ) . length !== items . length ) {
@@ -49,13 +50,13 @@ <h2>Controlled stack</h2>
4950 // no need to init twice (would will return same grid) or register dup events
5051 const grid = gridRef . current = GridStack . init (
5152 {
52- float : true ,
53+ float : false ,
5354 acceptWidgets : true ,
5455 disableOneColumnMode : true , // side-by-side and fever columns to fit smaller screens
5556 column : 6 ,
5657 minRow : 1 ,
5758 } ,
58- `.controlled- ${ id } `
59+ gridContainerRef . current
5960 )
6061 . on ( 'added' , ( ev , gsItems ) => {
6162 if ( grid . _ignoreCB ) return ;
@@ -68,45 +69,35 @@ <h2>Controlled stack</h2>
6869 } ) ;
6970 } )
7071 . 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+
7179 if ( grid . _ignoreCB ) return ;
7280 gsItems . forEach ( n => removeItem ( n . id ) ) ;
7381 } )
74-
82+ . on ( 'change' , ( ev , gsItems ) => {
83+ const newItems = grid . save ( false ) ;
84+ changeItems ( newItems ) ;
85+ } )
7586 } else {
7687 //
7788 // update existing grid layout, which is optimized to updates only diffs (will add new/delete items for examples)
7889 //
7990 const grid = gridRef . current ;
80- const layout = [ ] ;
81- items . forEach ( ( a ) => layout . push (
91+ const layout = items . map ( ( a ) =>
8292 refs . current [ a . id ] . current . gridstackNode || { ...a , el : refs . current [ a . id ] . current }
83- ) ) ;
93+ ) ;
8494 grid . _ignoreCB = true ; // hack: ignore added/removed since we're the one doing the update
8595 grid . load ( layout ) ;
8696 delete grid . _ignoreCB ;
8797 }
8898
89- // NOTE: old code is incorrect as it re-does the GS binding, but dragged item is left behind so you get dup DOM elements with same ids
90- // grid.batchUpdate()
91- // items.forEach((a) => {
92- // // remove existing widgets
93- // if (refs.current[a.id] && refs.current[a.id].current) {
94- // grid.removeWidget(refs.current[a.id].current, false, false)
95- // }
96- // grid.makeWidget(refs.current[a.id].current)
97- // })
98- // grid.batchUpdate(false)
99-
10099 } , [ items ] )
101100
102- useEffect ( ( ) => {
103- return ( ) => {
104- // console.log('cleanup', id)
105- // gridRef.current.destroy(false, false)
106- // gridRef.current = null
107- }
108- } )
109-
110101 return (
111102 // ********************
112103 // NOTE: constructing DOM grid items in template when gridstack is also allowed editing (dragging between grids, or adding/removing from say a toolbar)
@@ -119,10 +110,10 @@ <h2>Controlled stack</h2>
119110 // is not robust as things get added, and pollutes the DOM attr for default/missing entries, vs optimized code in GS.
120111 // ********************
121112 < div style = { { width : '100%' , marginRight : '10px' } } >
122- < div className = { ` grid-stack controlled- ${ id } ` } >
113+ < div className = " grid-stack" ref = { gridContainerRef } >
123114 { items . map ( ( item , i ) => {
124115 return (
125- < div ref = { refs . current [ item . id ] } key = { ` ${ id } - ${ item . id } ` } className = { ' grid-stack-item' } gs-id = { item . id } gs-w = { item . w } gs-h = { item . h } gs-x = { item . x } gs-y = { item . y } >
116+ < div ref = { refs . current [ item . id ] } key = { item . id } className = " grid-stack-item" gs-id = { item . id } gs-w = { item . w } gs-h = { item . h } gs-x = { item . x } gs-y = { item . y } >
126117 < div className = "grid-stack-item-content" >
127118 < Item { ...item } />
128119 </ div >
@@ -142,32 +133,42 @@ <h2>Controlled stack</h2>
142133 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 } ] )
143134
144135 return (
145- < div style = { { display : 'flex' } } >
146- < div style = { { display : 'flex' , width : '50%' } } >
147- < ControlledStack
148- id = 'gs1'
149- items = { items1 }
150- addItem = { ( item ) => {
151- setItems1 ( items => [ ...items , item ] )
152- } }
153- removeItem = { ( id ) => {
154- setItems1 ( items => items . filter ( i => i . id !== id ) )
155- } }
156- />
157- </ div >
158- < div style = { { display : 'flex' , width : '50%' } } >
159- < ControlledStack
160- id = 'gs2'
161- items = { items2 }
162- addItem = { ( item ) => {
163- setItems2 ( items => [ ...items , item ] )
164- } }
165- removeItem = { ( id ) => {
166- setItems2 ( items => items . filter ( i => i . id !== id ) )
167- } }
168- />
136+ < div >
137+ < div style = { { display : 'flex' , gap : '16px' , marginBottom : '16px' } } >
138+ < div > </ div >
139+ </ div >
140+
141+ < div style = { { display : 'flex' , gap : '16px' , marginBottom : '16px' } } >
142+ < button onClick = { ( ) => setItems1 ( items => [ ...items , { id : `item-1-${ Date . now ( ) } ` , x : 2 , y : 0 , w : 2 , h : 2 } ] ) } > Add Item to 1 grid</ button >
143+ < button onClick = { ( ) => setItems2 ( items => [ ...items , { id : `item-2-${ Date . now ( ) } ` , x : 2 , y : 0 , w : 2 , h : 2 } ] ) } > Add Item to 2 grid</ button >
169144 </ div >
170- </ div >
145+ < div style = { { display : 'flex' } } >
146+ < div style = { { display : 'flex' , width : '50%' } } >
147+ < ControlledStack
148+ items = { items1 }
149+ addItem = { ( item ) => {
150+ setItems1 ( items => [ ...items , item ] )
151+ } }
152+ removeItem = { ( id ) => {
153+ setItems1 ( items => items . filter ( i => i . id !== id ) )
154+ } }
155+ changeItems = { ( items ) => setItems1 ( items ) }
156+ />
157+ </ div >
158+ < div style = { { display : 'flex' , width : '50%' } } >
159+ < ControlledStack
160+ items = { items2 }
161+ addItem = { ( item ) => {
162+ setItems2 ( items => [ ...items , item ] )
163+ } }
164+ removeItem = { ( id ) => {
165+ setItems2 ( items => items . filter ( i => i . id !== id ) )
166+ } }
167+ changeItems = { ( items ) => setItems2 ( items ) }
168+ />
169+ </ div >
170+ </ div >
171+ </ div >
171172 )
172173 }
173174
0 commit comments