|
1 | | -import ColorscalePicker, {Colorscale} from 'react-colorscales'; |
| 1 | +import ColorscalePicker, { |
| 2 | + Colorscale, |
| 3 | + COLOR_PICKER_CONSTANTS, |
| 4 | +} from 'react-colorscales'; |
| 5 | +import Dropdown from './Dropdown'; |
| 6 | +import Info from '../fields/Info'; |
2 | 7 | import PropTypes from 'prop-types'; |
3 | | -import React, {Component} from 'react'; |
| 8 | +import React, {Component, Fragment} from 'react'; |
4 | 9 |
|
5 | 10 | class Scale extends Component { |
6 | | - constructor(props) { |
7 | | - super(props); |
| 11 | + constructor() { |
| 12 | + super(); |
| 13 | + |
8 | 14 | this.state = { |
| 15 | + selectedColorscaleType: 'sequential', |
9 | 16 | showColorscalePicker: false, |
10 | 17 | }; |
11 | | - this.toggle = this.toggle.bind(this); |
| 18 | + |
| 19 | + this.onChange = this.onChange.bind(this); |
| 20 | + this.onClick = this.onClick.bind(this); |
12 | 21 | } |
13 | 22 |
|
14 | | - toggle() { |
| 23 | + onClick() { |
15 | 24 | this.setState({ |
16 | 25 | showColorscalePicker: !this.state.showColorscalePicker, |
17 | 26 | }); |
18 | 27 | } |
19 | 28 |
|
| 29 | + onChange(selectedColorscaleType) { |
| 30 | + this.setState({selectedColorscaleType}); |
| 31 | + } |
| 32 | + |
20 | 33 | render() { |
21 | | - const {selected, onColorscaleChange} = this.props; |
| 34 | + const {onColorscaleChange, selected} = this.props; |
| 35 | + const {selectedColorscaleType, showColorscalePicker} = this.state; |
| 36 | + const description = |
| 37 | + COLOR_PICKER_CONSTANTS.COLORSCALE_DESCRIPTIONS[selectedColorscaleType]; |
| 38 | + const colorscaleOptions = COLOR_PICKER_CONSTANTS.COLORSCALE_TYPES.map( |
| 39 | + type => ({ |
| 40 | + label: type + ' scales', |
| 41 | + value: type, |
| 42 | + }) |
| 43 | + ); |
| 44 | + const _ = this.context.localize; |
| 45 | + |
22 | 46 | return ( |
23 | | - <div> |
24 | | - <Colorscale colorscale={selected} onClick={this.toggle} /> |
25 | | - {this.state.showColorscalePicker ? ( |
26 | | - <ColorscalePicker |
27 | | - onChange={onColorscaleChange} |
28 | | - colorscale={selected} |
29 | | - /> |
| 47 | + <Fragment> |
| 48 | + <Colorscale colorscale={selected} onClick={this.onClick} /> |
| 49 | + |
| 50 | + {showColorscalePicker ? ( |
| 51 | + <div className="customPickerContainer"> |
| 52 | + <Dropdown |
| 53 | + options={colorscaleOptions} |
| 54 | + value={selectedColorscaleType} |
| 55 | + onChange={this.onChange} |
| 56 | + clearable={false} |
| 57 | + searchable={false} |
| 58 | + placeholder={_('Select a Colorscale Type')} |
| 59 | + /> |
| 60 | + {description ? ( |
| 61 | + <Fragment> |
| 62 | + <ColorscalePicker |
| 63 | + onChange={onColorscaleChange} |
| 64 | + colorscale={selected} |
| 65 | + width={215} |
| 66 | + colorscaleType={this.state.selectedColorscaleType} |
| 67 | + onColorscaleTypeChange={this.onColorscaleTypeChange} |
| 68 | + disableSwatchControls |
| 69 | + scaleLength={7} |
| 70 | + /> |
| 71 | + <Info>{description}</Info> |
| 72 | + </Fragment> |
| 73 | + ) : null} |
| 74 | + </div> |
30 | 75 | ) : null} |
31 | | - </div> |
| 76 | + </Fragment> |
32 | 77 | ); |
33 | 78 | } |
34 | 79 | } |
35 | 80 |
|
36 | 81 | Scale.propTypes = { |
37 | 82 | onColorscaleChange: PropTypes.func, |
38 | 83 | selected: PropTypes.array, |
| 84 | + label: PropTypes.string, |
| 85 | +}; |
| 86 | + |
| 87 | +Scale.contextTypes = { |
| 88 | + localize: PropTypes.func, |
| 89 | +}; |
| 90 | + |
| 91 | +Scale.contextTypes = { |
| 92 | + localize: PropTypes.func, |
39 | 93 | }; |
40 | 94 |
|
41 | 95 | export default Scale; |
0 commit comments