ui icon indicating copy to clipboard operation
ui copied to clipboard

[bug]: Combobox search functionality searches values instead of labels

Open tbeckett1211 opened this issue 1 year ago • 7 comments

Describe the bug

The example on the web page works fine but that is because the values have the same text as the labels. Most selects will have a description as the label and some sort of ID as the value. Upon changing the values to in the examples to random strings, the search/filter does not filter based on the labels, it searches based upon the value.

Affected component/components

combobox

How to reproduce

Change values of frameworks to values that does not directly contain the same text as the labels. Then try to search for a framework in the list

Codesandbox/StackBlitz link

No response

Logs

No response

System Info

Chrome

Before submitting

  • [X] I've made research efforts and searched the documentation
  • [X] I've searched for existing issues

tbeckett1211 avatar Oct 12 '24 23:10 tbeckett1211

Hey @tbeckett1211, I would like to fix this issue. Assign it to me.

karanBRAVO avatar Oct 14 '24 14:10 karanBRAVO

@karanBRAVO I have no idea how to do that, however, I would really appreciate a patch for this.

tbeckett1211 avatar Oct 14 '24 14:10 tbeckett1211

🤩⏳ I have started working on this issue.

karanBRAVO avatar Oct 14 '24 16:10 karanBRAVO

Take a look at this cmdk PR. [email protected] adds a new keywords option. This Shadcn PR implements this.

Alternatively, you can write a custom filter function. See the cmdk docs.

BrendanC23 avatar Oct 15 '24 13:10 BrendanC23

@BrendanC23 the new "keywords" option is useful, but the combobox is still filtering for keywords + values, so you might type text that doesn't correspond to any keywords, and yet get (counterintuitive) results -- for example if a value is "zzzzz" and a label is "New York" (with corresponding ["New", "York"]) keywords), typing "z" will match New York. One would have to use the custom filter function to exclude the value key from being searched I guess

tommaso-moro avatar Dec 09 '24 11:12 tommaso-moro

This can help some one if need : search-select-field

Image

faso-dev avatar Sep 18 '25 00:09 faso-dev

For those struggling. You want the keywords prop on the CommandItem. However as @tommaso-moro rightly says, this does still search on value:

<CommandItem
    key={option.value}
    value={option.value}
    keywords={[option.label]}
/>

If you don't want value searching, update the Command filter prop:

<Command
    filter={(_, search, keywords) => {
        const extendValue = keywords?.join(" ") ?? "";
        
        return extendValue.includes(search) ? 1 : 0
    }}
/>

oliverbenns avatar Dec 11 '25 04:12 oliverbenns