huntabyte / cmdk-sv

cmdk, but for Svelte ✨
https://cmdk-sv.com
MIT License
410 stars 17 forks source link

Combobox search feature is bugged! #42

Closed yb-cs closed 7 months ago

yb-cs commented 7 months ago

Current Behavior

The current search feature searches based on value not on label. This is not the intended behavior.

Expected Behavior

It is supposed to search based on the label not on value.

Steps To Reproduce

Just test by changing the value of the items and see that the search is based on that.

Link to Reproduction / Stackblitz (reproduction template)

No response

More Information

No response

yb-cs commented 7 months ago

a temp fix was just putting the label before the value like this:

      <Command.Item
        value={`${framework.label}:${framework.value}`}
        onSelect={(currentValue) => {
          console.log(currentValue);
          value = currentValue;
          closeAndFocusTrigger(ids.trigger);
        }}
      >

and in the function the update is as follows: // Split the value and get the second part const valuePart = value.split(":")[1]; console.log("Value part:", valuePart);

// Find the framework with the matching value
const matchingFramework = frameworks.find((f) => f.value === valuePart);
console.log("Matching framework:", matchingFramework);
huntabyte commented 7 months ago

This issue would fall under cmdk-sv, so I will move it over there to be investigated, thanks!

huntabyte commented 7 months ago

This is actually the intended behavior. If a value isn't provided, it uses the text content as the value, so it appears you're doing it correctly by updating the value to include the label if that's what you wish to search by!

yb-cs commented 7 months ago

hmmmm in the shadcn in nextjs the functionality is like I mentioned that is why I thought it was a bug it searches by label not by value. Since this is a direct port I thought it is a bug.. Thanks for the reply tho.

lukeed commented 7 months ago

Can confirm is the same behavior as shadcn-ui

huntabyte commented 7 months ago

Can confirm is the same behavior as shadcn-ui

@lukeed do you mean there is a bug with this project or that it is not and has the same functionality as shadcn-ui? Just want to make sure it's working as expected.

lukeed commented 7 months ago

Sorry. It’s the same behavior as shadcn-ui. So this can probably be closed given that this is meant to be a 1:1 replacement.

That said, when working with shadcn-ui directly, I did initially expect label text to be matched…but for compat sake the behavior would have to remain as it is currently

huntabyte commented 7 months ago

Good to hear!

@yb-cs if you're wanting to do something with some sort of value (like an id or something) on select, but search based on the label, you can just place the label in the text content (or value):

{#each things as thing}
<Command.Item onSelect={() => doSomething(thing.value)}>
    {thing.label}
</Command.Item>
{/each}