|
1
|
"use client" |
|
2
|
|
|
3
|
import { MagnifyingGlass, XMark } from "@medusajs/icons" |
|
4
|
import clsx from "clsx" |
|
5
|
import React from "react" |
|
6
|
import { useKeyboardShortcut } from "../../../hooks/use-keyboard-shortcut" |
|
7
|
import { Kbd } from "../../Kbd" |
|
8
|
|
|
9
|
type SearchInputProps = { |
|
10
|
onChange: (value: string) => void |
|
11
|
} & Omit<React.ComponentProps<"input">, "onChange"> |
|
12
|
|
|
13
|
export const SearchInput = ({ |
|
14
|
value, |
|
15
|
onChange, |
|
16
|
className, |
|
17
|
placeholder = "Search...", |
|
18
|
...props |
|
19
|
}: SearchInputProps) => { |
|
20
|
useKeyboardShortcut({ |
|
21
|
metakey: false, |
|
22
|
shortcutKeys: ["escape"], |
|
23
|
action: () => onChange(""), |
|
24
|
checkEditing: false, |
|
25
|
preventDefault: true, |
|
26
|
}) |
|
27
|
|
|
28
|
return ( |
|
29
|
<div className="flex flex-col gap-docs_0.5"> |
|
30
|
<div className="relative"> |
|
31
|
<MagnifyingGlass className="absolute left-docs_0.5 top-[8.5px] bottom-[8.5px] text-medusa-fg-muted" /> |
|
32
|
<input |
|
33
|
type="text" |
|
34
|
placeholder={placeholder} |
|
35
|
className={clsx( |
|
36
|
"w-full h-docs_2 pl-docs_2 text-base md:text-compact-small placeholder:text-medusa-fg-muted", |
|
37
|
"bg-medusa-bg-field text-medusa-fg-base rounded-full", |
|
38
|
"shadow-borders-base hover:bg-medusa-bg-field-hover", |
|
39
|
"focus:bg-medusa-bg-field focus:shadow-borders-interactive-with-active focus:outline-none", |
|
40
|
className |
|
41
|
)} |
|
42
|
value={value} |
|
43
|
onChange={(e) => onChange(e.target.value)} |
|
44
|
{...props} |
|
45
|
/> |
|
46
|
{value && ( |
|
47
|
<button |
|
48
|
className={clsx( |
|
49
|
"absolute right-docs_0.5 top-[8.5px] bottom-[8.5px] appearance-none", |
|
50
|
"flex items-center justify-center" |
|
51
|
)} |
|
52
|
onClick={() => onChange("")} |
|
53
|
data-testid="clear-button" |
|
54
|
> |
|
55
|
<XMark className="text-medusa-fg-muted" /> |
|
56
|
</button> |
|
57
|
)} |
|
58
|
</div> |
|
59
|
<span className="flex gap-docs_0.25 justify-end items-center text-compact-x-small"> |
|
60
|
<Kbd variant="small">esc</Kbd> |
|
61
|
<span className="text-medusa-fg-muted">Clear Search</span> |
|
62
|
</span> |
|
63
|
</div> |
|
64
|
) |
|
65
|
} |