|
| 1 | +import Dropdown from './Dropdown'; |
| 2 | +import RadioBlocks from '../widgets/RadioBlocks'; |
| 3 | +import Field from './Field'; |
| 4 | +import PropTypes from 'prop-types'; |
| 5 | +import React, {Component, Fragment} from 'react'; |
| 6 | +import {connectToContainer} from 'lib'; |
| 7 | +import Info from './Info'; |
| 8 | +import DataSelector from './DataSelector'; |
| 9 | + |
| 10 | +export class UnconnectedTextPosition extends Component { |
| 11 | + constructor(props) { |
| 12 | + super(props); |
| 13 | + this.state = { |
| 14 | + posType: typeof props.fullValue === 'string' ? 'simple' : 'custom', |
| 15 | + }; |
| 16 | + } |
| 17 | + |
| 18 | + render() { |
| 19 | + const _ = this.context.localize; |
| 20 | + const radioOptions = [ |
| 21 | + {label: _('All'), value: 'simple'}, |
| 22 | + {label: _('Custom'), value: 'custom'}, |
| 23 | + ]; |
| 24 | + const control = |
| 25 | + this.state.posType === 'simple' ? ( |
| 26 | + <Fragment> |
| 27 | + <Info> |
| 28 | + {_( |
| 29 | + 'This will position all text values on the plot according to the selected position.' |
| 30 | + )} |
| 31 | + </Info> |
| 32 | + <Dropdown options={this.props.options} attr="textposition" /> |
| 33 | + </Fragment> |
| 34 | + ) : ( |
| 35 | + <Fragment> |
| 36 | + <Info> |
| 37 | + <div> |
| 38 | + {_( |
| 39 | + 'This will position text values individually, according to the provided data positions array. ' |
| 40 | + )} |
| 41 | + </div> |
| 42 | + </Info> |
| 43 | + <DataSelector attr="textposition" /> |
| 44 | + <Info> |
| 45 | + <div>{_('("Top", "Middle", "Bottom") + ("Left", "Center", "Right")')}</div> |
| 46 | + </Info> |
| 47 | + </Fragment> |
| 48 | + ); |
| 49 | + |
| 50 | + return ( |
| 51 | + <Field {...this.props}> |
| 52 | + <RadioBlocks |
| 53 | + options={radioOptions} |
| 54 | + activeOption={this.state.posType} |
| 55 | + onOptionChange={value => { |
| 56 | + this.setState({posType: value}); |
| 57 | + if (value === 'simple') { |
| 58 | + this.props.updatePlot('middle center'); |
| 59 | + } else { |
| 60 | + this.props.updateContainer({textpositionsrc: null}); |
| 61 | + } |
| 62 | + }} |
| 63 | + /> |
| 64 | + {control} |
| 65 | + </Field> |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +UnconnectedTextPosition.propTypes = { |
| 71 | + ...Field.propTypes, |
| 72 | + options: PropTypes.array, |
| 73 | + fullValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string]), |
| 74 | +}; |
| 75 | + |
| 76 | +UnconnectedTextPosition.contextTypes = { |
| 77 | + localize: PropTypes.func, |
| 78 | +}; |
| 79 | + |
| 80 | +export default connectToContainer(UnconnectedTextPosition, { |
| 81 | + modifyPlotProps: (props, context, plotProps) => { |
| 82 | + const {localize: _} = context; |
| 83 | + let options = [ |
| 84 | + {label: _('Top Left'), value: 'top left'}, |
| 85 | + {label: _('Top Center'), value: 'top center'}, |
| 86 | + {label: _('Top Right'), value: 'top right'}, |
| 87 | + {label: _('Middle Left'), value: 'middle left'}, |
| 88 | + {label: _('Middle Center'), value: 'middle center'}, |
| 89 | + {label: _('Middle Right'), value: 'middle right'}, |
| 90 | + {label: _('Bottom Left'), value: 'bottom left'}, |
| 91 | + {label: _('Bottom Center'), value: 'bottom center'}, |
| 92 | + {label: _('Bottom Right'), value: 'bottom right'}, |
| 93 | + ]; |
| 94 | + if (context.container.type === 'pie' || context.container.type === 'bar') { |
| 95 | + options = [ |
| 96 | + {label: _('Inside'), value: 'inside'}, |
| 97 | + {label: _('Outside'), value: 'outside'}, |
| 98 | + {label: _('Auto'), value: 'auto'}, |
| 99 | + {label: _('None'), value: 'none'}, |
| 100 | + ]; |
| 101 | + } |
| 102 | + plotProps.options = options; |
| 103 | + plotProps.clearable = false; |
| 104 | + }, |
| 105 | +}); |
0 commit comments