Skip to content

Commit b9a2e62

Browse files
change API for modifying framework
Instead of modifyPlotProps we have a more general supplyPlotProps. If a supplyPlotProps is present it is run. Most implementations of supplyPlotProps will run `unpackPlotProps` first and then alter plotProps as necessary. However, this enables us to now alter `props` and `context` _before_ running unpackPlotProps. This will allows us to "fix" the problem of nested layout fields within a trace context. See #58 (comment) for background.
1 parent a781939 commit b9a2e62

14 files changed

+313
-190
lines changed

src/__tests__/nestedConnections-test.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/components/containers/Section.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import MenuPanel from './MenuPanel';
22
import React, {Component, cloneElement} from 'react';
33
import PropTypes from 'prop-types';
44
import unpackPlotProps from '../../lib/unpackPlotProps';
5+
import {containerConnectedContextTypes} from '../../lib/connectToContainer';
56

67
function childIsVisible(child) {
78
return Boolean((child.props.plotProps || {}).isVisible);
89
}
910

10-
class Section extends Component {
11+
export default class Section extends Component {
1112
constructor(props, context) {
1213
super(props, context);
1314

@@ -49,7 +50,11 @@ class Section extends Component {
4950
if (child.plotProps) {
5051
plotProps = child.plotProps;
5152
} else if (isAttr) {
52-
plotProps = unpackPlotProps(child.props, context, child.type);
53+
if (child.type.supplyPlotProps) {
54+
plotProps = child.type.supplyPlotProps(child.props, context);
55+
} else {
56+
plotProps = unpackPlotProps(child.props, context);
57+
}
5358
} else {
5459
plotProps = {isVisible: true};
5560
}
@@ -84,12 +89,4 @@ Section.propTypes = {
8489
name: PropTypes.string,
8590
};
8691

87-
Section.contextTypes = {
88-
container: PropTypes.object,
89-
defaultContainer: PropTypes.object,
90-
fullContainer: PropTypes.object,
91-
getValObject: PropTypes.func,
92-
updateContainer: PropTypes.func,
93-
};
94-
95-
export default Section;
92+
Section.contextTypes = containerConnectedContextTypes;

src/components/fields/AxesRange.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import Numeric from './Numeric';
2-
import React, {Component} from 'react';
3-
import {connectToContainer} from '../../lib';
2+
import {connectToContainer, unpackPlotProps} from '../../lib';
43

5-
class AxesRange extends Component {
6-
static modifyPlotProps(props, context, plotProps) {
7-
if (!plotProps.isVisible) {
8-
return;
9-
}
10-
const {fullContainer} = plotProps;
11-
if (fullContainer && fullContainer.autorange) {
12-
plotProps.isVisible = false;
13-
}
14-
}
15-
16-
render() {
17-
return <Numeric {...this.props} />;
4+
function supplyPlotProps(props, context) {
5+
const plotProps = unpackPlotProps(props, context);
6+
const {fullContainer} = plotProps;
7+
if (plotProps.isVisible && fullContainer && fullContainer.autorange) {
8+
plotProps.isVisible = false;
189
}
10+
return plotProps;
1911
}
2012

21-
AxesRange.propTypes = {
22-
...Numeric.propTypes,
23-
};
24-
25-
AxesRange.defaultProps = {
26-
showArrows: false,
27-
};
28-
29-
export default connectToContainer(AxesRange);
13+
export default connectToContainer(Numeric, {supplyPlotProps});

src/components/fields/CanvasSize.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
import Numeric from './Numeric';
2-
import React, {Component} from 'react';
3-
import {connectToContainer} from '../../lib';
2+
import {connectToContainer, unpackPlotProps} from '../../lib';
43

5-
class CanvasSize extends Component {
6-
static modifyPlotProps(props, context, plotProps) {
7-
if (!plotProps.isVisible) {
8-
return;
9-
}
10-
const {fullContainer} = plotProps;
11-
if (fullContainer && fullContainer.autosize) {
12-
plotProps.isVisible = false;
13-
}
14-
}
15-
16-
render() {
17-
return <Numeric {...this.props} />;
4+
function supplyPlotProps(props, context) {
5+
const plotProps = unpackPlotProps(props, context);
6+
const {fullContainer} = plotProps;
7+
if (plotProps.isVisible && fullContainer && fullContainer.autosize) {
8+
plotProps.isVisible = false;
189
}
10+
return plotProps;
1911
}
2012

21-
CanvasSize.propTypes = {
22-
...Numeric.propTypes,
23-
};
24-
25-
export default connectToContainer(CanvasSize);
13+
export default connectToContainer(Numeric, {supplyPlotProps});

src/components/fields/DataSelector.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ import PropTypes from 'prop-types';
33
import React, {Component} from 'react';
44
import Field from './Field';
55
import nestedProperty from 'plotly.js/src/lib/nested_property';
6-
import {connectToContainer} from '../../lib';
6+
import {connectToContainer, unpackPlotProps} from '../../lib';
77

88
function attributeIsData(meta = {}) {
99
return meta.valType === 'data_array' || meta.arrayOk;
1010
}
1111

1212
class DataSelector extends Component {
13-
static modifyPlotProps(props, context, plotProps) {
14-
if (attributeIsData(plotProps.attrMeta)) {
15-
plotProps.isVisible = true;
16-
}
17-
}
18-
1913
constructor(props) {
2014
super(props);
2115

@@ -72,4 +66,12 @@ DataSelector.propTypes = {
7266
...Field.propTypes,
7367
};
7468

75-
export default connectToContainer(DataSelector);
69+
function supplyPlotProps(props, context) {
70+
const plotProps = unpackPlotProps(props, context);
71+
if (attributeIsData(plotProps.attrMeta)) {
72+
plotProps.isVisible = true;
73+
}
74+
return plotProps;
75+
}
76+
77+
export default connectToContainer(DataSelector, {supplyPlotProps});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import {connectLayoutToPlot} from '../../../lib';
3+
import {Panel, Fold, Numeric} from '../../';
4+
import {TestEditor, fixtures} from '../../../lib/test-utils';
5+
import {mount} from 'enzyme';
6+
import CanvasSize from '../CanvasSize';
7+
8+
describe('CanvasSize', () => {
9+
const LayoutPanel = connectLayoutToPlot(Panel);
10+
11+
it('is hidden when autosize is true', () => {
12+
const fixtureProps = fixtures.scatter({layout: {autosize: true}});
13+
const wrapper = mount(
14+
<TestEditor {...{...fixtureProps}}>
15+
<LayoutPanel name="Layout">
16+
<Fold name="Canvas">
17+
<CanvasSize attr="width" />
18+
</Fold>
19+
</LayoutPanel>
20+
</TestEditor>
21+
).find(Numeric);
22+
23+
expect(wrapper.length).toBe(0);
24+
});
25+
26+
it('is visible when autosize is false', () => {
27+
const fixtureProps = fixtures.scatter({layout: {autosize: false}});
28+
const wrapper = mount(
29+
<TestEditor {...{...fixtureProps}}>
30+
<LayoutPanel name="Layout">
31+
<Fold name="Canvas">
32+
<CanvasSize attr="width" />
33+
</Fold>
34+
</LayoutPanel>
35+
</TestEditor>
36+
).find(Numeric);
37+
38+
expect(wrapper.length).toBe(1);
39+
});
40+
});

src/__tests__/connectLayoutToPlot-test.js renamed to src/lib/__tests__/connectLayoutToPlot-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Numeric} from '../components/fields';
2-
import {Fold, Panel, Section} from '../components/containers';
3-
import NumericInput from '../components/widgets/NumericInputStatefulWrapper';
1+
import NumericInput from '../../components/widgets/NumericInputStatefulWrapper';
42
import React from 'react';
5-
import {EDITOR_ACTIONS} from '../constants';
6-
import {TestEditor, fixtures, plotly} from '../lib/test-utils';
7-
import {connectLayoutToPlot} from '../lib';
3+
import connectLayoutToPlot from '../connectLayoutToPlot';
4+
import {EDITOR_ACTIONS} from '../../constants';
5+
import {Fold, Panel, Section} from '../../components/containers';
6+
import {Numeric} from '../../components/fields';
7+
import {TestEditor, fixtures, plotly} from '../test-utils';
88
import {mount} from 'enzyme';
99

1010
const Layouts = [Panel, Fold, Section].map(connectLayoutToPlot);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react';
2+
import {connectTraceToPlot, connectToContainer, unpackPlotProps} from '..';
3+
import {Numeric} from '../../components/fields';
4+
import {TestEditor, fixtures} from '../test-utils';
5+
import {mount} from 'enzyme';
6+
7+
describe('connectToContainer', () => {
8+
it('connectToContainer accepts supplyPlotProps function', () => {
9+
const onUpdate = jest.fn();
10+
const fixtureProps = fixtures.scatter({layout: {width: 10}});
11+
const ConnectedNumeric = connectTraceToPlot(
12+
connectToContainer(Numeric, {
13+
supplyPlotProps: (props, context) => {
14+
const plotProps = unpackPlotProps(props, context);
15+
plotProps.connectToContainerModifiedPlotProp = true;
16+
return plotProps;
17+
},
18+
})
19+
);
20+
21+
const numeric = mount(
22+
<TestEditor {...{...fixtureProps, onUpdate}}>
23+
<ConnectedNumeric traceIndex={0} label="Opacity" attr="opacity" />
24+
</TestEditor>
25+
).find(Numeric);
26+
27+
expect(numeric.prop('connectToContainerModifiedPlotProp')).toBe(true);
28+
});
29+
});

src/__tests__/connectTraceToPlot-test.js renamed to src/lib/__tests__/connectTraceToPlot-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Numeric} from '../components/fields';
2-
import {Fold, Panel, Section} from '../components/containers';
3-
import NumericInput from '../components/widgets/NumericInputStatefulWrapper';
1+
import NumericInput from '../../components/widgets/NumericInputStatefulWrapper';
42
import React from 'react';
5-
import {EDITOR_ACTIONS} from '../constants';
6-
import {TestEditor, fixtures, plotly} from '../lib/test-utils';
7-
import {connectTraceToPlot} from '../lib';
3+
import connectTraceToPlot from '../connectTraceToPlot';
4+
import {EDITOR_ACTIONS} from '../../constants';
5+
import {Fold, Panel, Section} from '../../components/containers';
6+
import {Numeric} from '../../components/fields';
7+
import {TestEditor, fixtures, plotly} from '../test-utils';
88
import {mount} from 'enzyme';
99

1010
const Traces = [Panel, Fold, Section].map(connectTraceToPlot);

0 commit comments

Comments
 (0)