pacocoursey / cmdk

Fast, unstyled command menu React component.
https://cmdk.paco.me
MIT License
9.42k stars 265 forks source link

Value from `onValueChange` not detecting filtered value #68

Closed davidkpiano closed 1 year ago

davidkpiano commented 1 year ago

Discovered this when narrowing the input to select only one value, and noticed that the right area didn't change:

https://user-images.githubusercontent.com/1093738/205722637-4a2802d2-c239-4de9-b66f-cbe37d509536.mp4

nathanlambert commented 1 year ago

It's not just that example either:

https://user-images.githubusercontent.com/248587/207175507-8b303660-cb3a-4326-96cb-713c0d7562d7.mov

JettChenT commented 1 year ago

A workaround: querying for the aria-selected item

Based on: https://github.com/pacocoursey/cmdk/blob/605ae1304baff5ed6f7675d6028ad8dc0b93f0ca/cmdk/src/index.tsx#L408-L410

Step 1: Create a reference to the command

const cmdref = useRef<any>(null)
<Command
    // ...
    ref={cmdref}
>
    // ...
    <Command.Item value="target">
        target
    </Command.Item>
</Command>

Step 2: Access the value of the command when needed:

const val = cmdref.current.querySelector(`[cmdk-item=""][aria-selected="true"]`).dataset.value
// val = "target"