cmdk icon indicating copy to clipboard operation
cmdk copied to clipboard

[Question] How to make the selected item appear on top of the list instead of the bottom ?

Open ghostlexly opened this issue 1 year ago • 2 comments

When i open cmdk, the selected item appears at the end, how can we make it appear on top ?

Capture d’écran 2024-08-05 à 12 46 23

ghostlexly avatar Aug 05 '24 10:08 ghostlexly

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']

brasillero avatar Aug 08 '24 20:08 brasillero

Yes i can do it like this, i was looking for a official solution in cmdk.

Thanks

ghostlexly avatar Sep 22 '24 11:09 ghostlexly