0

I'm using the React-Select library to generate select options in react as you will see in the screenshot provided. The concept is simple, I have a button to add a new row that has a select options to input clients, the other input to insert date. Upon clicking on the plus button, it will add a new row to be filled with the exact info like i've explained. Please see the screenshot. enter image description here

I have a problem filling up my state when the data changes for each row, so each object in the array represents a row, the following is my state

this.state = {
    customClients: [
     {
      date: null,
      clients: [{ value: "", label: "" }]
    }
    ]
  }

and this is a snippet of my render method

{this.state.customClients.map((client, i) => {
      let cli_id = `cli-${i}`;
      let date_id = `date-${i}`;
      return (
        <div className="row" key={i} id={i}>
          <div className="col-md-4">
            <p className="mb-1 mt-3 font-weight-bold text-muted">Clients</p>
            <Select
              name="clients"
              inputId={cli_id}
              isMulti
              value={client.clients}
              onChange={this.handleCustomClients}
              options={clients || []}
              className="basic-multi-select"
              classNamePrefix="select"
            />
          </div>

          <div className="col-md-8">
            <div className="form-group">
              <p className="mb-1 mt-3 font-weight-bold text-muted">Date</p>
              <DatePicker
                name="date"
                id={date_id}
                className="form-control"
                selected={client.date}
                onChange={this.handleCustomClients}
                minDate={new Date()}
              />
            </div>
          </div>
        </div>
      );
    })}
2
  • what problem exactly you are facing? Commented Dec 31, 2018 at 14:03
  • What's in handleCustomClients? Commented Dec 31, 2018 at 14:05

1 Answer 1

1

Since you are changing the object which recites inside array you need to pass the index of the record you want to edit. first let's update the onChange event and pass the index

<Select
        name="clients"
        inputId={cli_id}
        isMulti
        value={client.clients}
        onChange={e => this.handleCustomClients(e, i)}
        options={clients || []}
        className="basic-multi-select"
        classNamePrefix="select"
/>

And the method below

handleCustomClients(e, i) {
    this.setState(prevState => {
      return (prevState.customClients[i].clients = e);
    });
}

code sandbox https://codesandbox.io/s/rm1k8oor7o

Cheers!

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

1 Comment

This appears to no longer work with react-select version 5? the onChange handler only returns action and option, no event object that I can find.

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.