Open jaxxreal opened 4 months ago
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.
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 : <Command className="rounded-lg border shadow-md" shouldFilter={false} loop key={isLoading.toString().concat(q)}
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 autoFocus
to the <CommandInput>
to stop input losing focus on re-render.
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 Command.List
is slightly more stable but not as good.
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}
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