forked from react-grid-layout/react-grid-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactGridLayout.jsx
855 lines (761 loc) · 22.1 KB
/
ReactGridLayout.jsx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
// @flow
import * as React from "react";
import { deepEqual } from "fast-equals";
import clsx from "clsx";
import {
bottom,
childrenEqual,
cloneLayoutItem,
compact,
compactType,
fastRGLPropsEqual,
getAllCollisions,
getLayoutItem,
moveElement,
noop,
synchronizeLayoutWithChildren,
withLayoutItem
} from "./utils";
import { calcXY } from "./calculateUtils";
import GridItem from "./GridItem";
import ReactGridLayoutPropTypes from "./ReactGridLayoutPropTypes";
import type {
ChildrenArray as ReactChildrenArray,
Element as ReactElement
} from "react";
// Types
import type {
CompactType,
GridResizeEvent,
GridDragEvent,
DragOverEvent,
Layout,
DroppingPosition,
LayoutItem
} from "./utils";
import type { PositionParams } from "./calculateUtils";
type State = {
activeDrag: ?LayoutItem,
layout: Layout,
mounted: boolean,
oldDragItem: ?LayoutItem,
oldLayout: ?Layout,
oldResizeItem: ?LayoutItem,
resizing: boolean,
droppingDOMNode: ?ReactElement<any>,
droppingPosition?: DroppingPosition,
// Mirrored props
children: ReactChildrenArray<ReactElement<any>>,
compactType?: CompactType,
propsLayout?: Layout
};
import type { Props, DefaultProps } from "./ReactGridLayoutPropTypes";
// End Types
const layoutClassName = "react-grid-layout";
let isFirefox = false;
// Try...catch will protect from navigator not existing (e.g. node) or a bad implementation of navigator
try {
isFirefox = /firefox/i.test(navigator.userAgent);
} catch (e) {
/* Ignore */
}
/**
* A reactive, fluid grid layout with draggable, resizable components.
*/
export default class ReactGridLayout extends React.Component<Props, State> {
// TODO publish internal ReactClass displayName transform
static displayName: ?string = "ReactGridLayout";
// Refactored to another module to make way for preval
static propTypes = ReactGridLayoutPropTypes;
static defaultProps: DefaultProps = {
autoSize: true,
cols: 12,
className: "",
style: {},
draggableHandle: "",
draggableCancel: "",
containerPadding: null,
rowHeight: 150,
maxRows: Infinity, // infinite vertical growth
layout: [],
margin: [10, 10],
isBounded: false,
isDraggable: true,
isResizable: true,
allowOverlap: false,
isDroppable: false,
useCSSTransforms: true,
transformScale: 1,
verticalCompact: true,
compactType: "vertical",
preventCollision: false,
droppingItem: {
i: "__dropping-elem__",
h: 1,
w: 1
},
resizeHandles: ["se"],
onLayoutChange: noop,
onDragStart: noop,
onDrag: noop,
onDragStop: noop,
onResizeStart: noop,
onResize: noop,
onResizeStop: noop,
onDrop: noop,
onDropDragOver: noop
};
state: State = {
activeDrag: null,
layout: synchronizeLayoutWithChildren(
this.props.layout,
this.props.children,
this.props.cols,
// Legacy support for verticalCompact: false
compactType(this.props),
this.props.allowOverlap
),
mounted: false,
oldDragItem: null,
oldLayout: null,
oldResizeItem: null,
resizing: false,
droppingDOMNode: null,
children: []
};
dragEnterCounter: number = 0;
componentDidMount() {
this.setState({ mounted: true });
// Possibly call back with layout on mount. This should be done after correcting the layout width
// to ensure we don't rerender with the wrong width.
this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
}
static getDerivedStateFromProps(
nextProps: Props,
prevState: State
): $Shape<State> | null {
let newLayoutBase;
if (prevState.activeDrag) {
return null;
}
// Legacy support for compactType
// Allow parent to set layout directly.
if (
!deepEqual(nextProps.layout, prevState.propsLayout) ||
nextProps.compactType !== prevState.compactType
) {
newLayoutBase = nextProps.layout;
} else if (!childrenEqual(nextProps.children, prevState.children)) {
// If children change, also regenerate the layout. Use our state
// as the base in case because it may be more up to date than
// what is in props.
newLayoutBase = prevState.layout;
}
// We need to regenerate the layout.
if (newLayoutBase) {
const newLayout = synchronizeLayoutWithChildren(
newLayoutBase,
nextProps.children,
nextProps.cols,
compactType(nextProps),
nextProps.allowOverlap
);
return {
layout: newLayout,
// We need to save these props to state for using
// getDerivedStateFromProps instead of componentDidMount (in which we would get extra rerender)
compactType: nextProps.compactType,
children: nextProps.children,
propsLayout: nextProps.layout
};
}
return null;
}
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
return (
// NOTE: this is almost always unequal. Therefore the only way to get better performance
// from SCU is if the user intentionally memoizes children. If they do, and they can
// handle changes properly, performance will increase.
this.props.children !== nextProps.children ||
!fastRGLPropsEqual(this.props, nextProps, deepEqual) ||
this.state.activeDrag !== nextState.activeDrag ||
this.state.mounted !== nextState.mounted ||
this.state.droppingPosition !== nextState.droppingPosition
);
}
componentDidUpdate(prevProps: Props, prevState: State) {
if (!this.state.activeDrag) {
const newLayout = this.state.layout;
const oldLayout = prevState.layout;
this.onLayoutMaybeChanged(newLayout, oldLayout);
}
}
/**
* Calculates a pixel value for the container.
* @return {String} Container height in pixels.
*/
containerHeight(): ?string {
if (!this.props.autoSize) return;
const nbRow = bottom(this.state.layout);
const containerPaddingY = this.props.containerPadding
? this.props.containerPadding[1]
: this.props.margin[1];
return (
nbRow * this.props.rowHeight +
(nbRow - 1) * this.props.margin[1] +
containerPaddingY * 2 +
"px"
);
}
/**
* When dragging starts
* @param {String} i Id of the child
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
onDragStart: (i: string, x: number, y: number, GridDragEvent) => void = (
i: string,
x: number,
y: number,
{ e, node }: GridDragEvent
) => {
const { layout } = this.state;
const l = getLayoutItem(layout, i);
if (!l) return;
// Create placeholder (display only)
const placeholder = {
w: l.w,
h: l.h,
x: l.x,
y: l.y,
placeholder: true,
i: i
};
this.setState({
oldDragItem: cloneLayoutItem(l),
oldLayout: layout,
activeDrag: placeholder
});
return this.props.onDragStart(layout, l, l, null, e, node);
};
/**
* Each drag movement create a new dragelement and move the element to the dragged location
* @param {String} i Id of the child
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
onDrag: (i: string, x: number, y: number, GridDragEvent) => void = (
i,
x,
y,
{ e, node }
) => {
const { oldDragItem } = this.state;
let { layout } = this.state;
const { cols, allowOverlap, preventCollision } = this.props;
const l = getLayoutItem(layout, i);
if (!l) return;
// Create placeholder (display only)
const placeholder = {
w: l.w,
h: l.h,
x: l.x,
y: l.y,
placeholder: true,
i: i
};
// Move the element to the dragged location.
const isUserAction = true;
layout = moveElement(
layout,
l,
x,
y,
isUserAction,
preventCollision,
compactType(this.props),
cols,
allowOverlap
);
this.props.onDrag(layout, oldDragItem, l, placeholder, e, node);
this.setState({
layout: allowOverlap
? layout
: compact(layout, compactType(this.props), cols),
activeDrag: placeholder
});
};
/**
* When dragging stops, figure out which position the element is closest to and update its x and y.
* @param {String} i Index of the child.
* @param {Number} x X position of the move
* @param {Number} y Y position of the move
* @param {Event} e The mousedown event
* @param {Element} node The current dragging DOM element
*/
onDragStop: (i: string, x: number, y: number, GridDragEvent) => void = (
i,
x,
y,
{ e, node }
) => {
if (!this.state.activeDrag) return;
const { oldDragItem } = this.state;
let { layout } = this.state;
const { cols, preventCollision, allowOverlap } = this.props;
const l = getLayoutItem(layout, i);
if (!l) return;
// Move the element here
const isUserAction = true;
layout = moveElement(
layout,
l,
x,
y,
isUserAction,
preventCollision,
compactType(this.props),
cols,
allowOverlap
);
// Set state
const newLayout = allowOverlap
? layout
: compact(layout, compactType(this.props), cols);
this.props.onDragStop(newLayout, oldDragItem, l, null, e, node);
const { oldLayout } = this.state;
this.setState({
activeDrag: null,
layout: newLayout,
oldDragItem: null,
oldLayout: null
});
this.onLayoutMaybeChanged(newLayout, oldLayout);
};
onLayoutMaybeChanged(newLayout: Layout, oldLayout: ?Layout) {
if (!oldLayout) oldLayout = this.state.layout;
if (!deepEqual(oldLayout, newLayout)) {
this.props.onLayoutChange(newLayout);
}
}
onResizeStart: (i: string, w: number, h: number, GridResizeEvent) => void = (
i,
w,
h,
{ e, node }
) => {
const { layout } = this.state;
const l = getLayoutItem(layout, i);
if (!l) return;
this.setState({
oldResizeItem: cloneLayoutItem(l),
oldLayout: this.state.layout,
resizing: true
});
this.props.onResizeStart(layout, l, l, null, e, node);
};
onResize: (i: string, w: number, h: number, GridResizeEvent) => void = (
i,
w,
h,
{ e, node, size, handle }
) => {
const { oldResizeItem } = this.state;
const { layout } = this.state;
const { cols, preventCollision, allowOverlap } = this.props;
let shouldMoveItem = false;
let finalLayout;
let x;
let y;
const [newLayout, l] = withLayoutItem(layout, i, l => {
let hasCollisions;
x = l.x;
y = l.y;
if (["sw", "w", "nw", "n", "ne"].indexOf(handle) !== -1) {
if (["sw", "nw", "w"].indexOf(handle) !== -1) {
x = l.x + (l.w - w);
w = l.x !== x && x < 0 ? l.w : w;
x = x < 0 ? 0 : x;
}
if (["ne", "n", "nw"].indexOf(handle) !== -1) {
y = l.y + (l.h - h);
h = l.y !== y && y < 0 ? l.h : h;
y = y < 0 ? 0 : y;
}
shouldMoveItem = true;
}
// Something like quad tree should be used
// to find collisions faster
if (preventCollision && !allowOverlap) {
const collisions = getAllCollisions(layout, {
...l,
w,
h,
x,
y
}).filter(layoutItem => layoutItem.i !== l.i);
hasCollisions = collisions.length > 0;
// If we're colliding, we need adjust the placeholder.
if (hasCollisions) {
// Reset layoutItem dimensions if there were collisions
y = l.y;
h = l.h;
x = l.x;
w = l.w;
shouldMoveItem = false;
}
}
l.w = w;
l.h = h;
return l;
});
// Shouldn't ever happen, but typechecking makes it necessary
if (!l) return;
finalLayout = newLayout;
if (shouldMoveItem) {
// Move the element to the new position.
const isUserAction = true;
finalLayout = moveElement(
newLayout,
l,
x,
y,
isUserAction,
this.props.preventCollision,
compactType(this.props),
cols,
allowOverlap
);
}
// Create placeholder element (display only)
const placeholder = {
w: l.w,
h: l.h,
x: l.x,
y: l.y,
static: true,
i: i
};
this.props.onResize(finalLayout, oldResizeItem, l, placeholder, e, node);
// Re-compact the newLayout and set the drag placeholder.
this.setState({
layout: allowOverlap
? finalLayout
: compact(finalLayout, compactType(this.props), cols),
activeDrag: placeholder
});
};
onResizeStop: (i: string, w: number, h: number, GridResizeEvent) => void = (
i,
w,
h,
{ e, node }
) => {
const { layout, oldResizeItem } = this.state;
const { cols, allowOverlap } = this.props;
const l = getLayoutItem(layout, i);
// Set state
const newLayout = allowOverlap
? layout
: compact(layout, compactType(this.props), cols);
this.props.onResizeStop(newLayout, oldResizeItem, l, null, e, node);
const { oldLayout } = this.state;
this.setState({
activeDrag: null,
layout: newLayout,
oldResizeItem: null,
oldLayout: null,
resizing: false
});
this.onLayoutMaybeChanged(newLayout, oldLayout);
};
/**
* Create a placeholder object.
* @return {Element} Placeholder div.
*/
placeholder(): ?ReactElement<any> {
const { activeDrag } = this.state;
if (!activeDrag) return null;
const {
width,
cols,
margin,
containerPadding,
rowHeight,
maxRows,
useCSSTransforms,
transformScale
} = this.props;
// {...this.state.activeDrag} is pretty slow, actually
return (
<GridItem
w={activeDrag.w}
h={activeDrag.h}
x={activeDrag.x}
y={activeDrag.y}
i={activeDrag.i}
className={`react-grid-placeholder ${
this.state.resizing ? "placeholder-resizing" : ""
}`}
containerWidth={width}
cols={cols}
margin={margin}
containerPadding={containerPadding || margin}
maxRows={maxRows}
rowHeight={rowHeight}
isDraggable={false}
isResizable={false}
isBounded={false}
useCSSTransforms={useCSSTransforms}
transformScale={transformScale}
>
<div />
</GridItem>
);
}
/**
* Given a grid item, set its style attributes & surround in a <Draggable>.
* @param {Element} child React element.
* @return {Element} Element wrapped in draggable and properly placed.
*/
processGridItem(
child: ReactElement<any>,
isDroppingItem?: boolean
): ?ReactElement<any> {
if (!child || !child.key) return;
const l = getLayoutItem(this.state.layout, String(child.key));
if (!l) return null;
const {
width,
cols,
margin,
containerPadding,
rowHeight,
maxRows,
isDraggable,
isResizable,
isBounded,
useCSSTransforms,
transformScale,
draggableCancel,
draggableHandle,
resizeHandles,
resizeHandle
} = this.props;
const { mounted, droppingPosition } = this.state;
// Determine user manipulations possible.
// If an item is static, it can't be manipulated by default.
// Any properties defined directly on the grid item will take precedence.
const draggable =
typeof l.isDraggable === "boolean"
? l.isDraggable
: !l.static && isDraggable;
const resizable =
typeof l.isResizable === "boolean"
? l.isResizable
: !l.static && isResizable;
const resizeHandlesOptions = l.resizeHandles || resizeHandles;
// isBounded set on child if set on parent, and child is not explicitly false
const bounded = draggable && isBounded && l.isBounded !== false;
return (
<GridItem
containerWidth={width}
cols={cols}
margin={margin}
containerPadding={containerPadding || margin}
maxRows={maxRows}
rowHeight={rowHeight}
cancel={draggableCancel}
handle={draggableHandle}
onDragStop={this.onDragStop}
onDragStart={this.onDragStart}
onDrag={this.onDrag}
onResizeStart={this.onResizeStart}
onResize={this.onResize}
onResizeStop={this.onResizeStop}
isDraggable={draggable}
isResizable={resizable}
isBounded={bounded}
useCSSTransforms={useCSSTransforms && mounted}
usePercentages={!mounted}
transformScale={transformScale}
w={l.w}
h={l.h}
x={l.x}
y={l.y}
i={l.i}
minH={l.minH}
minW={l.minW}
maxH={l.maxH}
maxW={l.maxW}
static={l.static}
droppingPosition={isDroppingItem ? droppingPosition : undefined}
resizeHandles={resizeHandlesOptions}
resizeHandle={resizeHandle}
>
{child}
</GridItem>
);
}
// Called while dragging an element. Part of browser native drag/drop API.
// Native event target might be the layout itself, or an element within the layout.
onDragOver: DragOverEvent => void | false = e => {
e.preventDefault(); // Prevent any browser native action
e.stopPropagation();
// we should ignore events from layout's children in Firefox
// to avoid unpredictable jumping of a dropping placeholder
// FIXME remove this hack
if (
isFirefox &&
// $FlowIgnore can't figure this out
!e.nativeEvent.target?.classList.contains(layoutClassName)
) {
return false;
}
const {
droppingItem,
onDropDragOver,
margin,
cols,
rowHeight,
maxRows,
width,
containerPadding,
transformScale
} = this.props;
// Allow user to customize the dropping item or short-circuit the drop based on the results
// of the `onDragOver(e: Event)` callback.
const onDragOverResult = onDropDragOver?.(e);
if (onDragOverResult === false) {
if (this.state.droppingDOMNode) {
this.removeDroppingPlaceholder();
}
return false;
}
const finalDroppingItem = { ...droppingItem, ...onDragOverResult };
const { layout } = this.state;
// $FlowIgnore missing def
const gridRect = e.currentTarget.getBoundingClientRect(); // The grid's position in the viewport
// Calculate the mouse position relative to the grid
const layerX = e.clientX - gridRect.left;
const layerY = e.clientY - gridRect.top;
const droppingPosition = {
left: layerX / transformScale,
top: layerY / transformScale,
e
};
if (!this.state.droppingDOMNode) {
const positionParams: PositionParams = {
cols,
margin,
maxRows,
rowHeight,
containerWidth: width,
containerPadding: containerPadding || margin
};
const calculatedPosition = calcXY(
positionParams,
layerY,
layerX,
finalDroppingItem.w,
finalDroppingItem.h
);
this.setState({
droppingDOMNode: <div key={finalDroppingItem.i} />,
droppingPosition,
layout: [
...layout,
{
...finalDroppingItem,
x: calculatedPosition.x,
y: calculatedPosition.y,
static: false,
isDraggable: true
}
]
});
} else if (this.state.droppingPosition) {
const { left, top } = this.state.droppingPosition;
const shouldUpdatePosition = left != layerX || top != layerY;
if (shouldUpdatePosition) {
this.setState({ droppingPosition });
}
}
};
removeDroppingPlaceholder: () => void = () => {
const { droppingItem, cols } = this.props;
const { layout } = this.state;
const newLayout = compact(
layout.filter(l => l.i !== droppingItem.i),
compactType(this.props),
cols,
this.props.allowOverlap
);
this.setState({
layout: newLayout,
droppingDOMNode: null,
activeDrag: null,
droppingPosition: undefined
});
};
onDragLeave: EventHandler = e => {
e.preventDefault(); // Prevent any browser native action
e.stopPropagation();
this.dragEnterCounter--;
// onDragLeave can be triggered on each layout's child.
// But we know that count of dragEnter and dragLeave events
// will be balanced after leaving the layout's container
// so we can increase and decrease count of dragEnter and
// when it'll be equal to 0 we'll remove the placeholder
if (this.dragEnterCounter === 0) {
this.removeDroppingPlaceholder();
}
};
onDragEnter: EventHandler = e => {
e.preventDefault(); // Prevent any browser native action
e.stopPropagation();
this.dragEnterCounter++;
};
onDrop: EventHandler = (e: Event) => {
e.preventDefault(); // Prevent any browser native action
e.stopPropagation();
const { droppingItem } = this.props;
const { layout } = this.state;
const item = layout.find(l => l.i === droppingItem.i);
// reset dragEnter counter on drop
this.dragEnterCounter = 0;
this.removeDroppingPlaceholder();
this.props.onDrop(layout, item, e);
};
render(): React.Element<"div"> {
const { className, style, isDroppable, innerRef } = this.props;
const mergedClassName = clsx(layoutClassName, className);
const mergedStyle = {
height: this.containerHeight(),
...style
};
return (
<div
ref={innerRef}
className={mergedClassName}
style={mergedStyle}
onDrop={isDroppable ? this.onDrop : noop}
onDragLeave={isDroppable ? this.onDragLeave : noop}
onDragEnter={isDroppable ? this.onDragEnter : noop}
onDragOver={isDroppable ? this.onDragOver : noop}
>
{React.Children.map(this.props.children, child =>
this.processGridItem(child)
)}
{isDroppable &&
this.state.droppingDOMNode &&
this.processGridItem(this.state.droppingDOMNode, true)}
{this.placeholder()}
</div>
);
}
}