I am able to add input fields and get the state of said fields. I am also adding a delete button along with each new field but have not been able to get the delete function to work.
I am creating the delete button here along with the input field.
class InputField extends React.Component {
render() {
const { name, onChange } = this.props;
return (
<div className="row">
<div className="input-field col s12">
<label htmlFor={name}>OBR3</label>
<input id={name}
type="text"
className=""
value={this.props.inputValues}
onChange={onChange} />
<button type='delete' value='Delete'
onClick={this.props.handleRemove}>
<i className="material-icons">remove</i>
</button>
</div>
</div>
)
}
}
export class OBR3 extends React.Component {
...
this.state = {
inputValues: {},
inputs: []
...
handleRemove (item, event) {
const newState = this.state.newState
if (newState.indexOf(item) > 1) {
newState.splice(newState.indexOf(item), 1)
}
this.setState({ inputs: newState })
console.log(newState.indexOf(item))
}
handleChange (name, event) {
let inputValues = this.state.inputValues
inputValues[name] = event.target.value
this.setState({ inputValues })
}
This function actually adds the input field and let's me .bind(this) . Not sure but I think If I was able to add the delete button here It might solve my issue?
handleAddInput() {
const name = `OBR3-${(this.state.inputs).length}`;
let inputBox = <InputField key={this.state.inputs.length}
name={name}
onChange={this.handleChange.bind(this, name)} />
const inputs = this.state.inputs
inputs.push( inputBox );
this.setState({ inputs });
}
And finally this is how I am now rendering everything.
render() {
return (
<div>
<p id='OBR3'>OBR3</p>
<button type='submit' value='Submit'
onClick={this.handleAddInput.bind(this)}>
<i className="material-icons">add</i>
</button>
{this.state.inputs}
</div>
...