import * as React from 'react'
import { TextInput, Text, Box, ScrollArea } from '@mantine/core'
import { IconSearch } from '@tabler/icons-react'
interface CommandItemData {
value: string
label: string
description?: string
icon?: React.ReactNode
disabled?: boolean
group?: string
}
interface CommandProps {
items: CommandItemData[]
value?: string
onValueChange?: (value: string) => void
placeholder?: string
emptyMessage?: string
className?: string
inputClassName?: string
listClassName?: string
}
function Command({ className, children, ...props }: React.ComponentPropsWithoutRef<'div'>) {
return {children}
}
function CommandInput({ className, value, onChange, placeholder, ...props }: {
className?: string
value?: string
onChange?: (e: React.ChangeEvent) => void
placeholder?: string
}) {
return (
}
className={className}
value={value}
onChange={onChange}
placeholder={placeholder}
styles={{ input: { border: 'none', borderBottom: '1px solid var(--mantine-color-default-border)' } }}
{...props}
/>
)
}
function CommandList({ className, children }: { className?: string; children?: React.ReactNode }) {
return (
{children}
)
}
function CommandEmpty({ className, children }: { className?: string; children?: React.ReactNode }) {
return (
{children}
)
}
function CommandGroup({ className, heading, children }: { className?: string; heading?: string; children?: React.ReactNode }) {
return (
{heading && {heading}}
{children}
)
}
function CommandSeparator({ className }: { className?: string }) {
return
}
function CommandItem({ className, selected, disabled, onSelect, children }: {
className?: string
selected?: boolean
disabled?: boolean
onSelect?: () => void
children?: React.ReactNode
}) {
return (
{children}
)
}
function CommandShortcut({ className, children }: { className?: string; children?: React.ReactNode }) {
return {children}
}
function CommandSearch({
items,
value,
onValueChange,
placeholder = 'Search...',
emptyMessage = 'No results found.',
className,
}: CommandProps) {
const [query, setQuery] = React.useState('')
const [selectedValue, setSelectedValue] = React.useState(value ?? '')
const filtered = React.useMemo(() => {
if (!query) return items
const q = query.toLowerCase()
return items.filter(
(item) =>
item.label.toLowerCase().includes(q) ||
item.description?.toLowerCase().includes(q) ||
item.value.toLowerCase().includes(q)
)
}, [items, query])
const groups = React.useMemo(() => {
const map = new Map()
for (const item of filtered) {
const key = item.group ?? ''
if (!map.has(key)) map.set(key, [])
map.get(key)!.push(item)
}
return map
}, [filtered])
const handleSelect = (val: string) => {
setSelectedValue(val)
onValueChange?.(val)
}
return (
setQuery(e.target.value)}
placeholder={placeholder}
/>
{filtered.length === 0 ? (
{emptyMessage}
) : (
Array.from(groups.entries()).map(([group, groupItems]) => (
{groupItems.map((item) => (
handleSelect(item.value)}
>
{item.icon && {item.icon}}
{item.label}
{item.description && (
{item.description}
)}
))}
))
)}
)
}
export { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSearch, CommandSeparator, CommandShortcut }
export type { CommandItemData, CommandProps }