CaptainCodeman / svelte-headlessui

HeadlessUI components for Svelte
https://captaincodeman.github.io/svelte-headlessui/
MIT License
549 stars 25 forks source link

Combobox filter function bug #88

Closed sillvva closed 7 months ago

sillvva commented 7 months ago

In this filter function:

  const filter = async (value: string) => {
    // current active item
    const current = state.active === -1
      ? state.selected
      : state.items[state.active].value

I'm running into a bug where state.active is undefined. (Refer to the first value in the console log screenshot.)

CleanShot 2024-02-15 at 19 22 48@2x

I can reproduce this reliably. It happens on a fresh page refresh when I start entering an input in a combobox for the first time. The second time (after exiting and refocusing the combobox), it works correctly.

I believe it can be fixed with this change:

    const current = state.active === -1
      ? state.selected
-     : state.items[state.active].value
+     : state.items[state.active || 0].value

It will default to the first item if state.active is undefined.

sillvva commented 7 months ago

Apparently, it was caused by something I did on my end. Turns out, if you set $combobox.selected = null, you also need to set $combobox.active = 0. Otherwise, that error happens.