Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-selection of first item doesn't work with deferred values / suspense queries #250

Open
arvinpoddar opened this issue Apr 29, 2024 · 2 comments

Comments

@arvinpoddar
Copy link

arvinpoddar commented Apr 29, 2024

When using a deferred value to make a query (closely referencing the useDeferredValue docs example: https://react.dev/reference/react/useDeferredValue#usage), the first item in the list is not always highlighted when the query results update. Only after additional query refinement will the first result become selected.

We do get the desired behavior if we remove the shouldFilter={false}, but the auto-selection behavior should still work if you're relying on external filtering behavior.

Reproduction CodeSandbox: https://codesandbox.io/p/devbox/cmdk-suspense-9jth8l

Reproduction Video:

Screen.Recording.2024-04-29.at.3.09.10.PM.mov

Thank you!

@updbqn
Copy link

updbqn commented May 23, 2024

A workaround is to make the Command component controlled and set the selected item yourself when the items change.

https://codesandbox.io/p/devbox/cmdk-suspense-forked-8qsltv?workspaceId=693b57f1-c881-460c-b8d5-92868a4692cc

@arvinpoddar
Copy link
Author

A workaround is to make the Command component controlled and set the selected item yourself when the items change.

https://codesandbox.io/p/devbox/cmdk-suspense-forked-8qsltv?workspaceId=693b57f1-c881-460c-b8d5-92868a4692cc

Yep, ultimately the workaround we implemented was something like this, but it feels hacky to have to have a side effect on state to accomplish this. Ideally, the behavior should come baked in with Command if the user is not using it as a controlled component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants