react-grid-layout 0.18.2 → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/GridItem.jsx CHANGED
@@ -182,6 +182,7 @@ export default class GridItem extends React.Component<Props, State> {
182
182
  // We can't deeply compare children. If the developer memoizes them, we can
183
183
  // use this optimization.
184
184
  if (this.props.children !== nextProps.children) return true;
185
+ if (this.props.droppingPosition !== nextProps.droppingPosition) return true;
185
186
  // TODO memoize these calculations so they don't take so long?
186
187
  const oldPosition = calcGridItemPosition(
187
188
  this.getPositionParams(this.props),
@@ -205,6 +206,10 @@ export default class GridItem extends React.Component<Props, State> {
205
206
  );
206
207
  }
207
208
 
209
+ componentDidMount() {
210
+ this.moveDroppingItem({});
211
+ }
212
+
208
213
  componentDidUpdate(prevProps: Props) {
209
214
  this.moveDroppingItem(prevProps);
210
215
  }
@@ -213,12 +218,13 @@ export default class GridItem extends React.Component<Props, State> {
213
218
  // this element by `x, y` pixels.
214
219
  moveDroppingItem(prevProps: Props) {
215
220
  const { droppingPosition } = this.props;
216
- const prevDroppingPosition = prevProps.droppingPosition;
217
- const { dragging } = this.state;
221
+ if (!droppingPosition) return;
218
222
 
219
- if (!droppingPosition || !prevDroppingPosition) {
220
- return;
221
- }
223
+ const prevDroppingPosition = prevProps.droppingPosition || {
224
+ left: 0,
225
+ top: 0
226
+ };
227
+ const { dragging } = this.state;
222
228
 
223
229
  if (!this.currentNode) {
224
230
  // eslint-disable-next-line react/no-find-dom-node
@@ -202,7 +202,8 @@ export default class ReactGridLayout extends React.Component<Props, State> {
202
202
  // handle changes properly, performance will increase.
203
203
  this.props.children !== nextProps.children ||
204
204
  !fastRGLPropsEqual(this.props, nextProps, isEqual) ||
205
- !isEqual(this.state.activeDrag, nextState.activeDrag)
205
+ this.state.activeDrag !== nextState.activeDrag ||
206
+ this.state.droppingPosition !== nextState.droppingPosition
206
207
  );
207
208
  }
208
209
 
@@ -7,15 +7,17 @@ import {
7
7
  cloneLayout,
8
8
  synchronizeLayoutWithChildren,
9
9
  validateLayout,
10
- noop
10
+ noop,
11
+ type Layout
11
12
  } from "./utils";
12
13
  import {
13
14
  getBreakpointFromWidth,
14
15
  getColsFromBreakpoint,
15
- findOrGenerateResponsiveLayout
16
+ findOrGenerateResponsiveLayout,
17
+ type ResponsiveLayout,
18
+ type Breakpoints,
16
19
  } from "./responsiveUtils";
17
20
  import ReactGridLayout from "./ReactGridLayout";
18
- import type { Layout } from "./utils";
19
21
 
20
22
  const type = obj => Object.prototype.toString.call(obj);
21
23
 
@@ -46,9 +48,9 @@ type Props<Breakpoint: string = string> = {|
46
48
 
47
49
  // Responsive config
48
50
  breakpoint?: ?Breakpoint,
49
- breakpoints: { [key: Breakpoint]: number },
51
+ breakpoints: Breakpoints<Breakpoint>,
50
52
  cols: { [key: Breakpoint]: number },
51
- layouts: { [key: Breakpoint]: Layout },
53
+ layouts: ResponsiveLayout<Breakpoint>,
52
54
  width: number,
53
55
  margin: { [key: Breakpoint]: [number, number] } | [number, number],
54
56
  containerPadding: { [key: Breakpoint]: [number, number] } | [number, number],
@@ -4,22 +4,15 @@ import { cloneLayout, compact, correctBounds } from "./utils";
4
4
 
5
5
  import type { CompactType, Layout } from "./utils";
6
6
 
7
- export type ResponsiveLayout = {
8
- lg?: Layout,
9
- md?: Layout,
10
- sm?: Layout,
11
- xs?: Layout,
12
- xxs?: Layout
13
- };
14
-
15
- type Breakpoint = string;
7
+ export type Breakpoint = string;
8
+ export type DefaultBreakpoints = 'lg' | 'md' | 'sm' | 'xs' | 'xxs';
16
9
 
17
- type Breakpoints = {
18
- lg?: number,
19
- md?: number,
20
- sm?: number,
21
- xs?: number,
22
- xxs?: number
10
+ // + indicates read-only
11
+ export type ResponsiveLayout<T: Breakpoint> = {
12
+ +[breakpoint: T]: Layout
13
+ };
14
+ export type Breakpoints<T: Breakpoint> = {
15
+ +[breakpoint: T]: number
23
16
  };
24
17
 
25
18
  /**
@@ -30,7 +23,7 @@ type Breakpoints = {
30
23
  * @return {String} Highest breakpoint that is less than width.
31
24
  */
32
25
  export function getBreakpointFromWidth(
33
- breakpoints: Breakpoints,
26
+ breakpoints: Breakpoints<Breakpoint>,
34
27
  width: number
35
28
  ): Breakpoint {
36
29
  const sorted = sortBreakpoints(breakpoints);
@@ -50,7 +43,7 @@ export function getBreakpointFromWidth(
50
43
  */
51
44
  export function getColsFromBreakpoint(
52
45
  breakpoint: Breakpoint,
53
- cols: Breakpoints
46
+ cols: Breakpoints<Breakpoint>
54
47
  ): number {
55
48
  if (!cols[breakpoint]) {
56
49
  throw new Error(
@@ -77,8 +70,8 @@ export function getColsFromBreakpoint(
77
70
  * @return {Array} New layout.
78
71
  */
79
72
  export function findOrGenerateResponsiveLayout(
80
- layouts: ResponsiveLayout,
81
- breakpoints: Breakpoints,
73
+ layouts: ResponsiveLayout<Breakpoint>,
74
+ breakpoints: Breakpoints<Breakpoint>,
82
75
  breakpoint: Breakpoint,
83
76
  lastBreakpoint: Breakpoint,
84
77
  cols: number,
@@ -110,7 +103,7 @@ export function findOrGenerateResponsiveLayout(
110
103
  * @param {Object} breakpoints Key/value pair of breakpoint names to widths.
111
104
  * @return {Array} Sorted breakpoints.
112
105
  */
113
- export function sortBreakpoints(breakpoints: Breakpoints): Array<Breakpoint> {
106
+ export function sortBreakpoints(breakpoints: Breakpoints<Breakpoint>): Array<Breakpoint> {
114
107
  const keys: Array<string> = Object.keys(breakpoints);
115
108
  return keys.sort(function(a, b) {
116
109
  return breakpoints[a] - breakpoints[b];
package/lib/utils.js CHANGED
@@ -20,7 +20,7 @@ export type LayoutItem = {
20
20
  isDraggable?: ?boolean,
21
21
  isResizable?: ?boolean
22
22
  };
23
- export type Layout = Array<LayoutItem>;
23
+ export type Layout = $ReadOnlyArray<LayoutItem>;
24
24
  export type Position = {
25
25
  left: number,
26
26
  top: number,
@@ -165,6 +165,8 @@ export function collides(l1: LayoutItem, l2: LayoutItem): boolean {
165
165
  * Given a layout, compact it. This involves going down each y coordinate and removing gaps
166
166
  * between items.
167
167
  *
168
+ * Does not modify layout items (clones). Creates a new layout array.
169
+ *
168
170
  * @param {Array} layout Layout.
169
171
  * @param {Boolean} verticalCompact Whether or not to compact the layout
170
172
  * vertically.
@@ -247,6 +249,9 @@ function resolveCompactionCollision(
247
249
 
248
250
  /**
249
251
  * Compact an item in the layout.
252
+ *
253
+ * Modifies item.
254
+ *
250
255
  */
251
256
  export function compactItem(
252
257
  compareWith: Layout,
@@ -294,6 +299,8 @@ export function compactItem(
294
299
  /**
295
300
  * Given a layout, make sure all elements fit within its bounds.
296
301
  *
302
+ * Modifies layout items.
303
+ *
297
304
  * @param {Array} layout Layout array.
298
305
  * @param {Number} bounds Number of columns.
299
306
  */
@@ -372,6 +379,8 @@ export function getStatics(layout: Layout): Array<LayoutItem> {
372
379
  /**
373
380
  * Move an element. Responsible for doing cascading movements of other elements.
374
381
  *
382
+ * Modifies layout items.
383
+ *
375
384
  * @param {Array} layout Full layout to modify.
376
385
  * @param {LayoutItem} l element to move.
377
386
  * @param {Number} [x] X position in grid units.
@@ -416,6 +425,7 @@ export function moveElement(
416
425
  : compactType === "horizontal" && typeof x === "number"
417
426
  ? oldX >= x
418
427
  : false;
428
+ // $FlowIgnore acceptable modification of read-only array as it was recently cloned
419
429
  if (movingUp) sorted = sorted.reverse();
420
430
  const collisions = getAllCollisions(sorted, l);
421
431
 
@@ -579,8 +589,14 @@ export function sortLayoutItems(
579
589
  else return sortLayoutItemsByRowCol(layout);
580
590
  }
581
591
 
592
+ /**
593
+ * Sort layout items by row ascending and column ascending.
594
+ *
595
+ * Does not modify Layout.
596
+ */
582
597
  export function sortLayoutItemsByRowCol(layout: Layout): Layout {
583
- return [].concat(layout).sort(function(a, b) {
598
+ // Slice to clone array as sort modifies
599
+ return layout.slice(0).sort(function(a, b) {
584
600
  if (a.y > b.y || (a.y === b.y && a.x > b.x)) {
585
601
  return 1;
586
602
  } else if (a.y === b.y && a.x === b.x) {
@@ -591,8 +607,13 @@ export function sortLayoutItemsByRowCol(layout: Layout): Layout {
591
607
  });
592
608
  }
593
609
 
610
+ /**
611
+ * Sort layout items by column ascending then row ascending.
612
+ *
613
+ * Does not modify Layout.
614
+ */
594
615
  export function sortLayoutItemsByColRow(layout: Layout): Layout {
595
- return [].concat(layout).sort(function(a, b) {
616
+ return layout.slice(0).sort(function(a, b) {
596
617
  if (a.x > b.x || (a.x === b.x && a.y > b.y)) {
597
618
  return 1;
598
619
  }
@@ -604,6 +625,8 @@ export function sortLayoutItemsByColRow(layout: Layout): Layout {
604
625
  * Generate a layout using the initialLayout and children as a template.
605
626
  * Missing entries will be added, extraneous ones will be truncated.
606
627
  *
628
+ * Does not modify initialLayout.
629
+ *
607
630
  * @param {Array} initialLayout Layout passed in through props.
608
631
  * @param {String} breakpoint Current responsive breakpoint.
609
632
  * @param {?String} compact Compaction option.
@@ -618,7 +641,7 @@ export function synchronizeLayoutWithChildren(
618
641
  initialLayout = initialLayout || [];
619
642
 
620
643
  // Generate one layout item per child.
621
- let layout: Layout = [];
644
+ const layout: LayoutItem[] = [];
622
645
  React.Children.forEach(children, (child: ReactElement<any>, i: number) => {
623
646
  // Don't overwrite if it already exists.
624
647
  const exists = getLayoutItem(initialLayout, String(child.key));
@@ -653,10 +676,8 @@ export function synchronizeLayoutWithChildren(
653
676
  });
654
677
 
655
678
  // Correct the layout.
656
- layout = correctBounds(layout, { cols: cols });
657
- layout = compact(layout, compactType, cols);
658
-
659
- return layout;
679
+ const correctedLayout = correctBounds(layout, { cols: cols });
680
+ return compact(correctedLayout, compactType, cols);
660
681
  }
661
682
 
662
683
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-grid-layout",
3
- "version": "0.18.2",
3
+ "version": "0.18.3",
4
4
  "description": "A draggable and resizable grid layout with responsive breakpoints, for React.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -56,6 +56,9 @@
56
56
  "babel-plugin-preval": "^4.0.0",
57
57
  "css-loader": "^3.4.2",
58
58
  "ejs": "^3.0.1",
59
+ "enzyme": "^3.11.0",
60
+ "enzyme-adapter-react-16": "^1.15.2",
61
+ "enzyme-to-json": "^3.4.4",
59
62
  "eslint": "^6.8.0",
60
63
  "eslint-plugin-flowtype": "^4.6.0",
61
64
  "eslint-plugin-mocha": "^6.2.2",
@@ -64,7 +67,7 @@
64
67
  "flow-bin": "^0.119.0",
65
68
  "husky": "^3.0.9",
66
69
  "imports-loader": "^0.8.0",
67
- "jest-cli": "^24.9.0",
70
+ "jest": "^24.9.0",
68
71
  "lint-staged": "^9.5.0",
69
72
  "lodash": "^4.17.5",
70
73
  "opener": "^1.4.3",
@@ -74,6 +77,7 @@
74
77
  "react-hot-loader": "^4.12.18",
75
78
  "react-transform-hmr": "^1.0.2",
76
79
  "style-loader": "^1.1.2",
80
+ "timsort": "^0.3.0",
77
81
  "webpack": "^4.41.5",
78
82
  "webpack-cli": "^3.3.10",
79
83
  "webpack-dev-server": "^3.10.1"
@@ -82,6 +86,12 @@
82
86
  "registry": "https://registry.npmjs.org"
83
87
  },
84
88
  "jest": {
89
+ "setupFilesAfterEnv": [
90
+ "<rootDir>test/util/setupTests.js"
91
+ ],
92
+ "snapshotSerializers": [
93
+ "enzyme-to-json/serializer"
94
+ ],
85
95
  "testMatch": [
86
96
  "<rootDir>/test/spec/*.js"
87
97
  ],