cmdk
cmdk copied to clipboard
[Question] How to make the selected item appear on top of the list instead of the bottom ?
When i open cmdk, the selected item appears at the end, how can we make it appear on top ?
You can just filter the items to remove the selected ones and append the selected one to the beginning of the array.
Here is a ChatGPT example:
// Suppose you have an array of items and a selected item
const items: string[] = ['item1', 'item2', 'item3', 'item4'];
const selectedItem: string = 'item3';
// Function to filter and reorder the items
function filterAndReorderItems(items: string[], selectedItem: string): string[] {
// Filter the items to remove the selected item
const filteredItems = items.filter(item => item !== selectedItem);
// Add the selected item to the beginning of the array
const reorderedItems = [selectedItem, ...filteredItems];
return reorderedItems;
}
// Call the function and log the result
const result = filterAndReorderItems(items, selectedItem);
console.log(result); // Output: ['item3', 'item1', 'item2', 'item4']
Yes i can do it like this, i was looking for a official solution in cmdk.
Thanks