0

I have this form invoice registration created in reactJs.

form invoice registration

The Form should handle input array (marked with red outline). whenever onChange event occured on the input. all the input is receiving the same value. How to prooperly handle this ? I intended to use this input value to generated data like this below :

[
    {
        invoice_number:102947303364,
        invoice_registration_no : 1235
    },
    {
        invoice_number:102947109177,
        invoice_registration_no : 6578
    },
]

Thanks in advance for the help.

1
  • Its look like you are binding the referance and not value Commented Aug 10, 2017 at 4:58

1 Answer 1

5

I do not know which is the primary key in DB, so I assumed invoice_number is the primary key.

class CustomList extends React.Component {
  constructor(props){
    super(props);
    this.handleInputChange = this.handleInputChange.bind(this);
    this.renderList = this.renderList.bind(this);
    
    this.state = {
      data: [
          {
              invoice_number:102947303364,
              invoice_registration_no : 1235,
              some_value: '',
          }, {
              invoice_number:102947109177,
              invoice_registration_no : 6578,
              some_value: '',
          },
      ],
    }
  }
  
  handleInputChange(e){
    const {id, value} = e.target;
    let {data} = this.state;
    const targetIndex = data.findIndex(datum => {
      return datum.invoice_number == id;
    });

    if(targetIndex !== -1){
      data[targetIndex].some_value = value;
      this.setState({data});
    }
  }
  
  renderList() {
      const {data} = this.state;
      let content = data.map(datum => {
        return (
          <li id={datum.invoice_number}>
            <p>{datum.invoice_number}</p>
            <input type="text" id={datum.invoice_number} value={datum.some_value}
              onChange={this.handleInputChange}/>
          </li>
        )
      });
      return (
        <ul>
          {content}
        </ul>
      )
  }
  
  render() {
    const myList = this.renderList();
    return (
      <div>
        {myList}
      </div>
    );
  }
}
ReactDOM.render(
  <CustomList/>,
  document.getElementById('root')
)
<html>
  <body>
    <div id="root"></div>
  </body>
</html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

what you need to see is handleInputChange method binds to CustomList class and renderList.

I just set id property to invoice_number at each input and find changed input's index using Array.prototype.findIndex.

Sign up to request clarification or add additional context in comments.

5 Comments

You could also declare handleInputChange like this: handleInputChange = () => {...}. Then you don't need to bind them in this ugly fashion :D
@Nocebo In fact, I usually use ::this.method which is ES6 grammer to bind method to class. However Stack Overflow doesn't support ES6, so I habitually wrote like this. Thank you for telling me!
Oh I see. Yeah just trying to help ;) Have a nice day :)
@Nocebo Thank you again for your advice :)
@HyeonJunOh Thanks. Can you please tell how to highlight particular input box border and render small error text when certain required input are left empty when user clicks on Submit button ? I know how to do it for few input element on screen but how to do it for array of objects.

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.