pacocoursey / cmdk

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

List selection doesn't reset when using external filtering #63

Open glocore opened 1 year ago

glocore commented 1 year ago

When filtering the list externally, the selected list item doesn't reset to 0 when the list changes. In the screencap below, I'm using Fuse.js to filter a list and mapping over the results.

https://user-images.githubusercontent.com/24620401/200040992-c7883cd0-9413-464e-bf5f-2dea6bb2aee5.mov

pacocoursey commented 1 year ago

That's expected; because you're managing filtering/ordering yourself, you should also take control of the value and reset it when appropriate.

glocore commented 1 year ago

@pacocoursey I agree, but resetting the value prop to the first item breaks keyboard navigation and selection since those are handled internally by cmdk. Is there a way to control those aspects externally as well?

pacocoursey commented 1 year ago

Ah interesting, maybe there should be an option to control those as well. I haven't actually the tried the case you are – will leave this issue open to see if there's a natural way to make it work…

manovotny commented 1 year ago

I ran into this too. A workaround is to control the value on Command and reset it when input changes. It appeared to work for my use case that was experiencing the same issues above.

const [selectedValue, setSelectedValue] = useState('');

return (
  <Command.Dialog
    shouldFilter={false}
    value={selectedValue}
    onValueChange={(v) => {
      setSelectedValue(v);
    }}
  >
    <Command.Input
      onValueChange={(v): void => {
        setSelectedValue('');
      }}
    />
    ...
  </Command.Dialog>
);