diff --git a/src/components/widgets/input.js b/src/components/widgets/input.js index eaa2b0b..63bc655 100644 --- a/src/components/widgets/input.js +++ b/src/components/widgets/input.js @@ -21,56 +21,68 @@ ******************************************************************************/ import React, { Component } from 'react'; -import { Form, FormGroup, Col, ControlLabel, FormControl } from 'react-bootstrap'; +import { Form, FormGroup, Col, ControlLabel, FormControl, InputGroup } from 'react-bootstrap'; class WidgetInput extends Component { - static whichValidationStateIs( condition ) { - switch(condition) { - case 'ok': return null; - case 'error': return 'error'; - default: return 'error'; - } - } - constructor(props) { super(props); this.state = { value: '', - validationState: WidgetInput.whichValidationStateIs('ok') + unit: '' }; } - validateInput(e) { - if (e.target.value === '' || e.target.value.endsWith('.')) { - this.setState({ - validationState: WidgetInput.whichValidationStateIs('ok'), - value: e.target.value }); - } else { - var num = Number(e.target.value); - if (Number.isNaN(num)) { - this.setState({ validationState: WidgetInput.whichValidationStateIs('error'), - value: e.target.value }); - } else { - this.setState({ - validationState: WidgetInput.whichValidationStateIs('ok'), - value: num }); - } - } + componentWillReceiveProps(nextProps) { + if (nextProps.simulationModel == null) { + return; + } + + // Update value + if (nextProps.widget.default_value && this.state.value === undefined) { + this.setState({ + value: nextProps.widget.default_value + }); + } + + // Update unit + if (nextProps.widget.simulationModel && nextProps.simulationModel.inputMapping && this.state.unit !== nextProps.simulationModel.inputMapping[nextProps.widget.signal].type) { + this.setState({ + unit: nextProps.simulationModel.inputMapping[nextProps.widget.signal].type + }); + } + } + + valueIsChanging(newValue) { + this.setState({ value: newValue }); + } + + valueChanged(newValue) { + if (this.props.onInputChanged) { + this.props.onInputChanged(newValue); + } + } + + handleKeyPress(e) { + if(e.charCode === 13) { + this.valueChanged(this.state.value) + } } render() { return (