Skip to content

Commit b67df3c

Browse files
committed
display kbd shortcut inside the search input field
1 parent 225c9c8 commit b67df3c

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/components/LanguagePicker/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const LanguagePicker = ({
109109
<CommandInput
110110
placeholder={t("page-languages-filter-placeholder")}
111111
className="h-9"
112+
kbdShortcut="\"
112113
/>
113114

114115
<CommandList className="max-h-[75vh]">

src/components/ui/command.tsx

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react"
22
import { Command as CommandPrimitive } from "cmdk"
3-
import { Search } from "lucide-react"
3+
import { MdOutlineSearch } from "react-icons/md"
44
import { type DialogProps } from "@radix-ui/react-dialog"
55

66
import { Dialog, DialogContent } from "@/components/ui/dialog"
@@ -36,25 +36,42 @@ const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
3636
)
3737
}
3838

39+
type CommandInputProps = React.ComponentPropsWithoutRef<
40+
typeof CommandPrimitive.Input
41+
> & {
42+
icon?: React.ElementType
43+
kbdShortcut?: string
44+
}
45+
3946
const CommandInput = React.forwardRef<
4047
React.ElementRef<typeof CommandPrimitive.Input>,
41-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
42-
>(({ className, ...props }, ref) => (
43-
<div
44-
className="flex items-center rounded border border-body px-3"
45-
cmdk-input-wrapper=""
46-
>
47-
<CommandPrimitive.Input
48-
ref={ref}
49-
className={cn(
50-
"placeholder:text-muted-foreground flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none disabled:cursor-not-allowed disabled:opacity-50",
51-
className
48+
CommandInputProps
49+
>(({ className, icon = MdOutlineSearch, kbdShortcut, ...props }, ref) => {
50+
const Icon = icon
51+
return (
52+
<div
53+
className="flex items-center rounded border border-body px-3"
54+
cmdk-input-wrapper=""
55+
>
56+
<CommandPrimitive.Input
57+
ref={ref}
58+
className={cn(
59+
"placeholder:text-muted-foreground flex h-11 w-full rounded-md bg-transparent py-3 text-sm outline-none disabled:cursor-not-allowed disabled:opacity-50",
60+
className
61+
)}
62+
{...props}
63+
/>
64+
{kbdShortcut && (
65+
<kbd className="rounded border border-solid border-disabled px-1 text-sm text-disabled">
66+
{kbdShortcut}
67+
</kbd>
5268
)}
53-
{...props}
54-
/>
55-
<Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
56-
</div>
57-
))
69+
{!kbdShortcut && Icon && (
70+
<Icon className="mr-2 h-4 w-4 shrink-0 opacity-50" />
71+
)}
72+
</div>
73+
)
74+
})
5875

5976
CommandInput.displayName = CommandPrimitive.Input.displayName
6077

0 commit comments

Comments
 (0)