Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we use multi select with async MultiSelectorItems fetched asynchronously based on what user typed in the input #94

Open
MickaelMesnage opened this issue Aug 29, 2024 · 1 comment

Comments

@MickaelMesnage
Copy link

MickaelMesnage commented Aug 29, 2024

Hello :)

Can we use the multi-select component with MultiSelectorItems fetched asynchronously based on what is typed in the input?

I tried, but the component doesn't display the SelectorItem.

I added a prop to MultiSelectorInput to retrieve the string typed in the input in the parent component. I use this string to make an asynchronous request, and I set the result in a useState which is then used for my MultiSelectorItems, but it's not working.

This is my code :

export const AdsFiltersGameCity = () => {
  const [data, setData] = useState<string[]>([]);
  const [inputValue, setInputValue] = useState<string>("");

  const { search } = useCitySearch();

  const onCitiesChanged = (newValues: string[]) => { };

  const [_, cancel] = useDebounce(
    async () => {
      const res = await search(inputValue);
      setData(res);
    },
    400,
    [inputValue]
  );

  return (
      <MultiSelector
        values={cities}
        onValuesChange={onCitiesChanged}
        loop={false}
      >
        <MultiSelectorTrigger formatLabel={formatLabel}>
          <MultiSelectorInput setInputValueExternal={setInputValue} />
        </MultiSelectorTrigger>
        <MultiSelectorContent>
          <MultiSelectorList>
            {data.map((option, i) => (
              <MultiSelectorItem key={option} value={option}>
                {option}
              </MultiSelectorItem>
            ))}
          </MultiSelectorList>
        </MultiSelectorContent>
      </MultiSelector>
  );
};

Best regards,
Mickaël

@BelkacemYerfa
Copy link
Owner

Hi @MickaelMesnage.

Responding to your question, I think it is possible to implement search behaviour on the multi select, u just need to give the onValueChange prop the function that will handle the behaviour that you want to do after u make the search.

An example on this is what we're doing for the command search on the user end side on shadcn extension, you can find it here.

Let us know how it goes.
Best regards,
Belkacem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants