Skip to content

Commit d5b45c0

Browse files
authored
chore: clear search button (#91)
1 parent 41f8fa9 commit d5b45c0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/features/common/components/command.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { MagnifyingGlassIcon } from '@radix-ui/react-icons'
44
import { Command as CommandPrimitive } from 'cmdk'
55
import { Dialog, DialogContent } from '@/features/common/components/dialog'
66
import { cn } from '../utils'
7+
import { XIcon } from 'lucide-react'
8+
import { Button } from '@/features/common/components/button'
79

810
const Command = React.forwardRef<React.ElementRef<typeof CommandPrimitive>, React.ComponentPropsWithoutRef<typeof CommandPrimitive>>(
911
({ className, ...props }, ref) => (
@@ -30,10 +32,15 @@ const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
3032
)
3133
}
3234

35+
const clearSearchButtonLabel = 'Clear search'
3336
const CommandInput = React.forwardRef<
3437
React.ElementRef<typeof CommandPrimitive.Input>,
35-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
36-
>(({ className, ...props }, ref) => (
38+
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input> & {
39+
// CmdK doesn't have the option to specify search type input
40+
// so we handle it ourselves
41+
onSearchClear?: () => void
42+
}
43+
>(({ className, onSearchClear, ...props }, ref) => (
3744
<div className="flex items-center px-3" cmdk-input-wrapper="">
3845
<MagnifyingGlassIcon className="mr-2 size-4 shrink-0 opacity-50" />
3946
<CommandPrimitive.Input
@@ -44,6 +51,17 @@ const CommandInput = React.forwardRef<
4451
)}
4552
{...props}
4653
/>
54+
{onSearchClear && (
55+
<Button
56+
onClick={onSearchClear}
57+
variant="no-style"
58+
size="icon"
59+
aria-label={clearSearchButtonLabel}
60+
className={cn('size-4 text-muted-foreground')}
61+
>
62+
<XIcon size={'1rem'} />
63+
</Button>
64+
)}
4765
</div>
4866
))
4967

src/features/search/components/search.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ export function Search() {
6060

6161
return (
6262
<Command className={cn('bg-card text-card-foreground w-80 h-auto z-20 border')} shouldFilter={false} loop>
63-
<CommandInput placeholder={searchPlaceholderLabel} value={term} onValueChange={handleInput} ref={searchInputRef} />
63+
<CommandInput
64+
placeholder={searchPlaceholderLabel}
65+
value={term}
66+
onValueChange={handleInput}
67+
ref={searchInputRef}
68+
onSearchClear={clearTerm}
69+
/>
6470
<CommandList>
6571
<RenderLoadable
6672
loadable={loadableResults}

0 commit comments

Comments
 (0)