Skip to content

Commit 26ec207

Browse files
move components from shame -> derived. Update index and tests
1 parent 76efee6 commit 26ec207

File tree

10 files changed

+146
-141
lines changed

10 files changed

+146
-141
lines changed

src/DefaultEditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
Flaglist,
1111
Fold,
1212
Info,
13+
LayoutNumericFraction,
14+
LayoutNumericFractionInverse,
1315
Numeric,
1416
Panel,
1517
PanelMenuWrapper,
@@ -24,8 +26,6 @@ import {
2426
import {DEFAULT_FONTS} from './constants';
2527
import {localize, connectAxesToLayout, connectLayoutToPlot} from './lib';
2628

27-
import {LayoutNumericFractionInverse, LayoutNumericFraction} from './shame';
28-
2929
const LayoutPanel = connectLayoutToPlot(Panel);
3030
const AxesFold = connectAxesToLayout(Fold);
3131

src/components/fields/AxesRange.js

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

src/components/fields/CanvasSize.js

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

src/components/fields/__tests__/CanvasSize-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import {connectLayoutToPlot} from '../../../lib';
3-
import {Panel, Fold, Numeric} from '../../';
3+
import {Panel, Fold} from '../../';
44
import {TestEditor, fixtures} from '../../../lib/test-utils';
55
import {mount} from 'enzyme';
6-
import CanvasSize from '../CanvasSize';
6+
import {CanvasSize} from '../';
77

88
describe('CanvasSize', () => {
99
const LayoutPanel = connectLayoutToPlot(Panel);
@@ -18,9 +18,9 @@ describe('CanvasSize', () => {
1818
</Fold>
1919
</LayoutPanel>
2020
</TestEditor>
21-
).find(Numeric);
21+
).find(CanvasSize);
2222

23-
expect(wrapper.length).toBe(0);
23+
expect(wrapper.children().length).toBe(0);
2424
});
2525

2626
it('is visible when autosize is false', () => {
@@ -33,8 +33,8 @@ describe('CanvasSize', () => {
3333
</Fold>
3434
</LayoutPanel>
3535
</TestEditor>
36-
).find(Numeric);
36+
).find(CanvasSize);
3737

38-
expect(wrapper.length).toBe(1);
38+
expect(wrapper.children().length).toBe(1);
3939
});
4040
});

src/components/fields/derived.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import isNumeric from 'fast-isnumeric';
2+
import {UnconnectedNumeric} from './Numeric';
3+
import {
4+
connectLayoutToPlot,
5+
connectToContainer,
6+
getLayoutContext,
7+
unpackPlotProps,
8+
} from '../../lib';
9+
10+
export const CanvasSize = connectToContainer(UnconnectedNumeric, {
11+
modifyPlotProps: (props, context, plotProps) => {
12+
const {fullContainer} = plotProps;
13+
if (plotProps.isVisible && fullContainer && fullContainer.autosize) {
14+
plotProps.isVisible = false;
15+
}
16+
},
17+
});
18+
19+
export const AxesRange = connectToContainer(UnconnectedNumeric, {
20+
modifyPlotProps: (props, context, plotProps) => {
21+
const {fullContainer} = plotProps;
22+
if (plotProps.isVisible && fullContainer && fullContainer.autorange) {
23+
plotProps.isVisible = false;
24+
}
25+
return plotProps;
26+
},
27+
});
28+
29+
class NumericFractionNoArrows extends UnconnectedNumeric {}
30+
NumericFractionNoArrows.propTypes = UnconnectedNumeric.propTypes;
31+
NumericFractionNoArrows.defaultProps = {
32+
showArrows: false,
33+
postfix: '%',
34+
};
35+
36+
// Workaround the issue with nested layouts inside trace component.
37+
// See:
38+
// https://github.com/plotly/react-plotly.js-editor/issues/58#issuecomment-345492794
39+
const supplyLayoutPlotProps = (props, context) => {
40+
return unpackPlotProps(props, {
41+
...context,
42+
...getLayoutContext(context),
43+
});
44+
};
45+
46+
export const LayoutNumericFractionInverse = connectLayoutToPlot(
47+
connectToContainer(NumericFractionNoArrows, {
48+
supplyPlotProps: supplyLayoutPlotProps,
49+
modifyPlotProps: (props, context, plotProps) => {
50+
const {attrMeta, fullValue, updatePlot} = plotProps;
51+
plotProps.fullValue = () => {
52+
let fv = fullValue();
53+
if (isNumeric(fv)) {
54+
fv = Math.round((1 - fv) * 100);
55+
}
56+
return fv;
57+
};
58+
59+
plotProps.updatePlot = v => {
60+
if (isNumeric(v)) {
61+
updatePlot(1 - v / 100);
62+
} else {
63+
updatePlot(v);
64+
}
65+
};
66+
67+
// Also take the inverse of max and min.
68+
if (attrMeta) {
69+
if (isNumeric(attrMeta.min)) {
70+
plotProps.max = (1 - attrMeta.min) * 100;
71+
}
72+
73+
if (isNumeric(attrMeta.max)) {
74+
plotProps.min = (1 - attrMeta.max) * 100;
75+
}
76+
}
77+
},
78+
})
79+
);
80+
81+
export const LayoutNumericFraction = connectLayoutToPlot(
82+
connectToContainer(NumericFractionNoArrows, {
83+
supplyPlotProps: supplyLayoutPlotProps,
84+
modifyPlotProps: (props, context, plotProps) => {
85+
const {attrMeta, fullValue, updatePlot} = plotProps;
86+
plotProps.fullValue = () => {
87+
let fv = fullValue();
88+
if (isNumeric(fv)) {
89+
fv = fv * 100;
90+
}
91+
return fv;
92+
};
93+
94+
plotProps.updatePlot = v => {
95+
if (isNumeric(v)) {
96+
updatePlot(v / 100);
97+
} else {
98+
updatePlot(v);
99+
}
100+
};
101+
102+
if (attrMeta) {
103+
if (isNumeric(attrMeta.max)) {
104+
plotProps.max = attrMeta.max * 100;
105+
}
106+
107+
if (isNumeric(attrMeta.min)) {
108+
plotProps.min = attrMeta.min * 100;
109+
}
110+
}
111+
},
112+
})
113+
);

