Skip to content

Commit 40124b4

Browse files
section does not clone when not necessary
This fixes an issue where non-attr components were lagging in updates by one render cycle
1 parent 3ae82b3 commit 40124b4

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/components/containers/Section.js

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,26 @@ import PropTypes from 'prop-types';
55
import unpackPlotProps from '../../lib/unpackPlotProps';
66
import {containerConnectedContextTypes} from '../../lib/connectToContainer';
77

8-
function childIsVisible(child) {
9-
const attrVisible = Boolean((child.props.plotProps || {}).isVisible);
10-
const sectionVisible = Boolean(child.props['data-section-child-visible']);
11-
return attrVisible || sectionVisible;
12-
}
13-
148
export default class Section extends Component {
159
constructor(props, context) {
1610
super(props, context);
1711

1812
this.children = null;
1913
this.menuPanel = null;
14+
this.sectionVisible = false;
2015

21-
this.processAndSetChildren(context);
16+
this.processAndSetChildren(props, context);
2217
}
2318

2419
componentWillReceiveProps(nextProps, nextContext) {
25-
this.processAndSetChildren(nextContext);
20+
this.processAndSetChildren(nextProps, nextContext);
2621
}
2722

28-
processAndSetChildren(context) {
29-
let children = this.props.children;
30-
if (!Array.isArray(children)) {
31-
children = [children];
32-
}
23+
processAndSetChildren(nextProps, nextContext) {
24+
this.sectionVisible = false;
3325

34-
const attrChildren = [];
26+
const children = React.Children.toArray(nextProps.children);
27+
this.children = [];
3528
let menuPanel = null;
3629

3730
for (let i = 0; i < children.length; i++) {
@@ -40,7 +33,8 @@ export default class Section extends Component {
4033
continue;
4134
}
4235
if (child.type === MenuPanel) {
43-
// Process the first menuPanel. Ignore the rest.
36+
// Process the first menuPanel. Ignore the rest. MenuPanel does
37+
// not affect visibility.
4438
if (menuPanel) {
4539
continue;
4640
}
@@ -55,38 +49,34 @@ export default class Section extends Component {
5549
plotProps = child.plotProps;
5650
} else if (isAttr) {
5751
if (child.type.supplyPlotProps) {
58-
plotProps = child.type.supplyPlotProps(child.props, context);
52+
plotProps = child.type.supplyPlotProps(child.props, nextContext);
5953
if (child.type.modifyPlotProps) {
60-
child.type.modifyPlotProps(child.props, context, plotProps);
54+
child.type.modifyPlotProps(child.props, nextContext, plotProps);
6155
}
6256
} else {
63-
plotProps = unpackPlotProps(child.props, context);
57+
plotProps = unpackPlotProps(child.props, nextContext);
6458
}
6559

6660
// assign plotProps as a prop of children. If they are connectedToContainer
6761
// it will see plotProps and skip recomputing them.
68-
newProps = {plotProps, key: i};
62+
newProps = {plotProps};
63+
this.sectionVisible = this.sectionVisible || plotProps.isVisible;
64+
this.children.push(cloneElement(child, newProps));
6965
} else if (child.type === Info) {
7066
// Info panels do not change section visibility.
71-
newProps = {key: i, 'data-section-child-visible': false};
67+
this.children.push(child);
7268
} else {
7369
// custom UI currently forces section visibility.
74-
newProps = {key: i, 'data-section-child-visible': true};
70+
this.sectionVisible = true;
71+
this.children.push(child);
7572
}
76-
77-
const childProps = Object.assign(newProps, child.props);
78-
attrChildren.push(cloneElement(child, childProps));
7973
}
8074

81-
this.children = attrChildren.length ? attrChildren : null;
8275
this.menuPanel = menuPanel;
8376
}
8477

8578
render() {
86-
const hasVisibleChildren =
87-
this.children && this.children.some(childIsVisible);
88-
89-
return hasVisibleChildren ? (
79+
return this.sectionVisible ? (
9080
<div className="section">
9181
<div className="section__heading">
9282
{this.props.name}

0 commit comments

Comments
 (0)