import React from 'react'; import PropTypes from 'prop-types'; import { Col, Row, FormGroup, FormControl } from 'react-bootstrap'; import ControlHeader from '../ControlHeader'; const propTypes = { name: PropTypes.string.isRequired, label: PropTypes.string, description: PropTypes.string, onChange: PropTypes.func, value: PropTypes.array, }; const defaultProps = { label: null, description: null, 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('`Min` value should be numeric or empty'); } if (mm[1] && isNaN(mm[1])) { errors.push('`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 (
); } } BoundsControl.propTypes = propTypes; BoundsControl.defaultProps = defaultProps;