-
-
Notifications
You must be signed in to change notification settings - Fork 289
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
The first item is not selected by default when Command.List
content is dynamic
#280
Comments
Maybe exposing the store object to control the value stored can be a solution, +1 checked this on chrome and firefox |
Same issue for me did you find a solution ? |
Found a workaround, you can add a Key to the CommandList. When the key changes, the component is fully rerendered. For me i added the query as the key. Everytime the query changes, the command is fully rerendered with the right selected first item. |
Hello! I just applied changes to the example I've attached but I see no changes in behavior. Am I doing something wrong? |
For your case II add to put the key at the command level : the general idea is that id a key element is changed, the element will be fuly rerender after a state change. In my case the results list are in a children component. Rerendering this component was enought for my use case. You need to also include the isloading with Q because you want a full rerender when is transition from loading to not loading. This is a woraround, not a best practice |
If it helps, I had to add |
Adding a key does kinda mess up the performance and behavior of the list, especially when switching from no results to exists results. Adding the key to |
Any solution? Still encountering the problem. |
I suspect others like me are trying to use this component to build a nice "typeahead" kind of experience, and this bug totally breaks it :( |
The problem is that the component is used uncontrolled. To fix this, control the Command instance like this: const items = ... // result of async loading
const [activeItem, setActiveItem] = useState<string | undefined>();
// this is just a classic direct rerender, you could avoid it if you can call
// setActiveItem from where you load items
const lastItems = useRef(items);
if (items !== lastItems.current) {
lastItems.current = items;
setActiveItem(items?.[0]?.id ?? undefined);
}
return (
<Command
shouldFilter={false}
value={activeItem}
onValueChange={(itemId) => {
setActiveItem(itemId);
}}
>
....
<CommandItem
key={item.id}
value={item.id} |
you shouldn't call setState in the body of the function like this |
Issue description
When content of
Command.List
is dynamic eg rendering result of a search API endpoint results the first item is not always selected by default.In case I am doing something wrong, please let me know.
Setup
shadcn's Command component, cmdk@1.0.0
Repro
Repro steps for stackblitz template I drafted up.
Expected:
Add <query>
item appears - it's highlighted since it's the first item in the listActual:
Add <query>
item appears - it's NOT highlightedEDIT: bringing code sample here, just in case
The text was updated successfully, but these errors were encountered: