tbleckert / react-select-search

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

Not selectable option when after search #171

Closed selimdoyranli closed 3 years ago

selimdoyranli commented 3 years ago

https://user-images.githubusercontent.com/8565993/104350590-53ed9a80-552a-11eb-85c6-a1d0abb5dc1f.mov

Originally posted by @bansalvks in https://github.com/tbleckert/react-select-search/issues/150#issuecomment-758819373

selimdoyranli commented 3 years ago

I reverted packages to

"fuse.js": "3.4.5", "react-select-search": "2.2.0"

fixed. but i think not a good solution

ejabu commented 3 years ago

image

I went through the code and try to commenting these two lines.

It works

icaroscherma commented 3 years ago

Using onChange fix this issue. The internal state of the selected value it's replaced if you don't propagate it, resulting in the examples showing this odd behavior. ;)

ejabu commented 3 years ago

@icaroscherma can you provide some full example ? thanks

icaroscherma commented 3 years ago

@ejabu

import React, {useState, useRef};
import {useSelect, fuzzySearch} from 'react-select-search';

const MyCustomSelect = ({ options, value, multiple, disabled, onChange, ...props }) => {
  const listRef = useRef(null);
  const [snapshot, valueProps, optionProps] = useSelect({
    options,
    value,
    multiple,
    disabled,
    onChange,
    search: true,
    filterOptions: fuzzySearch,
  });
  return ...; // your custom template goes here
};
const [selected, setSelected] = useState('');

return <><MyCustomSelect value={selected} onChange={setSelected} /></>;

Rephrasing what I said, the current render / hook flow of the project looks for a preset value after you search (exactly those 2 lines that you pointed), if you haven't a set value it will not display it, this is because it does a re-render to clear the search input.

ivanjeremic commented 3 years ago

I reverted packages to

"fuse.js": "3.4.5", "react-select-search": "2.2.0"

fixed. but i think not a good solution

This fixed it for me to thanks, something needs to be broken in the newer versions. PS. I was on the newest versions

icaroscherma commented 3 years ago

@ivanjeremic It's not broken in the newer versions, the approach used it's different, as the lib was rewritten to be more optimized. As you requested in issue #154 .

Here's your example: https://codesandbox.io/s/select-search-3-test-skjec

tbleckert commented 3 years ago

As @icaroscherma states, this was fixed in 3.0.4. Closing this one. @selimdoyranli let me know if this is still an issue.

selimdoyranli commented 3 years ago

As @icaroscherma states, this was fixed in 3.0.4. Closing this one. @selimdoyranli let me know if this is still an issue.

working on latest version. thank you

khughitt commented 2 years ago

In case anyone else runs into issues with more recent versions of fuse.js (e.g. "options.xx is undefined"), I was able to get it working by simply unpacking the matched options from the fuse query result, e.g.:

function fuzzySearch(options) {
  ...
  const opts = fuse.search(value);
  return opts.map(x => (x.item));
}