Skip to content

Commit 2591bf7

Browse files
committed
Merge react-grid-layout#1296; allow null children
Squashed commit of the following: commit 5dfce15 Author: Jeff Rasmussen <[email protected]> Date: Mon Oct 5 14:51:58 2020 -0600 Check for null or undefined commit 79cf5bd Author: Jeff Rasmussen <[email protected]> Date: Mon Oct 5 14:51:27 2020 -0600 Undo vs code "help" regarding formatting style commit b20a36c Author: Jeff Rasmussen <[email protected]> Date: Mon Oct 5 14:20:44 2020 -0600 Fix missed ! commit 36fede3 Author: Jeff Rasmussen <[email protected]> Date: Mon Oct 5 14:14:14 2020 -0600 change check to validate against null instead of coercion to bool commit c0e3a28 Author: Jeff Rasmussen <[email protected]> Date: Mon Oct 5 14:12:18 2020 -0600 change check to validate against null instead of coercion to bool commit 91a4650 Author: Jeff Rasmussen <[email protected]> Date: Mon Sep 21 12:57:44 2020 -0600 Protect against children that may be null, this happens with conditional rendering https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator
1 parent 87bbbc1 commit 2591bf7

File tree

4 files changed

+1114
-9
lines changed

4 files changed

+1114
-9
lines changed

lib/ReactGridLayoutPropTypes.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,12 @@ export default {
227227

228228
// Children must not have duplicate keys.
229229
children: function (props: Props, propName: string) {
230-
var children = props[propName];
230+
const children = props[propName];
231231

232232
// Check children keys for duplicates. Throw if found.
233-
var keys = {};
233+
const keys = {};
234234
React.Children.forEach(children, function (child) {
235+
if (child?.key == null) return;
235236
if (keys[child.key]) {
236237
throw new Error(
237238
'Duplicate child key "' +

lib/utils.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ export function cloneLayoutItem(layoutItem: LayoutItem): LayoutItem {
159159
*/
160160
export function childrenEqual(a: ReactChildren, b: ReactChildren): boolean {
161161
return isEqual(
162-
React.Children.map(a, c => c.key),
163-
React.Children.map(b, c => c.key)
162+
React.Children.map(a, c => c?.key),
163+
React.Children.map(b, c => c?.key)
164164
);
165165
}
166166

@@ -687,11 +687,14 @@ export function synchronizeLayoutWithChildren(
687687

688688
// Generate one layout item per child.
689689
const layout: LayoutItem[] = [];
690-
React.Children.forEach(children, (child: ReactElement<any>, i: number) => {
690+
React.Children.forEach(children, (child: ReactElement<any>) => {
691+
// Child may not exist
692+
if (child?.key == null) return;
693+
691694
// Don't overwrite if it already exists.
692695
const exists = getLayoutItem(initialLayout, String(child.key));
693696
if (exists) {
694-
layout[i] = cloneLayoutItem(exists);
697+
layout.push(cloneLayoutItem(exists));
695698
} else {
696699
if (!isProduction && child.props._grid) {
697700
console.warn(
@@ -707,17 +710,17 @@ export function synchronizeLayoutWithChildren(
707710
validateLayout([g], "ReactGridLayout.children");
708711
}
709712
// FIXME clone not really necessary here
710-
layout[i] = cloneLayoutItem({ ...g, i: child.key });
713+
layout.push(cloneLayoutItem({ ...g, i: child.key }));
711714
} else {
712715
// Nothing provided: ensure this is added to the bottom
713716
// FIXME clone not really necessary here
714-
layout[i] = cloneLayoutItem({
717+
layout.push(cloneLayoutItem({
715718
w: 1,
716719
h: 1,
717720
x: 0,
718721
y: bottom(layout),
719722
i: String(child.key)
720-
});
723+
}));
721724
}
722725
}
723726
});

0 commit comments

Comments
 (0)