0

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>
...

1 Answer 1

1

First: send input index as prop to the input

handleAddInput(e) {
    const inputList = this.state.inputList

    this.setState({
        inputList: inputList.concat(<InputField key={inputList.length}
                                                className="mdl-textfield__input"
                                                type="text"
                                                id="OBR3"
                                                inputIndex={inputList.length} //this one
                                                value={this.props.value}
                                                onChange={this.props.callback}
        />)
    })
    e.preventDefault()
}

Second: Send in call back event.target.value (or just event) and index of input:

<input className="mdl-textfield__input" 
    type="text" id="OBR3" 
    value={this.props.value} 
    onChange={(e) => this.props.callback(this.props.inputIndex, e.target.value)} />

Third: In parent:

handleOBR3OnChange(inputIndex, value) {
    console.log(inputIndex, value) //you know, what input is changed and know the value 
    ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

With these changes I am able to see the state of inputIndex in the child component. However I am not able to get the value, and the state is not hoisted up to the parent component. I may simply not be fully understanding this solution, but so far it looks I am in the same position as before.
I thought handleOBR3OnChange in the parent component, isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.