Skip to content

Commit ec7ea2f

Browse files
committed
fix(utils): onLayoutChange didn't clone object in last release
Fixes react-grid-layout#683 and test added
1 parent 11544fe commit ec7ea2f

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export function compact(
152152
const out = Array(layout.length);
153153

154154
for (let i = 0, len = sorted.length; i < len; i++) {
155-
let l = sorted[i];
155+
let l = cloneLayoutItem(sorted[i]);
156156

157157
// Don't move static elements
158158
if (!l.static) {

test/spec/utils-test.js

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ import assert from "power-assert";
1515
/*:: declare function describe(name: string, fn: Function): void; */
1616
/*:: declare function it(name: string, fn: Function): void; */
1717

18+
//
19+
// Utils
20+
//
21+
function stripArray(arr) {
22+
return arr.map(stripObject);
23+
}
24+
25+
function stripObject(obj) {
26+
obj = Object.assign({}, obj);
27+
return Object.keys(obj).reduce((memo, key) => {
28+
if (obj[key] != null) memo[key] = obj[key];
29+
return memo;
30+
}, {});
31+
}
32+
33+
function assertDeepEqualStrip(obj1, obj2) {
34+
assert.deepEqual(stripArray(obj1), stripArray(obj2));
35+
}
36+
//
37+
// Specs
38+
//
39+
1840
describe("bottom", () => {
1941
it("Handles an empty layout as input", () => {
2042
assert(bottom([]) === 0);
@@ -248,8 +270,8 @@ describe("moveElement", () => {
248270
describe("compact vertical", () => {
249271
it("Removes empty vertical space above item", () => {
250272
const layout = [{ i: "1", x: 0, y: 1, w: 1, h: 1 }];
251-
assert.deepEqual(compact(layout, "vertical", 10), [
252-
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }
273+
assertDeepEqualStrip(compact(layout, "vertical", 10), [
274+
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false, static: false }
253275
]);
254276
});
255277

@@ -258,9 +280,9 @@ describe("compact vertical", () => {
258280
{ x: 0, y: 0, w: 1, h: 5, i: "1" },
259281
{ x: 0, y: 1, w: 1, h: 1, i: "2" }
260282
];
261-
assert.deepEqual(compact(layout, "vertical", 10), [
262-
{ x: 0, y: 0, w: 1, h: 5, i: "1", moved: false },
263-
{ x: 0, y: 5, w: 1, h: 1, i: "2", moved: false }
283+
assertDeepEqualStrip(compact(layout, "vertical", 10), [
284+
{ x: 0, y: 0, w: 1, h: 5, i: "1", moved: false, static: false },
285+
{ x: 0, y: 5, w: 1, h: 1, i: "2", moved: false, static: false }
264286
]);
265287
});
266288

@@ -272,21 +294,33 @@ describe("compact vertical", () => {
272294
{ x: 5, y: 2, w: 1, h: 1, i: "4" },
273295
{ x: 5, y: 3, w: 1, h: 1, i: "5", static: true }
274296
];
275-
assert.deepEqual(compact(layout, "vertical", 10), [
276-
{ x: 0, y: 0, w: 2, h: 5, i: "1", moved: false },
277-
{ x: 0, y: 5, w: 10, h: 1, i: "2", moved: false },
278-
{ x: 5, y: 6, w: 1, h: 1, i: "3", moved: false },
279-
{ x: 5, y: 7, w: 1, h: 1, i: "4", moved: false },
297+
298+
assertDeepEqualStrip(compact(layout, "vertical", 10), [
299+
{ x: 0, y: 0, w: 2, h: 5, i: "1", moved: false, static: false },
300+
{ x: 0, y: 5, w: 10, h: 1, i: "2", moved: false, static: false },
301+
{ x: 5, y: 6, w: 1, h: 1, i: "3", moved: false, static: false },
302+
{ x: 5, y: 7, w: 1, h: 1, i: "4", moved: false, static: false },
280303
{ x: 5, y: 3, w: 1, h: 1, i: "5", moved: false, static: true }
281304
]);
282305
});
306+
307+
it("Clones layout items (does not modify input)", () => {
308+
const layout = [
309+
{ x: 0, y: 0, w: 2, h: 5, i: "1" },
310+
{ x: 0, y: 0, w: 10, h: 1, i: "2" }
311+
];
312+
const out = compact(layout, "vertical", 10);
313+
layout.forEach(item => {
314+
assert(!out.includes(item));
315+
});
316+
});
283317
});
284318

285319
describe("compact horizontal", () => {
286320
it("compact horizontal should remove empty horizontal space to left of item", () => {
287-
const layout = [{ i: "1", x: 5, y: 5, w: 1, h: 1 }];
288-
assert.deepEqual(compact(layout, "horizontal", 10), [
289-
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }
321+
const layout = [{ x: 5, y: 5, w: 1, h: 1, i: "1" }];
322+
assertDeepEqualStrip(compact(layout, "horizontal", 10), [
323+
{ x: 0, y: 0, w: 1, h: 1, i: "1", moved: false, static: false }
290324
]);
291325
});
292326

@@ -295,9 +329,9 @@ describe("compact horizontal", () => {
295329
{ y: 0, x: 0, h: 1, w: 5, i: "1" },
296330
{ y: 0, x: 1, h: 1, w: 1, i: "2" }
297331
];
298-
assert.deepEqual(compact(layout, "horizontal", 10), [
299-
{ y: 0, x: 0, h: 1, w: 5, i: "1", moved: false },
300-
{ y: 0, x: 5, h: 1, w: 1, i: "2", moved: false }
332+
assertDeepEqualStrip(compact(layout, "horizontal", 10), [
333+
{ y: 0, x: 0, h: 1, w: 5, i: "1", moved: false, static: false },
334+
{ y: 0, x: 5, h: 1, w: 1, i: "2", moved: false, static: false }
301335
]);
302336
});
303337

@@ -309,11 +343,11 @@ describe("compact horizontal", () => {
309343
{ y: 5, x: 2, h: 1, w: 1, i: "4" },
310344
{ y: 5, x: 2, h: 1, w: 1, i: "5", static: true }
311345
];
312-
assert.deepEqual(compact(layout, "horizontal", 10), [
313-
{ y: 0, x: 0, h: 2, w: 5, i: "1", moved: false },
314-
{ y: 0, x: 5, h: 10, w: 1, i: "2", moved: false },
315-
{ y: 5, x: 6, h: 1, w: 1, i: "3", moved: false },
316-
{ y: 5, x: 7, h: 1, w: 1, i: "4", moved: false },
346+
assertDeepEqualStrip(compact(layout, "horizontal", 10), [
347+
{ y: 0, x: 0, h: 2, w: 5, i: "1", moved: false, static: false },
348+
{ y: 0, x: 5, h: 10, w: 1, i: "2", moved: false, static: false },
349+
{ y: 5, x: 6, h: 1, w: 1, i: "3", moved: false, static: false },
350+
{ y: 5, x: 7, h: 1, w: 1, i: "4", moved: false, static: false },
317351
{ y: 5, x: 2, h: 1, w: 1, i: "5", moved: false, static: true }
318352
]);
319353
});

0 commit comments

Comments
 (0)