|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import {connectToContainer} from 'lib'; |
| 4 | +import Field from './Field'; |
| 5 | +import Numeric from './Numeric'; |
| 6 | +import Radio from './Radio'; |
| 7 | + |
| 8 | +class UnconnectedCanvasSize extends Component { |
| 9 | + constructor(props, context) { |
| 10 | + super(props, context); |
| 11 | + |
| 12 | + this.state = { |
| 13 | + auto: props.container.autosize, |
| 14 | + width: props.container.width || props.fullContainer.width || '', |
| 15 | + height: props.container.height || props.fullContainer.height || '', |
| 16 | + }; |
| 17 | + |
| 18 | + this.setAuto = this.setAuto.bind(this); |
| 19 | + this.setWidth = this.setWidth.bind(this); |
| 20 | + this.setHeight = this.setHeight.bind(this); |
| 21 | + } |
| 22 | + |
| 23 | + setAuto(auto) { |
| 24 | + this.setState({auto: auto}); |
| 25 | + this.props.updatePlot(auto); |
| 26 | + this.context.updateContainer( |
| 27 | + auto |
| 28 | + ? {width: '', height: ''} |
| 29 | + : {width: this.state.width, height: this.state.height} |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + setWidth(inputValue) { |
| 34 | + this.setState({width: inputValue}); |
| 35 | + this.props.updateContainer({width: inputValue}); |
| 36 | + } |
| 37 | + |
| 38 | + setHeight(inputValue) { |
| 39 | + this.setState({height: inputValue}); |
| 40 | + this.props.updateContainer({height: inputValue}); |
| 41 | + } |
| 42 | + |
| 43 | + render() { |
| 44 | + const {attr} = this.props; |
| 45 | + const {localize: _} = this.context; |
| 46 | + const {auto, width, height} = this.state; |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <Radio |
| 51 | + attr={attr} |
| 52 | + label={_('Size')} |
| 53 | + options={[ |
| 54 | + {label: _('Auto'), value: true}, |
| 55 | + {label: _('Custom'), value: false}, |
| 56 | + ]} |
| 57 | + fullValue={auto} |
| 58 | + updatePlot={this.setAuto} |
| 59 | + /> |
| 60 | + {auto ? ( |
| 61 | + '' |
| 62 | + ) : ( |
| 63 | + <div> |
| 64 | + <Numeric |
| 65 | + label={_('Fixed Width')} |
| 66 | + suppressMultiValuedMessage |
| 67 | + attr="width" |
| 68 | + updatePlot={this.setWidth} |
| 69 | + fullValue={width} |
| 70 | + units="px" |
| 71 | + /> |
| 72 | + <Numeric |
| 73 | + label={_('Fixed height')} |
| 74 | + suppressMultiValuedMessage |
| 75 | + attr="height" |
| 76 | + updatePlot={this.setHeight} |
| 77 | + fullValue={height} |
| 78 | + units="px" |
| 79 | + /> |
| 80 | + </div> |
| 81 | + )} |
| 82 | + </div> |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +UnconnectedCanvasSize.propTypes = { |
| 88 | + fullValue: PropTypes.any, |
| 89 | + updatePlot: PropTypes.func, |
| 90 | + ...Field.propTypes, |
| 91 | +}; |
| 92 | + |
| 93 | +UnconnectedCanvasSize.contextTypes = { |
| 94 | + localize: PropTypes.func, |
| 95 | + updateContainer: PropTypes.func, |
| 96 | +}; |
| 97 | + |
| 98 | +export default connectToContainer(UnconnectedCanvasSize); |
0 commit comments