react-grid-layout 0.18.2 → 1.1.1

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.
@@ -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,9 +103,11 @@ 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(
107
+ breakpoints: Breakpoints<Breakpoint>
108
+ ): Array<Breakpoint> {
114
109
  const keys: Array<string> = Object.keys(breakpoints);
115
- return keys.sort(function(a, b) {
110
+ return keys.sort(function (a, b) {
116
111
  return breakpoints[a] - breakpoints[b];
117
112
  });
118
113
  }
package/lib/utils.js CHANGED
@@ -18,9 +18,11 @@ export type LayoutItem = {
18
18
  moved?: boolean,
19
19
  static?: boolean,
20
20
  isDraggable?: ?boolean,
21
- isResizable?: ?boolean
21
+ isResizable?: ?boolean,
22
+ resizeHandles?: Array<'s' | 'w' | 'e' | 'n' | 'sw' | 'nw' | 'se' | 'ne'>,
23
+ isBounded?: ?boolean
22
24
  };
23
- export type Layout = Array<LayoutItem>;
25
+ export type Layout = $ReadOnlyArray<LayoutItem>;
24
26
  export type Position = {
25
27
  left: number,
26
28
  top: number,
@@ -111,9 +113,11 @@ export function cloneLayoutItem(layoutItem: LayoutItem): LayoutItem {
111
113
  maxH: layoutItem.maxH,
112
114
  moved: Boolean(layoutItem.moved),
113
115
  static: Boolean(layoutItem.static),
114
- // These can be null
116
+ // These can be null/undefined
115
117
  isDraggable: layoutItem.isDraggable,
116
- isResizable: layoutItem.isResizable
118
+ isResizable: layoutItem.isResizable,
119
+ resizeHandles: layoutItem.resizeHandles,
120
+ isBounded: layoutItem.isBounded
117
121
  };
118
122
  }
119
123
 
@@ -165,6 +169,8 @@ export function collides(l1: LayoutItem, l2: LayoutItem): boolean {
165
169
  * Given a layout, compact it. This involves going down each y coordinate and removing gaps
166
170
  * between items.
167
171
  *
172
+ * Does not modify layout items (clones). Creates a new layout array.
173
+ *
168
174
  * @param {Array} layout Layout.
169
175
  * @param {Boolean} verticalCompact Whether or not to compact the layout
170
176
  * vertically.
@@ -247,6 +253,9 @@ function resolveCompactionCollision(
247
253
 
248
254
  /**
249
255
  * Compact an item in the layout.
256
+ *
257
+ * Modifies item.
258
+ *
250
259
  */
251
260
  export function compactItem(
252
261
  compareWith: Layout,
@@ -294,6 +303,8 @@ export function compactItem(
294
303
  /**
295
304
  * Given a layout, make sure all elements fit within its bounds.
296
305
  *
306
+ * Modifies layout items.
307
+ *
297
308
  * @param {Array} layout Layout array.
298
309
  * @param {Number} bounds Number of columns.
299
310
  */
@@ -372,6 +383,8 @@ export function getStatics(layout: Layout): Array<LayoutItem> {
372
383
  /**
373
384
  * Move an element. Responsible for doing cascading movements of other elements.
374
385
  *
386
+ * Modifies layout items.
387
+ *
375
388
  * @param {Array} layout Full layout to modify.
376
389
  * @param {LayoutItem} l element to move.
377
390
  * @param {Number} [x] X position in grid units.
@@ -416,6 +429,7 @@ export function moveElement(
416
429
  : compactType === "horizontal" && typeof x === "number"
417
430
  ? oldX >= x
418
431
  : false;
432
+ // $FlowIgnore acceptable modification of read-only array as it was recently cloned
419
433
  if (movingUp) sorted = sorted.reverse();
420
434
  const collisions = getAllCollisions(sorted, l);
421
435
 
@@ -579,8 +593,14 @@ export function sortLayoutItems(
579
593
  else return sortLayoutItemsByRowCol(layout);
580
594
  }
581
595
 
596
+ /**
597
+ * Sort layout items by row ascending and column ascending.
598
+ *
599
+ * Does not modify Layout.
600
+ */
582
601
  export function sortLayoutItemsByRowCol(layout: Layout): Layout {
583
- return [].concat(layout).sort(function(a, b) {
602
+ // Slice to clone array as sort modifies
603
+ return layout.slice(0).sort(function (a, b) {
584
604
  if (a.y > b.y || (a.y === b.y && a.x > b.x)) {
585
605
  return 1;
586
606
  } else if (a.y === b.y && a.x === b.x) {
@@ -591,8 +611,13 @@ export function sortLayoutItemsByRowCol(layout: Layout): Layout {
591
611
  });
592
612
  }
593
613
 
614
+ /**
615
+ * Sort layout items by column ascending then row ascending.
616
+ *
617
+ * Does not modify Layout.
618
+ */
594
619
  export function sortLayoutItemsByColRow(layout: Layout): Layout {
595
- return [].concat(layout).sort(function(a, b) {
620
+ return layout.slice(0).sort(function (a, b) {
596
621
  if (a.x > b.x || (a.x === b.x && a.y > b.y)) {
597
622
  return 1;
598
623
  }
@@ -604,6 +629,8 @@ export function sortLayoutItemsByColRow(layout: Layout): Layout {
604
629
  * Generate a layout using the initialLayout and children as a template.
605
630
  * Missing entries will be added, extraneous ones will be truncated.
606
631
  *
632
+ * Does not modify initialLayout.
633
+ *
607
634
  * @param {Array} initialLayout Layout passed in through props.
608
635
  * @param {String} breakpoint Current responsive breakpoint.
609
636
  * @param {?String} compact Compaction option.
@@ -618,7 +645,7 @@ export function synchronizeLayoutWithChildren(
618
645
  initialLayout = initialLayout || [];
619
646
 
620
647
  // Generate one layout item per child.
621
- let layout: Layout = [];
648
+ const layout: LayoutItem[] = [];
622
649
  React.Children.forEach(children, (child: ReactElement<any>, i: number) => {
623
650
  // Don't overwrite if it already exists.
624
651
  const exists = getLayoutItem(initialLayout, String(child.key));
@@ -653,10 +680,8 @@ export function synchronizeLayoutWithChildren(
653
680
  });
654
681
 
655
682
  // Correct the layout.
656
- layout = correctBounds(layout, { cols: cols });
657
- layout = compact(layout, compactType, cols);
658
-
659
- return layout;
683
+ const correctedLayout = correctBounds(layout, { cols: cols });
684
+ return compact(correctedLayout, compactType, cols);
660
685
  }
661
686
 
662
687
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-grid-layout",
3
- "version": "0.18.2",
3
+ "version": "1.1.1",
4
4
  "description": "A draggable and resizable grid layout with responsive breakpoints, for React.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -39,49 +39,59 @@
39
39
  "lodash.isequal": "^4.0.0",
40
40
  "prop-types": "^15.0.0",
41
41
  "react-draggable": "^4.0.0",
42
- "react-resizable": "^1.9.0"
42
+ "react-resizable": "^1.10.0"
43
43
  },
44
44
  "devDependencies": {
45
- "@babel/cli": "^7.8.0",
46
- "@babel/core": "^7.8.0",
47
- "@babel/plugin-proposal-class-properties": "^7.8.0",
48
- "@babel/plugin-transform-flow-comments": "^7.8.0",
49
- "@babel/preset-env": "^7.8.2",
50
- "@babel/preset-flow": "^7.8.0",
51
- "@babel/preset-react": "^7.8.0",
52
- "@babel/register": "^7.8.0",
53
- "babel-eslint": "^10.0.3",
54
- "babel-jest": "^25.1.0",
55
- "babel-loader": "^8.0.6",
56
- "babel-plugin-preval": "^4.0.0",
57
- "css-loader": "^3.4.2",
58
- "ejs": "^3.0.1",
59
- "eslint": "^6.8.0",
60
- "eslint-plugin-flowtype": "^4.6.0",
61
- "eslint-plugin-mocha": "^6.2.2",
62
- "eslint-plugin-react": "^7.17.0",
63
- "exports-loader": "^0.7.0",
64
- "flow-bin": "^0.119.0",
65
- "husky": "^3.0.9",
66
- "imports-loader": "^0.8.0",
67
- "jest-cli": "^24.9.0",
68
- "lint-staged": "^9.5.0",
69
- "lodash": "^4.17.5",
70
- "opener": "^1.4.3",
71
- "prettier": "^1.19.1",
72
- "react": "^16.12.0",
73
- "react-dom": "^16.12.0",
74
- "react-hot-loader": "^4.12.18",
45
+ "@babel/cli": "^7.11.6",
46
+ "@babel/core": "^7.11.6",
47
+ "@babel/plugin-proposal-class-properties": "^7.10.4",
48
+ "@babel/plugin-transform-flow-comments": "^7.10.4",
49
+ "@babel/preset-env": "^7.11.5",
50
+ "@babel/preset-flow": "^7.10.4",
51
+ "@babel/preset-react": "^7.10.4",
52
+ "@babel/register": "^7.11.5",
53
+ "babel-eslint": "^10.1.0",
54
+ "babel-jest": "^26.3.0",
55
+ "babel-loader": "^8.1.0",
56
+ "babel-plugin-preval": "^5.0.0",
57
+ "css-loader": "^4.2.2",
58
+ "ejs": "^3.1.5",
59
+ "enzyme": "^3.11.0",
60
+ "enzyme-adapter-react-16": "^1.15.4",
61
+ "enzyme-to-json": "^3.5.0",
62
+ "eslint": "^7.8.1",
63
+ "eslint-plugin-flowtype": "^5.2.0",
64
+ "eslint-plugin-mocha": "^8.0.0",
65
+ "eslint-plugin-react": "^7.20.6",
66
+ "exports-loader": "^1.1.0",
67
+ "flow-bin": "^0.133.0",
68
+ "husky": "^4.2.5",
69
+ "imports-loader": "^1.1.0",
70
+ "jest": "^26.4.2",
71
+ "lint-staged": "^10.3.0",
72
+ "lodash": "^4.17.20",
73
+ "opener": "^1.5.2",
74
+ "prettier": "^2.1.1",
75
+ "react": "^16.13.1",
76
+ "react-dom": "^16.13.1",
77
+ "react-hot-loader": "^4.12.21",
75
78
  "react-transform-hmr": "^1.0.2",
76
- "style-loader": "^1.1.2",
77
- "webpack": "^4.41.5",
78
- "webpack-cli": "^3.3.10",
79
- "webpack-dev-server": "^3.10.1"
79
+ "style-loader": "^1.2.1",
80
+ "timsort": "^0.3.0",
81
+ "webpack": "^4.44.1",
82
+ "webpack-cli": "^3.3.12",
83
+ "webpack-dev-server": "^3.11.0"
80
84
  },
81
85
  "publishConfig": {
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
  ],
@@ -1,16 +0,0 @@
1
- // @flow
2
- // flow-typed signature: 3fba12a77525f37b0492c8dab3e04f0e
3
- // flow-typed version: 94e9f7e0a4/classnames_v2.x.x/flow_>=v0.28.x
4
-
5
- type $npm$classnames$Classes =
6
- | string
7
- | { [className: string]: ?boolean }
8
- | Array<string>
9
- | void
10
- | null;
11
-
12
- declare module "classnames" {
13
- declare module.exports: (
14
- ...classes: Array<$npm$classnames$Classes>
15
- ) => string;
16
- }