Group separation, no result handling and filtering

feat/cmd-k
Kamran Ahmed 2 years ago
parent 44e5138b32
commit af05d795e3
  1. 30
      src/components/CommandMenu/CommandMenu.tsx

@ -46,11 +46,13 @@ export function CommandMenu() {
const normalizedSearchText = searchedText.trim().toLowerCase(); const normalizedSearchText = searchedText.trim().toLowerCase();
getAllPages().then((unfilteredPages = defaultPages) => { getAllPages().then((unfilteredPages = defaultPages) => {
const filteredPages = unfilteredPages.filter((currPage: PageType) => { const filteredPages = unfilteredPages
.filter((currPage: PageType) => {
return ( return (
currPage.title.toLowerCase().indexOf(normalizedSearchText) !== -1 currPage.title.toLowerCase().indexOf(normalizedSearchText) !== -1
); );
}); })
.slice(0, 10);
setActiveCounter(0); setActiveCounter(0);
setSearchResults(filteredPages); setSearchResults(filteredPages);
@ -63,7 +65,7 @@ export function CommandMenu() {
return ( return (
<div className="fixed left-0 right-0 top-0 z-50 flex h-full justify-center overflow-y-auto overflow-x-hidden bg-black/50"> <div className="fixed left-0 right-0 top-0 z-50 flex h-full justify-center overflow-y-auto overflow-x-hidden bg-black/50">
<div className="relative top-20 h-full w-full max-w-lg p-2 md:h-auto"> <div className="relative top-0 sm:top-20 h-full w-full max-w-lg p-2 md:h-auto">
<div className="relative rounded-lg bg-white shadow"> <div className="relative rounded-lg bg-white shadow">
<input <input
autofocus={true} autofocus={true}
@ -87,6 +89,8 @@ export function CommandMenu() {
); );
} else if (e.key === 'Tab') { } else if (e.key === 'Tab') {
e.preventDefault(); e.preventDefault();
} else if (e.key === 'Escape') {
setIsActive(false);
} else if (e.key === 'Enter') { } else if (e.key === 'Enter') {
const activePage = searchResults[activeCounter]; const activePage = searchResults[activeCounter];
if (activePage) { if (activePage) {
@ -98,7 +102,21 @@ export function CommandMenu() {
<div class="px-2 py-2"> <div class="px-2 py-2">
<div className="flex flex-col"> <div className="flex flex-col">
{searchResults.map((page, counter) => ( {searchResults.length === 0 && (
<div class="p-5 text-center text-sm text-gray-400">
No results found
</div>
)}
{searchResults.map((page, counter) => {
const prevPage = searchResults[counter - 1];
const groupChanged = prevPage && prevPage.group !== page.group;
return (
<>
{groupChanged && (
<div class="border-b border-gray-100"></div>
)}
<a <a
class={`block w-full rounded p-2 text-sm ${ class={`block w-full rounded p-2 text-sm ${
counter === activeCounter ? 'bg-gray-100' : '' counter === activeCounter ? 'bg-gray-100' : ''
@ -114,7 +132,9 @@ export function CommandMenu() {
)} )}
{page.title} {page.title}
</a> </a>
))} </>
);
})}
</div> </div>
</div> </div>
</div> </div>

Loading…
Cancel
Save