I'm trying to change values of react controlled inputs when I type in the browser.
I have provided a code example.
The third input is automatically filled in if the first two inputs have a value. But if they are empty how can I allow the user to directly input the value of the third field?
const PriceListRow = React.createClass({
getInitialState(){
return {
flatPrice: "",
}
},
setFlatPrice(a, b){
"use strict";
let self = this,
prod = a * b;
if ( !(_.isNumber(prod)) )
self.setState({flatPrice: ""});
else
self.setState({flatPrice: prod});
},
render(){
let self = this;
return <div>
<input type="number" name={"unitSize"} placeholder="1000 Sq. Ft." id={"unitSize"}
onBlur={ ()=> { self.setFlatPrice($("#bsp").val(), $("#unitSize").val()); }}
/>
<input type="number"
name={"bsp"}
placeholder="4500" id={"bsp"}
onBlur={ ()=> { self.setFlatPrice($("#bsp").val(), $("#unitSize").val()); }}
/>
<input type="text" name={"flatPrice"} placeholder="Size * BSP" id={"flatPrice"} value={self.state.flatPrice} />
</div>
}
});
If I understand you correctly. You want user to edit
flatPrice
even it is automatically set by above 2input
values.Note: Removed
jQuery
dependency completelyThe below snippet will address your need.