SearchCmdk 10.17.3
Command menu search can be used as an accessible combobox. You can render items, and it will automatically filter and sort them.
Default
"use client";
import { useEffect, useState } from "react";
import { SearchCmdk } from "@heathmont/moon-cmdk-tw";
type Item = {
label: string;
};
const items = [
{ label: "Aurum" },
{ label: "Argentum" },
{ label: "Zinc" },
{ label: "Plumbum" },
];
function Default() {
const [open, setOpen] = useState(false);
const [search, setSearch] = useState("");
useEffect(() => {
const down = (e: KeyboardEvent) => {
// Toggle the menu when ⌘K is pressed
if (e.key === "s" && e.metaKey) {
e.preventDefault();
setOpen((open) => !open);
}
};
document.addEventListener("keydown", down);
return () => document.removeEventListener("keydown", down);
}, []);
const filterItems = (values: Item[], search: string) => {
return values.filter(({ label }) => +label.toLowerCase().includes(search));
};
const filteredItems = filterItems(items, search.toLowerCase());
return (
<>
<SearchCmdk.Trigger
onClick={() => {
setOpen(true);
}}
>
<SearchCmdk.TriggerIcon />
<span className="text-moon-16">Search...</span>
<SearchCmdk.TriggerKbd>⌘K</SearchCmdk.TriggerKbd>
</SearchCmdk.Trigger>
{open && <SearchCmdk.Overlay />}
<SearchCmdk
open={open}
onOpenChange={setOpen}
label="Command Menu"
shouldFilter={false}
loop={true}
>
<SearchCmdk.InputWrapper>
<SearchCmdk.Icon />
<SearchCmdk.Input
value={search}
onValueChange={setSearch}
placeholder="Search for a component"
/>
<SearchCmdk.Kbd onClick={() => setOpen(false)}>Esc</SearchCmdk.Kbd>
</SearchCmdk.InputWrapper>
<SearchCmdk.Separator />
<SearchCmdk.Result>
<SearchCmdk.NoResults>No Results</SearchCmdk.NoResults>
{filteredItems.map(({ label }) => (
<SearchCmdk.ResultItem
key={label}
value={label}
onSelect={() => {
setOpen(false);
}}
>
{label}
</SearchCmdk.ResultItem>
))}
</SearchCmdk.Result>
</SearchCmdk>
</>
);
}
export default Default;
SearchCmdk
These are props specific to the SearchCmdk component:
Name | Type | Default |
---|---|---|
open* | boolean | - |
onOpenChange* | (open: boolean) => void | - |
label | string | - |
className | string | - |
Properties indicated with * are required.
SearchCmdk.InputWrapper
These are props specific to the SearchCmdk.InputWrapper component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.Input
These are props specific to the SearchCmdk.Input component:
Name | Type | Default |
---|---|---|
className | string | - |
placeholder | string | - |
SearchCmdk.Icon
These are props specific to the SearchCmdk.Icon component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.Separator
These are props specific to the SearchCmdk.Separator component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.Result
These are props specific to the SearchCmdk.Result component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.NoResults
These are props specific to the SearchCmdk.NoResults component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.ResultItem
These are props specific to the SearchCmdk.ResultItem component:
Name | Type | Default |
---|---|---|
className | string | - |
onSelect | (value: string) => void | - |
SearchCmdk.Kbd
These are props specific to the SearchCmdk.Kbd component:
Name | Type | Default |
---|---|---|
className | string | - |
SearchCmdk.Overlay
These are props specific to the SearchCmdk.Overlay component:
Name | Type | Default |
---|---|---|
className | string | - |