|
| 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 DataSelector from './DataSelector'; |
| 7 | +import MultiColorPicker from './MultiColorPicker'; |
| 8 | +import {MULTI_VALUED, COLORS} from 'lib/constants'; |
| 9 | + |
| 10 | +class UnconnectedColorArrayPicker extends Component { |
| 11 | + constructor(props, context) { |
| 12 | + super(props, context); |
| 13 | + |
| 14 | + const {fullContainer} = props; |
| 15 | + this.rootAttr = props.attr.split('.')[0]; |
| 16 | + |
| 17 | + let type = null; |
| 18 | + if ( |
| 19 | + !fullContainer[this.rootAttr] || |
| 20 | + (fullContainer[this.rootAttr] && !Array.isArray(fullContainer[this.rootAttr].color)) |
| 21 | + ) { |
| 22 | + type = 'constant'; |
| 23 | + } else if (fullContainer[this.rootAttr] && Array.isArray(fullContainer[this.rootAttr].color)) { |
| 24 | + type = 'variable'; |
| 25 | + } |
| 26 | + |
| 27 | + this.state = { |
| 28 | + type, |
| 29 | + value: { |
| 30 | + constant: type === 'constant' ? props.fullValue : COLORS.mutedBlue, |
| 31 | + variable: type === 'variable' ? props.fullValue : null, |
| 32 | + }, |
| 33 | + selectedConstantColorOption: type === 'constant' && props.multiValued ? 'multiple' : 'single', |
| 34 | + }; |
| 35 | + |
| 36 | + this.setType = this.setType.bind(this); |
| 37 | + this.setValue = this.setValue.bind(this); |
| 38 | + this.setColor = this.setColor.bind(this); |
| 39 | + this.setColorScale = this.setColorScale.bind(this); |
| 40 | + this.onConstantColorOptionChange = this.onConstantColorOptionChange.bind(this); |
| 41 | + } |
| 42 | + |
| 43 | + setType(type) { |
| 44 | + this.setState({type}); |
| 45 | + this.context.updateContainer( |
| 46 | + type === 'constant' |
| 47 | + ? {[this.rootAttr + '.color']: this.state.value[type], [this.rootAttr + '.colorsrc']: null} |
| 48 | + : {[this.rootAttr + '.color']: this.state.value[type], [this.rootAttr + '.colorsrc']: null} |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + setValue(inputValue) { |
| 53 | + const {type} = this.state; |
| 54 | + this.setState( |
| 55 | + type === 'constant' |
| 56 | + ? {value: {constant: inputValue, variable: this.state.value[type]}} |
| 57 | + : {value: {variable: inputValue, constant: this.state.value[type]}} |
| 58 | + ); |
| 59 | + this.props.updatePlot(inputValue); |
| 60 | + } |
| 61 | + |
| 62 | + setColor(inputValue) { |
| 63 | + const {type} = this.state; |
| 64 | + |
| 65 | + this.setState( |
| 66 | + type === 'constant' |
| 67 | + ? {value: {constant: inputValue, variable: this.state.value.variable}} |
| 68 | + : {value: {variable: inputValue, constant: this.state.value.constant}} |
| 69 | + ); |
| 70 | + this.props.updatePlot(inputValue); |
| 71 | + } |
| 72 | + |
| 73 | + setColorScale(inputValue) { |
| 74 | + this.setState({colorscale: inputValue}); |
| 75 | + this.context.updateContainer({[this.rootAttr + '.colorscale']: inputValue}); |
| 76 | + } |
| 77 | + |
| 78 | + onConstantColorOptionChange(value) { |
| 79 | + this.setState({ |
| 80 | + selectedConstantColorOption: value, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + render() { |
| 85 | + const {attr, fullValue} = this.props; |
| 86 | + const {localize: _} = this.context; |
| 87 | + const {type} = this.state; |
| 88 | + const options = [ |
| 89 | + {label: _('Constant'), value: 'constant'}, |
| 90 | + {label: _('Variable'), value: 'variable'}, |
| 91 | + ]; |
| 92 | + const multiValued = |
| 93 | + this.props.multiValued || (Array.isArray(fullValue) && fullValue.includes(MULTI_VALUED)); |
| 94 | + |
| 95 | + return ( |
| 96 | + <Field {...this.props} multiValued={multiValued} attr={attr}> |
| 97 | + <RadioBlocks options={options} activeOption={type} onOptionChange={this.setType} /> |
| 98 | + {type === 'constant' ? ( |
| 99 | + <MultiColorPicker |
| 100 | + attr={this.rootAttr + '.color'} |
| 101 | + multiColorMessage={_( |
| 102 | + 'Each trace will be colored according to the selected colorscale.' |
| 103 | + )} |
| 104 | + singleColorMessage={_('All traces will be colored in the the same color.')} |
| 105 | + setColor={this.setColor} |
| 106 | + setColorScale={this.setColorScale} |
| 107 | + onConstantColorOptionChange={this.onConstantColorOptionChange} |
| 108 | + parentSelectedConstantColorOption={this.state.selectedConstantColorOption} |
| 109 | + /> |
| 110 | + ) : multiValued ? null : ( |
| 111 | + <DataSelector |
| 112 | + suppressMultiValuedMessage |
| 113 | + attr={this.rootAttr + '.color'} |
| 114 | + updatePlot={this.setValue} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </Field> |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +UnconnectedColorArrayPicker.propTypes = { |
| 123 | + fullValue: PropTypes.any, |
| 124 | + updatePlot: PropTypes.func, |
| 125 | + ...Field.propTypes, |
| 126 | +}; |
| 127 | + |
| 128 | +UnconnectedColorArrayPicker.contextTypes = { |
| 129 | + localize: PropTypes.func, |
| 130 | + updateContainer: PropTypes.func, |
| 131 | +}; |
| 132 | + |
| 133 | +export default connectToContainer(UnconnectedColorArrayPicker); |
0 commit comments