all files / src/explore/components/controls/ BoundsControl.jsx

97.06% Statements 33/34
75% Branches 12/16
83.33% Functions 5/6
96.97% Lines 32/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                                                                                                        
import React from 'react';
import PropTypes from 'prop-types';
import { Col, Row, FormGroup, FormControl } from 'react-bootstrap';
import ControlHeader from '../ControlHeader';
import { t } from '../../../locales';
 
const propTypes = {
  onChange: PropTypes.func,
  value: PropTypes.array,
};
 
const defaultProps = {
  onChange: () => {},
  value: [null, null],
};
 
export default class BoundsControl extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      minMax: [
        props.value[0] === null ? '' : props.value[0],
        props.value[1] === null ? '' : props.value[1],
      ],
    };
    this.onChange = this.onChange.bind(this);
    this.onMinChange = this.onMinChange.bind(this);
    this.onMaxChange = this.onMaxChange.bind(this);
  }
  onMinChange(event) {
    this.setState({
      minMax: [
        event.target.value,
        this.state.minMax[1],
      ],
    }, this.onChange);
  }
  onMaxChange(event) {
    this.setState({
      minMax: [
        this.state.minMax[0],
        event.target.value,
      ],
    }, this.onChange);
  }
  onChange() {
    const mm = this.state.minMax;
    const errors = [];
    if (mm[0] && isNaN(mm[0])) {
      errors.push(t('`Min` value should be numeric or empty'));
    }
    if (Imm[1] && isNaN(mm[1])) {
      errors.push(t('`Max` value should be numeric or empty'));
    }
    if (errors.length === 0) {
      this.props.onChange([parseFloat(mm[0]), parseFloat(mm[1])], errors);
    } else {
      this.props.onChange([null, null], errors);
    }
  }
  render() {
    return (
      <div>
        <ControlHeader {...this.props} />
        <FormGroup bsSize="small">
          <Row>
            <Col xs={6}>
              <FormControl
                type="text"
                placeholder={t('Min')}
                onChange={this.onMinChange}
                value={this.state.minMax[0]}
              />
            </Col>
            <Col xs={6}>
              <FormControl
                type="text"
                placeholder={t('Max')}
                onChange={this.onMaxChange}
                value={this.state.minMax[1]}
              />
            </Col>
          </Row>
        </FormGroup>
      </div>
    );
  }
}
 
BoundsControl.propTypes = propTypes;
BoundsControl.defaultProps = defaultProps;