1
1
// @flow
2
2
import React from "react" ;
3
- import PropTypes from "prop-types" ;
3
+
4
4
import isEqual from "lodash.isequal" ;
5
5
import classNames from "classnames" ;
6
6
import {
@@ -12,22 +12,23 @@ import {
12
12
getLayoutItem ,
13
13
moveElement ,
14
14
synchronizeLayoutWithChildren ,
15
- validateLayout ,
16
15
getAllCollisions ,
17
- noop
16
+ compactType ,
17
+ noop ,
18
+ fastRGLPropsEqual
18
19
} from "./utils" ;
19
20
20
21
import { calcXY } from "./calculateUtils" ;
21
22
22
23
import GridItem from "./GridItem" ;
24
+ import ReactGridLayoutPropTypes from "./ReactGridLayoutPropTypes" ;
23
25
import type {
24
26
ChildrenArray as ReactChildrenArray ,
25
27
Element as ReactElement
26
28
} from "react" ;
27
29
28
30
// Types
29
31
import type {
30
- EventCallback ,
31
32
CompactType ,
32
33
GridResizeEvent ,
33
34
GridDragEvent ,
@@ -54,53 +55,9 @@ type State = {
54
55
propsLayout ?: Layout
55
56
} ;
56
57
57
- export type Props = {
58
- className : string ,
59
- style : Object ,
60
- width : number ,
61
- autoSize : boolean ,
62
- cols : number ,
63
- draggableCancel : string ,
64
- draggableHandle : string ,
65
- verticalCompact : boolean ,
66
- compactType : CompactType ,
67
- layout : Layout ,
68
- margin : [ number , number ] ,
69
- containerPadding : [ number , number ] | null ,
70
- rowHeight : number ,
71
- maxRows : number ,
72
- isDraggable : boolean ,
73
- isResizable : boolean ,
74
- isDroppable : boolean ,
75
- preventCollision : boolean ,
76
- useCSSTransforms : boolean ,
77
- transformScale : number ,
78
- droppingItem : $Shape < LayoutItem > ,
79
-
80
- // Callbacks
81
- onLayoutChange : Layout => void ,
82
- onDrag : EventCallback ,
83
- onDragStart : EventCallback ,
84
- onDragStop : EventCallback ,
85
- onResize : EventCallback ,
86
- onResizeStart : EventCallback ,
87
- onResizeStop : EventCallback ,
88
- onDrop : ( itemPosition : {
89
- x : number ,
90
- y : number ,
91
- w : number ,
92
- h : number ,
93
- e : Event
94
- } ) => void ,
95
- children : ReactChildrenArray < ReactElement < any >>
96
- } ;
97
- // End Types
98
-
99
- const compactType = ( props : Props ) : CompactType => {
100
- const { verticalCompact, compactType } = props || { } ;
58
+ import type { Props } from "./ReactGridLayoutPropTypes" ;
101
59
102
- return verticalCompact === false ? null : compactType ;
103
- } ;
60
+ // End Types
104
61
105
62
const layoutClassName = "react-grid-layout" ;
106
63
let isFirefox = false ;
@@ -119,135 +76,8 @@ export default class ReactGridLayout extends React.Component<Props, State> {
119
76
// TODO publish internal ReactClass displayName transform
120
77
static displayName = "ReactGridLayout" ;
121
78
122
- static propTypes = {
123
- //
124
- // Basic props
125
- //
126
- className : PropTypes . string ,
127
- style : PropTypes . object ,
128
-
129
- // This can be set explicitly. If it is not set, it will automatically
130
- // be set to the container width. Note that resizes will *not* cause this to adjust.
131
- // If you need that behavior, use WidthProvider.
132
- width : PropTypes . number ,
133
-
134
- // If true, the container height swells and contracts to fit contents
135
- autoSize : PropTypes . bool ,
136
- // # of cols.
137
- cols : PropTypes . number ,
138
-
139
- // A selector that will not be draggable.
140
- draggableCancel : PropTypes . string ,
141
- // A selector for the draggable handler
142
- draggableHandle : PropTypes . string ,
143
-
144
- // Deprecated
145
- verticalCompact : function ( props : Props ) {
146
- if (
147
- props . verticalCompact === false &&
148
- process . env . NODE_ENV !== "production"
149
- ) {
150
- console . warn (
151
- // eslint-disable-line no-console
152
- "`verticalCompact` on <ReactGridLayout> is deprecated and will be removed soon. " +
153
- 'Use `compactType`: "horizontal" | "vertical" | null.'
154
- ) ;
155
- }
156
- } ,
157
- // Choose vertical or hotizontal compaction
158
- compactType : PropTypes . oneOf ( [ "vertical" , "horizontal" ] ) ,
159
-
160
- // layout is an array of object with the format:
161
- // {x: Number, y: Number, w: Number, h: Number, i: String}
162
- layout : function ( props : Props ) {
163
- var layout = props . layout ;
164
- // I hope you're setting the data-grid property on the grid items
165
- if ( layout === undefined ) return ;
166
- validateLayout ( layout , "layout" ) ;
167
- } ,
168
-
169
- //
170
- // Grid Dimensions
171
- //
172
-
173
- // Margin between items [x, y] in px
174
- margin : PropTypes . arrayOf ( PropTypes . number ) ,
175
- // Padding inside the container [x, y] in px
176
- containerPadding : PropTypes . arrayOf ( PropTypes . number ) ,
177
- // Rows have a static height, but you can change this based on breakpoints if you like
178
- rowHeight : PropTypes . number ,
179
- // Default Infinity, but you can specify a max here if you like.
180
- // Note that this isn't fully fleshed out and won't error if you specify a layout that
181
- // extends beyond the row capacity. It will, however, not allow users to drag/resize
182
- // an item past the barrier. They can push items beyond the barrier, though.
183
- // Intentionally not documented for this reason.
184
- maxRows : PropTypes . number ,
185
-
186
- //
187
- // Flags
188
- //
189
- isDraggable : PropTypes . bool ,
190
- isResizable : PropTypes . bool ,
191
- // If true, grid items won't change position when being dragged over.
192
- preventCollision : PropTypes . bool ,
193
- // Use CSS transforms instead of top/left
194
- useCSSTransforms : PropTypes . bool ,
195
- // parent layout transform scale
196
- transformScale : PropTypes . number ,
197
- // If true, an external element can trigger onDrop callback with a specific grid position as a parameter
198
- isDroppable : PropTypes . bool ,
199
-
200
- //
201
- // Callbacks
202
- //
203
-
204
- // Callback so you can save the layout. Calls after each drag & resize stops.
205
- onLayoutChange : PropTypes . func ,
206
-
207
- // Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e, ?node).
208
- // All callbacks below have the same signature. 'start' and 'stop' callbacks omit the 'placeholder'.
209
- onDragStart : PropTypes . func ,
210
- // Calls on each drag movement.
211
- onDrag : PropTypes . func ,
212
- // Calls when drag is complete.
213
- onDragStop : PropTypes . func ,
214
- //Calls when resize starts.
215
- onResizeStart : PropTypes . func ,
216
- // Calls when resize movement happens.
217
- onResize : PropTypes . func ,
218
- // Calls when resize is complete.
219
- onResizeStop : PropTypes . func ,
220
- // Calls when some element is dropped.
221
- onDrop : PropTypes . func ,
222
-
223
- //
224
- // Other validations
225
- //
226
-
227
- droppingItem : PropTypes . shape ( {
228
- i : PropTypes . string . isRequired ,
229
- w : PropTypes . number . isRequired ,
230
- h : PropTypes . number . isRequired
231
- } ) ,
232
-
233
- // Children must not have duplicate keys.
234
- children : function ( props : Props , propName : string ) {
235
- var children = props [ propName ] ;
236
-
237
- // Check children keys for duplicates. Throw if found.
238
- var keys = { } ;
239
- React . Children . forEach ( children , function ( child ) {
240
- if ( keys [ child . key ] ) {
241
- throw new Error (
242
- 'Duplicate child key "' +
243
- child . key +
244
- '" found! This will cause problems in ReactGridLayout.'
245
- ) ;
246
- }
247
- keys [ child . key ] = true ;
248
- } ) ;
249
- }
250
- } ;
79
+ // Refactored to another module to make way for preval
80
+ static propTypes = ReactGridLayoutPropTypes ;
251
81
252
82
static defaultProps = {
253
83
autoSize : true ,
@@ -365,6 +195,13 @@ export default class ReactGridLayout extends React.Component<Props, State> {
365
195
return null ;
366
196
}
367
197
198
+ shouldComponentUpdate ( nextProps : Props , nextState : State ) {
199
+ return (
200
+ ! fastRGLPropsEqual ( this . props , nextProps , isEqual ) ||
201
+ ! isEqual ( this . state . activeDrag , nextState . activeDrag )
202
+ ) ;
203
+ }
204
+
368
205
componentDidUpdate ( prevProps : Props , prevState : State ) {
369
206
if ( ! this . state . activeDrag ) {
370
207
const newLayout = this . state . layout ;
0 commit comments