-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
ComboBox search behavior #458
Comments
It is using cmdk package under the hood which is not really well suited for a select. So the displayed behavior is most likely a cmdk bug. |
I was also struggling with the same issue, thanks for the fix @olsio |
I notice that when I'm using different value and label as below
CombBox only search for value, how to to search base on value and label? |
this could be a workaround if you are looking to search by label 🤔 <CommandItem
key={option.value}
value={option.label}
onSelect={(currentValue) => {
const value = options.find(
(option) => option.label.toLowerCase() === currentValue,
)?.value;
setValue(value ?? "");
setOpen(false);
}}
>
{option.label}
<CheckIcon
className={cn(
"ml-auto h-4 w-4",
value === company.value ? "opacity-100" : "opacity-0",
)}
/>
</CommandItem> |
primarily, it's ok. |
yaa so if i do this then suppose i have three people with the name Nimesh but their id is different then it only shows 1 Nimesh instead of 3 anyone facing this issue? |
you can set the value like a string template value={ and then imagine you have an onSubmit function where you receive this data, you can simply get that value and do a split value?.split('id=')[1] to get it. With this approach you will be able to search by label and by id in the searchbox. |
Combobox is the weakest component I've used for these reasons |
This needs an update asap. |
Use the custom filter option and combine
https://github.com/pacocoursey/cmdk/tree/v1.0.0?tab=readme-ov-file#parts-and-styling |
Thank you @shomyx you both helped me fix the cmdk update to v1 and the search :) |
It might be a bit more convenient to explicitly pass the keywords to the command item, so you don't have to parse the value again:
Depending on the data, I mostly found it more useful to normalise the search term and the haystack to lower case:
|
@joblab have you tried this? How do I pass the keywords to the filter function? |
@malun22 Yeah, I use the above code in production. The Command Component automatically passes the keywords for each Command item as the third argument to the filter function you pass to the Command Component via the filter prop. |
@joblab hm I see. Cool concept, but my keywords list seems to be empty always even when I give the Item the keyword property. |
I have a workaround filter = {(value, search) => { |
I combined the ids and name in values and I made a search with name
This is my demo data if it helps:
|
Hi guys, I tried all the above solutions but none worked for me. This is what worked for me. I liked it because I can use any keywords I want without depending on label
|
Hey @salman486 just to give you some more things to look into. cmdk itself offers a keywords property on the CommandItem
Which it will use for the search. Then you can just use the value to be unique without the hacks you added. The fuzzy match logic can be a bit too fuzzy but for that you can override the filter property on the Command component.
|
I have tried using that the issue is that in filter function in Command component it always gives me keywords = []. I am using cmdk version |
I just used it in a project and it was working fine so I would assume it was added with 1.0.0 |
I've implemented a workaround using the Command component's filter feature. Here's how I've set it up: <Popover
open={comboboxIsOpen}
onOpenChange={setComboboxIsOpen}
>
<PopoverTrigger asChild>
<Button
variant='outline'
role='combobox'
aria-expanded={comboboxIsOpen}
className='w-[200px] justify-between'
>
{comboboxValue
? platforms.find(
(platform) =>
platform.value === comboboxValue
)?.label
: 'Selecione uma Plataforma'}
<ChevronsUpDown className='ml-2 h-4 w-4 shrink-0 opacity-50' />
</Button>
</PopoverTrigger>
<PopoverContent className='w-[200px] p-0'>
<Command
filter={(value, search) => {
const sanitizedSearch = search.replace(
/[-\/\\^$*+?.()|[\]{}]/g,
'\\$&'
);
const searchRegex = new RegExp(
sanitizedSearch,
'i'
);
const platformLabel =
platforms.find(
(platform) => platform.value === value
)?.label || '';
return searchRegex.test(platformLabel) ? 1 : 0;
}}
>
<CommandInput placeholder='Buscar Plataforma...' />
<CommandList>
<CommandEmpty>
Nenhuma plataforma encontrada.
</CommandEmpty>
<CommandGroup>
{platforms.map((platform) => (
<CommandItem
key={platform.value}
value={platform.value}
onSelect={(currentValue) => {
setComboboxValue(
currentValue === comboboxValue
? ''
: currentValue
);
setComboboxIsOpen(false);
}}
>
<Check
className={cn(
'mr-2 h-4 w-4',
comboboxValue === platform.value
? 'opacity-100'
: 'opacity-0'
)}
/>
{platform.label}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover> |
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you. |
Can this be re-opened? |
Of course. |
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you. |
If you want to search in labels only (and not in values) use this:First put label inside value. Then split value inside filter function. (I'm using substring instead of split[1] in case there was an space in the label)
|
@shadcn can this be reopen |
The original issue seems fixed, but it is still a bit confusing that when typing For someone like me who wants to do an exact search, you can do the following: <Command
filter={(value, search) => {
if (value.includes(search)) return 1
return 0
}}
/> as stated in the here |
The search box is not performing a new search when you hit the backspace key. if I type "nexz" I get no results when I hit backspace and now have "nex" I still have no result even though "Next.js" should show.
The text was updated successfully, but these errors were encountered: