TanStackT
TanStack7mo ago
2 replies
faint-white

Using RQ with MultipleSelector component in Async

I am using this component and it's set up like this currently...

export default function MultiselectItems() {
  const { selectedItems, selectedItemQuantities, onSelectedItemsChange, onItemQuantityChange } = useJacketEditorContext()
  const [searchValue, setSearchValue] = useState('')

  const { data: items = [] } = useQuery({
    queryKey: ['itemsBySearchValue', searchValue],
    queryFn: ({ queryKey }) => {
      return itemService.getFinishedGoodItems(queryKey[1])
    },
  })

  const itemOptions = useMemo(
    () =>
      items.map((item) => ({
        value: String(item.id),
        label: `${item.sku} - ${item.name}`,
        disable: false,
        key: String(item.id),
      })),
    [items],
  )

  return (
    <>
      {/* Multi-select with tags */}
      <div>
        <MultiSelect
          options={itemOptions}
          onSearchSync={(value) => {
            setSearchValue(value)
            return itemOptions
          }}
          selectFirstItem={false}
          onChange={(opts) => {
            onSelectedItemsChange(opts)
          }}
          placeholder="Search for an item..."
          loadingIndicator={
            <p className="py-2 text-center text-lg leading-10 text-muted-foreground">loading...</p>
          }
          emptyIndicator={
            <p className="w-full text-center text-lg leading-10 text-muted-foreground">no results found.</p>
          }
        />
      </div>
   // ... (snip) ...
   )


So:
1. User enters a search term in the <MultiSelect>, which changes the SearchTerm state variable
2. The change in SearchTerm triggers RQ, which queries for a list of Items
3. The memo'd itemOptions are calculated
4. The <MultiSelect> updates with the latest options to select from

However, by using onSearchSync, there's no loading indicator displayed. I'm dont understand how to use both RQ (for caching) and this component in its async config.
shadcn/ui expansions collect lots of useful components which shadcn/ui does not have out of box. All the components are base on shadcn/ui. Just copy and paste. The component is yours.
Was this page helpful?