|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import {connectToContainer} from 'lib'; |
| 4 | +import Field from './Field'; |
| 5 | +import RadioBlocks from '../widgets/RadioBlocks'; |
| 6 | +import NumericInput from '../widgets/NumericInput'; |
| 7 | + |
| 8 | +export class UnconnectedHoverLabelNameLength extends Component { |
| 9 | + constructor(props) { |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + currentOption: this.getCurrentOption(props), |
| 13 | + }; |
| 14 | + this.onOptionChange = this.onOptionChange.bind(this); |
| 15 | + } |
| 16 | + |
| 17 | + getCurrentOption(props) { |
| 18 | + return props.fullValue > 0 ? 'clip' : props.fullValue === 0 ? 'hide' : 'no-clip'; |
| 19 | + } |
| 20 | + |
| 21 | + componentWillReceiveProps(nextProps) { |
| 22 | + if (nextProps.fullValue !== this.props.fullValue) { |
| 23 | + this.setState({ |
| 24 | + currentOption: this.getCurrentOption(nextProps), |
| 25 | + }); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + onOptionChange(option) { |
| 30 | + if (this.state.currentOption !== 'clip' && option === 'clip') { |
| 31 | + // this allows us to go back to plotly.js default if we've |
| 32 | + // clicked away from the 'clip' option. |
| 33 | + this.props.updatePlot(15); //eslint-disable-line |
| 34 | + return; |
| 35 | + } |
| 36 | + if (option === 'no-clip') { |
| 37 | + this.props.updatePlot(-1); |
| 38 | + return; |
| 39 | + } |
| 40 | + if (option === 'hide') { |
| 41 | + this.props.updatePlot(0); |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + render() { |
| 47 | + const _ = this.context.localize; |
| 48 | + |
| 49 | + return ( |
| 50 | + <Field {...this.props}> |
| 51 | + <RadioBlocks |
| 52 | + activeOption={this.state.currentOption} |
| 53 | + options={[ |
| 54 | + {label: _('Clip To'), value: 'clip'}, |
| 55 | + {label: _('No Clip'), value: 'no-clip'}, |
| 56 | + {label: _('Hide'), value: 'hide'}, |
| 57 | + ]} |
| 58 | + onOptionChange={this.onOptionChange} |
| 59 | + /> |
| 60 | + <div style={{height: '10px', width: '100%'}} /> |
| 61 | + {this.state.currentOption === 'clip' ? ( |
| 62 | + <NumericInput |
| 63 | + value={this.props.fullValue} |
| 64 | + onChange={this.props.updatePlot} |
| 65 | + onUpdate={this.props.updatePlot} |
| 66 | + units="px" |
| 67 | + /> |
| 68 | + ) : null} |
| 69 | + </Field> |
| 70 | + ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +UnconnectedHoverLabelNameLength.propTypes = { |
| 75 | + fullValue: PropTypes.number, |
| 76 | + updatePlot: PropTypes.func, |
| 77 | + ...Field.propTypes, |
| 78 | +}; |
| 79 | + |
| 80 | +UnconnectedHoverLabelNameLength.contextTypes = { |
| 81 | + localize: PropTypes.func, |
| 82 | +}; |
| 83 | + |
| 84 | +export default connectToContainer(UnconnectedHoverLabelNameLength, { |
| 85 | + modifyPlotProps: (props, context, plotProps) => { |
| 86 | + const {container} = plotProps; |
| 87 | + plotProps.isVisible = |
| 88 | + (container.hoverinfo && container.hoverinfo.includes('name')) || |
| 89 | + (container.hovertemplate || container.hovertemplate === ' '); |
| 90 | + }, |
| 91 | +}); |
0 commit comments