I have a Modal which contains controllers (Input, DropDown, Textarea, ...) built dynamically.
I'm trying to achieve a behavior for the Input controller onChange method, to get the value updated inside the input once the user type something inside.
(I tried with the simple html input and with some other libraries Input like ant design - but same behavior).
For that purpose, I sat a name for each input controller, and sent the event to onChange, then used setState to set a state variable with the name of the controller with it's value:
else if (element.Type === TEXT) {
const inputName = element.Name.toLowerCase().replace(" ", "");
this.setState({ [inputName]: element.Value }, () =>
elements.push(
<td className="view-cell" align="left">
{element.Name}<br />
<Input key={element.Id}
name={inputName}
style={{ width: '100%' }}
// label={element.Name}
defaultValue={element.Value}
value={this.state[inputName]}
disabled={!element.Editable}
// size={element.Width}
onChange={(e) => {
this.onInputValueChange(e)
this.onInputChange(element.Id, element.Name, e.target.value)
}}
/>
</td>
))
}
onInputValueChange(e) {
const name = e.target.name
this.setState({
[e.target.name]: e.target.value
}, () => {
console.log(this.state[name]);
});
}
The problem is that the change is not reflected on the screen.
What can be the reason? is it the fact that the setState is an async action?
To reproduce, you can use this sandbox (type something inside text boxes)