Skip to content

Commit 52109c5

Browse files
committed
Refactor
1 parent a453179 commit 52109c5

File tree

3 files changed

+68
-108
lines changed

3 files changed

+68
-108
lines changed

src/components/containers/PlotlyFold.js

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,10 @@ import {
1010
striptags,
1111
} from 'lib';
1212

13-
class PlotlyFold extends Component {
14-
constructor(props, context) {
15-
super(props, context);
16-
17-
this.foldVisible = false;
18-
this.determineVisibility(props, context);
19-
}
20-
21-
componentWillReceiveProps(nextProps, nextContext) {
22-
this.determineVisibility(nextProps, nextContext);
23-
}
24-
25-
determineVisibility(nextProps, nextContext) {
26-
this.foldVisible = false;
27-
28-
if (nextProps.forceVisibility) {
29-
this.foldVisible = true;
30-
return;
31-
}
32-
33-
React.Children.forEach(nextProps.children, child => {
34-
if (!child || this.foldVisible) {
35-
return;
36-
}
37-
38-
if (child.props.attr) {
39-
// attr components force fold open if they are visible
40-
const plotProps = unpackPlotProps(child.props, nextContext);
41-
if (child.type.modifyPlotProps) {
42-
child.type.modifyPlotProps(child.props, nextContext, plotProps);
43-
}
44-
45-
this.foldVisible = this.foldVisible || plotProps.isVisible;
46-
return;
47-
}
48-
49-
if (!(child.type.plotly_editor_traits || {}).no_visibility_forcing) {
50-
// non-attr components force visibility (unless they don't via traits)
51-
this.foldVisible = true;
52-
return;
53-
}
54-
});
13+
export class Fold extends Component {
14+
constructor() {
15+
super();
16+
this.foldVisible = true;
5517
}
5618

5719
render() {
@@ -141,20 +103,63 @@ class PlotlyFold extends Component {
141103
}
142104
}
143105

144-
PlotlyFold.plotly_editor_traits = {foldable: true};
106+
Fold.plotly_editor_traits = {foldable: true};
145107

146-
PlotlyFold.propTypes = {
108+
Fold.propTypes = {
147109
canDelete: PropTypes.bool,
148110
children: PropTypes.node,
149111
className: PropTypes.string,
150-
folded: PropTypes.bool.isRequired,
151-
toggleFold: PropTypes.func.isRequired,
112+
folded: PropTypes.bool,
113+
toggleFold: PropTypes.func,
152114
hideHeader: PropTypes.bool,
153115
icon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
154116
messageIfEmpty: PropTypes.string,
155117
localize: PropTypes.func,
156118
name: PropTypes.string,
157-
forceVisibility: PropTypes.bool,
119+
};
120+
121+
class PlotlyFold extends Fold {
122+
constructor(props, context) {
123+
super(props, context);
124+
125+
this.foldVisible = false;
126+
this.determineVisibility(props, context);
127+
}
128+
129+
componentWillReceiveProps(nextProps, nextContext) {
130+
this.determineVisibility(nextProps, nextContext);
131+
}
132+
133+
determineVisibility(nextProps, nextContext) {
134+
this.foldVisible = false;
135+
136+
React.Children.forEach(nextProps.children, child => {
137+
if (!child || this.foldVisible) {
138+
return;
139+
}
140+
141+
if (child.props.attr) {
142+
// attr components force fold open if they are visible
143+
const plotProps = unpackPlotProps(child.props, nextContext);
144+
if (child.type.modifyPlotProps) {
145+
child.type.modifyPlotProps(child.props, nextContext, plotProps);
146+
}
147+
148+
this.foldVisible = this.foldVisible || plotProps.isVisible;
149+
return;
150+
}
151+
152+
if (!(child.type.plotly_editor_traits || {}).no_visibility_forcing) {
153+
// non-attr components force visibility (unless they don't via traits)
154+
this.foldVisible = true;
155+
return;
156+
}
157+
});
158+
}
159+
}
160+
161+
PlotlyFold.plotly_editor_traits = {
162+
foldable: true,
158163
};
159164

160165
PlotlyFold.contextTypes = Object.assign(
@@ -165,26 +170,3 @@ PlotlyFold.contextTypes = Object.assign(
165170
);
166171

167172
export default localize(PlotlyFold);
168-
169-
export class Fold extends PlotlyFold {}
170-
171-
Fold.plotly_editor_traits = {
172-
foldable: true,
173-
};
174-
175-
Fold.defaultProps = {
176-
forceVisibility: true,
177-
};
178-
179-
Fold.propTypes = {
180-
children: PropTypes.node,
181-
className: PropTypes.string,
182-
folded: PropTypes.bool,
183-
toggleFold: PropTypes.func,
184-
hideHeader: PropTypes.bool,
185-
icon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
186-
messageIfEmpty: PropTypes.string,
187-
localize: PropTypes.func,
188-
name: PropTypes.string,
189-
forceVisibility: PropTypes.bool,
190-
};

src/components/containers/PlotlyPanel.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ PanelErrorImpl.propTypes = {
2626

2727
const PanelError = localize(PanelErrorImpl);
2828

29-
class PlotlyPanel extends Component {
29+
export class Panel extends Component {
3030
constructor(props) {
3131
super(props);
3232
this.state = {
@@ -101,7 +101,6 @@ class PlotlyPanel extends Component {
101101
toggleFold: () => this.toggleFold(index),
102102
});
103103
}
104-
105104
return child;
106105
}
107106
);
@@ -124,23 +123,19 @@ class PlotlyPanel extends Component {
124123
}
125124
}
126125

127-
PlotlyPanel.plotly_editor_traits = {no_visibility_forcing: true};
128-
129-
PlotlyPanel.propTypes = {
126+
Panel.propTypes = {
130127
children: PropTypes.node,
131128
addAction: PropTypes.object,
132129
showExpandCollapse: PropTypes.bool,
133130
noPadding: PropTypes.bool,
134131
};
135132

136-
PlotlyPanel.defaultProps = {
133+
Panel.defaultProps = {
137134
showExpandCollapse: true,
138135
};
139136

140-
export default PlotlyPanel;
141-
142-
export class Panel extends PlotlyPanel {}
137+
export default class PlotlyPanel extends Panel {}
143138

144-
Panel.plotly_editor_traits = {
145-
no_visibility_forcing: false,
139+
PlotlyPanel.plotly_editor_traits = {
140+
no_visibility_forcing: true,
146141
};

src/components/containers/PlotlySection.js

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import {
77
} from '../../lib';
88

99
export class Section extends Component {
10+
constructor() {
11+
super();
12+
this.sectionVisible = true;
13+
}
14+
1015
render() {
16+
if (!this.sectionVisible) {
17+
return null;
18+
}
19+
1120
return (
1221
<div className="section">
1322
{this.props.name ? (
@@ -31,9 +40,6 @@ Section.propTypes = {
3140
class PlotlySection extends Section {
3241
constructor(props, context) {
3342
super(props, context);
34-
35-
this.sectionVisible = false;
36-
3743
this.determineVisibility(props, context);
3844
}
3945

@@ -46,7 +52,7 @@ class PlotlySection extends Section {
4652
this.sectionVisible = Boolean(isVisible);
4753

4854
React.Children.forEach(nextProps.children, child => {
49-
if (!child || this.foldVisible) {
55+
if (!child || this.sectionVisible) {
5056
return;
5157
}
5258

@@ -66,31 +72,8 @@ class PlotlySection extends Section {
6672
}
6773
});
6874
}
69-
70-
render() {
71-
if (!this.sectionVisible) {
72-
return null;
73-
}
74-
return (
75-
<div className="section">
76-
{this.props.name ? (
77-
<div className="section__heading">
78-
<div className="section__heading__text">{this.props.name}</div>
79-
</div>
80-
) : null}
81-
{this.props.children}
82-
</div>
83-
);
84-
}
8575
}
8676

8777
PlotlySection.plotly_editor_traits = {no_visibility_forcing: true};
88-
89-
PlotlySection.propTypes = {
90-
children: PropTypes.node,
91-
name: PropTypes.string,
92-
attr: PropTypes.string,
93-
};
94-
9578
PlotlySection.contextTypes = containerConnectedContextTypes;
9679
export default localize(PlotlySection);

0 commit comments

Comments
 (0)