src/components/fields/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import AxesRange from './AxesRange';
21
import AxesSelector from './AxesSelector';
3-
import CanvasSize from './CanvasSize';
42
import ColorPicker from './Color';
53
import Dropdown from './Dropdown';
64
import Flaglist from './Flaglist';
@@ -10,6 +8,12 @@ import DataSelector from './DataSelector';
108
import Numeric from './Numeric';
119
import SymbolSelector from './SymbolSelector';
1210
import TraceSelector from './TraceSelector';
11+
import {
12+
AxesRange,
13+
CanvasSize,
14+
LayoutNumericFraction,
15+
LayoutNumericFractionInverse,
16+
} from './derived';
1317

1418
export {
1519
AxesRange,
@@ -19,6 +23,8 @@ export {
1923
Dropdown,
2024
Flaglist,
2125
Info,
26+
LayoutNumericFraction,
27+
LayoutNumericFractionInverse,
2228
Radio,
2329
DataSelector,
2430
Numeric,

src/components/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
Dropdown,
77
Flaglist,
88
Info,
9+
LayoutNumericFraction,
10+
LayoutNumericFractionInverse,
911
Radio,
1012
DataSelector,
1113
Numeric,
@@ -34,6 +36,8 @@ export {
3436
Flaglist,
3537
Fold,
3638
Info,
39+
LayoutNumericFraction,
40+
LayoutNumericFractionInverse,
3741
Numeric,
3842
Panel,
3943
PanelMenuWrapper,

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
Fold,
2121
Info,
2222
Layout,
23+
LayoutNumericFraction,
24+
LayoutNumericFractionInverse,
2325
Numeric,
2426
Panel,
2527
PanelMenuWrapper,
@@ -46,6 +48,8 @@ export {
4648
Info,
4749
Hub,
4850
Layout,
51+
LayoutNumericFraction,
52+
LayoutNumericFractionInverse,
4953
Numeric,
5054
Panel,
5155
PanelMenuWrapper,

src/lib/test-utils.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Adapter from 'enzyme-adapter-react-15';
88
configure({adapter: new Adapter()});
99

1010
const fixtures = {
11-
scatter() {
12-
return {
11+
scatter(config) {
12+
return applyConfig(config, {
1313
dataSources: {
1414
x1: [1, 2, 3],
1515
y1: [2, 3, 4],
@@ -18,11 +18,11 @@ const fixtures = {
1818
data: [{type: 'scatter', mode: 'markers', xsrc: 'x1', ysrc: 'y1'}],
1919
layout: {},
2020
},
21-
};
21+
});
2222
},
2323

24-
area() {
25-
return {
24+
area(config) {
25+
return applyConfig(config, {
2626
dataSources: {
2727
x1: [1, 2, 3],
2828
y1: [2, 3, 4],
@@ -39,11 +39,11 @@ const fixtures = {
3939
],
4040
layout: {},
4141
},
42-
};
42+
});
4343
},
4444

45-
pie() {
46-
return {
45+
pie(config) {
46+
return applyConfig(config, {
4747
dataSources: {
4848
x1: [1, 2, 3],
4949
y1: [2, 3, 4],
@@ -54,7 +54,7 @@ const fixtures = {
5454
],
5555
layout: {},
5656
},
57-
};
57+
});
5858
},
5959
};
6060

@@ -75,11 +75,6 @@ function applyConfig(config = {}, {graphDiv: {data, layout}, dataSources}) {
7575
return {dataSources, graphDiv};
7676
}
7777

78-
Object.keys(fixtures).forEach(k => {
79-
const fixtureFunc = fixtures[k];
80-
fixtures[k] = config => applyConfig(config, fixtureFunc());
81-
});
82-
8378
function newGraphDiv() {
8479
const graphDiv = window.document.createElement('div');
8580
graphDiv.id = 'graphDiv';

0 commit comments

Comments
 (0)