hsuanyi-chou / shadcn-ui-expansions

More components built on top of shadcn-ui.
https://shadcnui-expansions.typeart.cc/
MIT License
856 stars 38 forks source link

[feat]: add an "onSearchSync" prop or bypass the Debounce lag #123

Closed steffanek closed 1 month ago

steffanek commented 1 month ago

Feature description

I have 2 arrays that match the Option shape locally. Those arrays are hold by possibleOptions object (see below the mockSearch fn):

When I'm using the async onSearch with a following mockSearch function with delay = 0 or by passing the prop delay={0}, I still see a debounce:

const mockSearch = async (value: string): Promise<Option[]> => {
  return new Promise(resolve => {
    setTimeout(() => {
      if (!value) {
        resolve(possibleOptions.highlighted)
      }
      const res = possibleOptions.all.filter(option =>
        option.value.toLowerCase().includes(value.toLowerCase()),
      )
      resolve(res)
    }, 0)
  })
}

When I have options locally, I should be able to have a UX without a debounce, so the result almost appear instantly. Perhaps its about bypassing the Debounce? What you think @hsuanyi-chou ? Thanks in advance!

Affected component/components

No response

Additional Context

Additional details here...

Before submitting

hsuanyi-chou commented 1 month ago

Would not providing a loadingIndicator solve the problem?

steffanek commented 1 month ago

Would not providing a loadingIndicator solve the problem?

Unfortunately no @hsuanyi-chou

hsuanyi-chou commented 1 month ago

If you want filter locally, all you have to do is to build the Option type(which is { value: string; label: string; disable?: boolean; fixed?: boolean; [key:string]: string | undefined; }) and send to the defaultOptions or options prop.

cmdk will do the rest.

steffanek commented 1 month ago

If you want filter locally, all you have to do is to build the Option type(which is { value: string; label: string; disable?: boolean; fixed?: boolean; [key:string]: string | undefined; }) and send to the defaultOptions or options prop.

cmdk will do the rest.

@hsuanyi-chou That's what I did in the beginning. But you can only pass one full list in defaultOptions. So when the user click the multi-select (focus on) it will display the list of 1000 items. And my use case is the following:

hsuanyi-chou commented 1 month ago

If your case doesn’t need to send a request, you can simply remove the debounce and the loading state code to make it become a sync filter.

All the code is in your project, not in node_modules.

steffanek commented 1 month ago

@hsuanyi-chou yes I know and I tried to make this behavior based on your component, but there is plenty of logic, and could not figure it out what exactly I need to remove.

Would not be better if there was a props to make such behavior available for everyone? I can see its a popular features to have such suggested default options list when the multi-select focus is triggered.

hsuanyi-chou commented 1 month ago

added.

steffanek commented 1 month ago

@hsuanyi-chou works perfectly, many thanks! Btw are you on discord?

hsuanyi-chou commented 1 month ago

Yes, I am on discord. Here