sickdyd/react-search-autocomplete

Keep the search string the user entered in the search box onSelect.

Opened this issue · 0 comments

Hi there, currently when a user selects an item from the list of results the search input changes to reflect the name (or specified key) of the result item. I would prefer if it kept the user input in the search box.

I tried to accomplish this by updating the search string string via a state variable, but for whatever reason it refuses to change. I can set it to a empty string just fine OnSelect, i.e. setSearchString("")

const [searchString, setSearchString] = useState("");

const handleOnSearch = (string, results) => {
    setSearchString(string);
};
  
const handleOnSelect = (item) => {    
    history.push(`${url}resource-centre/${item.categorySlug}/${item.slug}`);

    console.log(searchString); // logs expected string
    setSearchString(searchString); // input does not update when state variable updated
};

<ReactSearchAutocomplete ... inputSearchString={searchString} onSearch={handleOnSearch} onSelect={handleOnSelect} ... />