1
1
// @flow
2
- import React from 'react' ;
2
+ import * as React from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import isEqual from 'lodash.isequal' ;
5
5
import classNames from 'classnames' ;
@@ -9,7 +9,7 @@ import GridItem from './GridItem';
9
9
const noop = function ( ) { } ;
10
10
11
11
// Types
12
- import type { ResizeEvent , DragEvent , Layout , LayoutItem } from './utils' ;
12
+ import type { EventCallback , GridResizeEvent , GridDragEvent , Layout , LayoutItem } from './utils' ;
13
13
type State = {
14
14
activeDrag : ?LayoutItem ,
15
15
layout : Layout ,
@@ -18,13 +18,42 @@ type State = {
18
18
oldLayout : ?Layout ,
19
19
oldResizeItem : ?LayoutItem
20
20
} ;
21
+
22
+ export type Props = {
23
+ className : string ,
24
+ style : Object ,
25
+ width : number ,
26
+ autoSize : boolean ,
27
+ cols : number ,
28
+ draggableCancel : string ,
29
+ draggableHandle : string ,
30
+ verticalCompact : boolean ,
31
+ layout : Layout ,
32
+ margin : [ number , number ] ,
33
+ containerPadding : [ number , number ] ,
34
+ rowHeight : number ,
35
+ maxRows : number ,
36
+ isDraggable : boolean ,
37
+ isResizable : boolean ,
38
+ useCSSTransforms : boolean ,
39
+
40
+ // Callbacks
41
+ onLayoutChange : ( Layout ) => void ,
42
+ onDrag : EventCallback ,
43
+ onDragStart : EventCallback ,
44
+ onDragStop : EventCallback ,
45
+ onResize : EventCallback ,
46
+ onResizeStart : EventCallback ,
47
+ onResizeStop : EventCallback ,
48
+ children : React . ChildrenArray < React . Element < any >> ,
49
+ } ;
21
50
// End Types
22
51
23
52
/**
24
53
* A reactive, fluid grid layout with draggable, resizable components.
25
54
*/
26
55
27
- export default class ReactGridLayout extends React . Component {
56
+ export default class ReactGridLayout extends React . Component < Props , State > {
28
57
// TODO publish internal ReactClass displayName transform
29
58
static displayName = "ReactGridLayout" ;
30
59
@@ -94,7 +123,7 @@ export default class ReactGridLayout extends React.Component {
94
123
// Callback so you can save the layout. Calls after each drag & resize stops.
95
124
onLayoutChange : PropTypes . func ,
96
125
97
- // Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e).
126
+ // Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e, ?node ).
98
127
// All callbacks below have the same signature. 'start' and 'stop' callbacks omit the 'placeholder'.
99
128
onDragStart : PropTypes . func ,
100
129
// Calls on each drag movement.
@@ -158,7 +187,7 @@ export default class ReactGridLayout extends React.Component {
158
187
oldResizeItem : null ,
159
188
} ;
160
189
161
- constructor ( props : $PropertyType < ReactGridLayout , 'props' > , context : any ) : void {
190
+ constructor ( props : Props , context : any ) : void {
162
191
super ( props , context ) ;
163
192
autoBindHandlers ( this , [ 'onDragStart' , 'onDrag' , 'onDragStop' , 'onResizeStart' , 'onResize' , 'onResizeStop' ] ) ;
164
193
}
@@ -170,7 +199,7 @@ export default class ReactGridLayout extends React.Component {
170
199
this . onLayoutMaybeChanged ( this . state . layout , this . props . layout ) ;
171
200
}
172
201
173
- componentWillReceiveProps ( nextProps : $PropertyType < ReactGridLayout , 'props' > ) {
202
+ componentWillReceiveProps ( nextProps : Props ) {
174
203
let newLayoutBase ;
175
204
// Allow parent to set layout directly.
176
205
if ( ! isEqual ( nextProps . layout , this . props . layout ) ) {
@@ -213,7 +242,7 @@ export default class ReactGridLayout extends React.Component {
213
242
* @param {Event } e The mousedown event
214
243
* @param {Element } node The current dragging DOM element
215
244
*/
216
- onDragStart ( i :string , x :number , y :number , { e, node} : DragEvent ) {
245
+ onDragStart ( i :string , x :number , y :number , { e, node} : GridDragEvent ) {
217
246
const { layout} = this . state ;
218
247
var l = getLayoutItem ( layout , i ) ;
219
248
if ( ! l ) return ;
@@ -231,7 +260,7 @@ export default class ReactGridLayout extends React.Component {
231
260
* @param {Event } e The mousedown event
232
261
* @param {Element } node The current dragging DOM element
233
262
*/
234
- onDrag ( i :string , x :number , y :number , { e, node} : DragEvent ) {
263
+ onDrag ( i :string , x :number , y :number , { e, node} : GridDragEvent ) {
235
264
const { oldDragItem} = this . state ;
236
265
let { layout} = this . state ;
237
266
var l = getLayoutItem ( layout , i ) ;
@@ -261,7 +290,7 @@ export default class ReactGridLayout extends React.Component {
261
290
* @param {Event } e The mousedown event
262
291
* @param {Element } node The current dragging DOM element
263
292
*/
264
- onDragStop ( i :string , x :number , y :number , { e, node} : DragEvent ) {
293
+ onDragStop ( i :string , x :number , y :number , { e, node} : GridDragEvent ) {
265
294
const { oldDragItem} = this . state ;
266
295
let { layout} = this . state ;
267
296
const l = getLayoutItem ( layout , i ) ;
@@ -292,7 +321,7 @@ export default class ReactGridLayout extends React.Component {
292
321
}
293
322
}
294
323
295
- onResizeStart ( i :string , w :number , h :number , { e, node} : ResizeEvent ) {
324
+ onResizeStart ( i :string , w :number , h :number , { e, node} : GridResizeEvent ) {
296
325
const { layout} = this . state ;
297
326
var l = getLayoutItem ( layout , i ) ;
298
327
if ( ! l ) return ;
@@ -305,7 +334,7 @@ export default class ReactGridLayout extends React.Component {
305
334
this . props . onResizeStart ( layout , l , l , null , e , node ) ;
306
335
}
307
336
308
- onResize ( i :string , w :number , h :number , { e, node} : ResizeEvent ) {
337
+ onResize ( i :string , w :number , h :number , { e, node} : GridResizeEvent ) {
309
338
const { layout, oldResizeItem} = this . state ;
310
339
var l = getLayoutItem ( layout , i ) ;
311
340
if ( ! l ) return ;
@@ -328,7 +357,7 @@ export default class ReactGridLayout extends React.Component {
328
357
} ) ;
329
358
}
330
359
331
- onResizeStop ( i :string , w :number , h :number , { e, node} : ResizeEvent ) {
360
+ onResizeStop ( i :string , w :number , h :number , { e, node} : GridResizeEvent ) {
332
361
const { layout, oldResizeItem} = this . state ;
333
362
var l = getLayoutItem ( layout , i ) ;
334
363
@@ -386,7 +415,7 @@ export default class ReactGridLayout extends React.Component {
386
415
*/
387
416
processGridItem ( child : React . Element < any > ) : ?React . Element < any > {
388
417
if ( ! child . key ) return ;
389
- const l = getLayoutItem ( this . state . layout , child . key ) ;
418
+ const l = getLayoutItem ( this . state . layout , String ( child . key ) ) ;
390
419
if ( ! l ) return null ;
391
420
const { width , cols , margin , containerPadding , rowHeight ,
392
421
maxRows , isDraggable , isResizable , useCSSTransforms ,
@@ -444,7 +473,10 @@ export default class ReactGridLayout extends React.Component {
444
473
445
474
return (
446
475
< div className = { classNames ( 'react-grid-layout' , className ) } style = { mergedStyle } >
447
- { React . Children . map ( this . props . children , ( child ) => this . processGridItem ( child ) ) }
476
+ {
477
+ // $FlowIgnore: Appears to think map calls back w/array
478
+ React . Children . map ( this . props . children , ( child ) => this . processGridItem ( child ) )
479
+ }
448
480
{ this . placeholder ( ) }
449
481
</ div >
450
482
) ;
0 commit comments