sickdyd / react-search-autocomplete

A search box that filters the provided array of objects
https://sickdyd.github.io/react-search-autocomplete
MIT License
216 stars 82 forks source link

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

Open webstar opened 1 year ago

webstar commented 1 year ago

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