forked from plotly/react-chart-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAxesSelector.js
More file actions
77 lines (67 loc) · 2.12 KB
/
AxesSelector.js
File metadata and controls
77 lines (67 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Field from './Field';
import PropTypes from 'prop-types';
import Dropdown from '../widgets/Dropdown';
import RadioBlocks from '../widgets/RadioBlocks';
import React, {Component} from 'react';
import {EditorControlsContext} from '../../context';
class AxesSelector extends Component {
constructor(props, context) {
super(props, context);
const {localize: _} = context;
if (!props.context.axesTargetHandler) {
throw new Error(_('AxesSelector must be nested within a connectAxesToPlot component'));
}
}
render() {
const {localize: _} = this.context;
const {axesTargetHandler, axesTarget, fullLayout} = this.props.context;
const {axesOptions} = this.props;
const maxCharsThatFitInRadio = 27;
const maxOptions = axesOptions.length > 4; // eslint-disable-line
const multipleSublots =
fullLayout &&
fullLayout._subplots &&
Object.values(fullLayout._subplots).some(s => s.length > 1);
const options = multipleSublots
? axesOptions.map(option =>
option.value === 'allaxes'
? option
: {
label: option.title,
value: option.value,
}
)
: axesOptions;
const totalCharsInOptions =
(options && options.map(o => o.label).reduce((acc, o) => acc + o.length, 0)) || 0;
return maxOptions || totalCharsInOptions >= maxCharsThatFitInRadio ? (
<Field {...this.props} label={_('Axis to Style')}>
<Dropdown
options={options}
value={axesTarget}
onChange={axesTargetHandler}
clearable={false}
/>
</Field>
) : (
<Field {...this.props} center>
<RadioBlocks
options={options}
activeOption={axesTarget}
onOptionChange={axesTargetHandler}
/>
</Field>
);
}
}
AxesSelector.requireContext = {
axesTargetHandler: PropTypes.func,
axesTarget: PropTypes.string,
fullLayout: PropTypes.object,
};
AxesSelector.contextType = EditorControlsContext;
AxesSelector.propTypes = {
axesOptions: PropTypes.array,
context: PropTypes.any,
};
export default AxesSelector;