|
| 1 | +import Field from './Field'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import React, {Component} from 'react'; |
| 4 | +import {connectToContainer} from 'lib'; |
| 5 | +import RadioBlocks from '../widgets/RadioBlocks'; |
| 6 | +import Numeric from './Numeric'; |
| 7 | +import DataSelector from './DataSelector'; |
| 8 | + |
| 9 | +const getType = value => (Array.isArray(value) ? 'variable' : 'constant'); |
| 10 | + |
| 11 | +class UnconnectedMarkerSize extends Component { |
| 12 | + constructor(props, context) { |
| 13 | + super(props, context); |
| 14 | + |
| 15 | + const type = getType(props.fullValue); |
| 16 | + this.state = { |
| 17 | + type, |
| 18 | + value: { |
| 19 | + constant: type === 'constant' ? props.fullValue : '6', |
| 20 | + variable: type === 'variable' ? props.fullValue : null, |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + this.setType = this.setType.bind(this); |
| 25 | + this.setValue = this.setValue.bind(this); |
| 26 | + } |
| 27 | + |
| 28 | + setType(type) { |
| 29 | + this.setState({type: type}); |
| 30 | + this.props.updatePlot(this.state.value[type]); |
| 31 | + if (type === 'constant') { |
| 32 | + this.context.updateContainer({['marker.sizesrc']: null}); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + setValue(inputValue) { |
| 37 | + const {type} = this.state; |
| 38 | + |
| 39 | + this.setState( |
| 40 | + type === 'constant' |
| 41 | + ? {value: {constant: inputValue}} |
| 42 | + : {value: {variable: inputValue}} |
| 43 | + ); |
| 44 | + this.props.updatePlot(inputValue); |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + const {attr} = this.props; |
| 49 | + const {localize: _} = this.context; |
| 50 | + const options = [ |
| 51 | + {label: _('Constant'), value: 'constant'}, |
| 52 | + {label: _('Variable'), value: 'variable'}, |
| 53 | + ]; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <Field {...this.props} attr={attr}> |
| 58 | + <RadioBlocks |
| 59 | + options={options} |
| 60 | + activeOption={this.state.type} |
| 61 | + onOptionChange={this.setType} |
| 62 | + /> |
| 63 | + {this.state.type === 'constant' ? ( |
| 64 | + <Numeric |
| 65 | + attr="marker.size" |
| 66 | + updatePlot={this.setValue} |
| 67 | + fullValue={this.state.value.constant} |
| 68 | + /> |
| 69 | + ) : ( |
| 70 | + <DataSelector |
| 71 | + attr="marker.size" |
| 72 | + updatePlot={this.setValue} |
| 73 | + fullValue={this.state.value.variable} |
| 74 | + /> |
| 75 | + )} |
| 76 | + </Field> |
| 77 | + </div> |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +UnconnectedMarkerSize.propTypes = { |
| 83 | + fullValue: PropTypes.any, |
| 84 | + updatePlot: PropTypes.func, |
| 85 | + ...Field.propTypes, |
| 86 | +}; |
| 87 | + |
| 88 | +UnconnectedMarkerSize.contextTypes = { |
| 89 | + localize: PropTypes.func, |
| 90 | + updateContainer: PropTypes.func, |
| 91 | +}; |
| 92 | + |
| 93 | +export default connectToContainer(UnconnectedMarkerSize); |
0 commit comments