tbleckert / react-select-search

⚡️ Lightweight select component for React
https://react-select-search.com
MIT License
676 stars 147 forks source link

Setting the state onChange with getOptions() #81

Closed sm-adil closed 4 years ago

sm-adil commented 4 years ago

Hey @tbleckert, With getOptions prop once we fetch the list and select an option, how are we supposed to update some (eg: drink) state with the onChange method. Cause whenever I try to update a state with onChange event the input field doesn't get updated.

Here's what I'm trying to achieve: https://codesandbox.io/s/eloquent-cherry-u3zm6

<SelectSearch
  options={[]}
  getOptions={(query) => {
    return new Promise((resolve, reject) => {
      fetch(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${query}`)
        .then(response => response.json())
        .then(({ drinks }) => {
            resolve(drinks.map(({ idDrink, strDrink }) => ({ value: idDrink, name: strDrink })))
        })
        .catch(reject);
    });
  }}
  onChage={value => updateState(value)} // how are we supposed to achieve this
  placeholder="Your favorite drink"
  search
/>
tbleckert commented 4 years ago

@mohammed-adil The second argument in the onChange handler is the complete option object (including any optional key/values you might have there). So:

onChange={(value, option) => updateState(option.name)}

Hope that answers your question.

tbleckert commented 4 years ago

Ah sorry, misread the problem. You meant that the selectbox is empty after you select the option? The looks like a bug. Will take a look asap.

tbleckert commented 4 years ago

Found the issue, working on a fix

sm-adil commented 4 years ago

Thanks for the quick response @tbleckert. Will wait for the fix 🌟

tbleckert commented 4 years ago

@mohammed-adil Just published a new version, v1.2.1. Could you verify that the issue has been resolved and then close the issue if so?

Thanks for reporting!

sm-adil commented 4 years ago

@tbleckert everything seems to be working fine 💯. Thanks for the quick fix ⚡