|
1
|
"use client" |
|
2
|
|
|
3
|
import * as Icons from "@medusajs/icons" |
|
4
|
import { Container, Input, Text } from "@medusajs/ui" |
|
5
|
import clsx from "clsx" |
|
6
|
import { CopyButton } from "docs-ui" |
|
7
|
import * as React from "react" |
|
8
|
|
|
9
|
const iconNames = Object.keys(Icons).filter((name) => name !== "default") |
|
10
|
|
|
11
|
const IconSearch = () => { |
|
12
|
const [query, setQuery] = React.useState<string | undefined>("") |
|
13
|
|
|
14
|
return ( |
|
15
|
<div className="mt-8 flex flex-col gap-y-2"> |
|
16
|
<Input |
|
17
|
type="search" |
|
18
|
value={query} |
|
19
|
onChange={(e) => setQuery(e.target.value)} |
|
20
|
/> |
|
21
|
<Container> |
|
22
|
<SearchResults query={query} /> |
|
23
|
</Container> |
|
24
|
</div> |
|
25
|
) |
|
26
|
} |
|
27
|
|
|
28
|
const SearchResults = ({ query = "" }: { query?: string }) => { |
|
29
|
const cleanQuery = escapeStringRegexp(query.trim().replace(/\s/g, " ")) |
|
30
|
const results = iconNames.filter((name) => |
|
31
|
new RegExp(`\\b${cleanQuery}`, "gi").test(name) |
|
32
|
) |
|
33
|
|
|
34
|
if (results.length === 0) { |
|
35
|
return ( |
|
36
|
<div |
|
37
|
className={clsx( |
|
38
|
"text-medusa-text-muted dark:text-medusa-text-muted-dark", |
|
39
|
"flex min-h-[300px] items-center justify-center" |
|
40
|
)} |
|
41
|
> |
|
42
|
<Text> |
|
43
|
No results found for{" "} |
|
44
|
<Text weight={"plus"} asChild> |
|
45
|
<span>{query}</span> |
|
46
|
</Text> |
|
47
|
</Text> |
|
48
|
</div> |
|
49
|
) |
|
50
|
} |
|
51
|
|
|
52
|
return ( |
|
53
|
<div className="grid w-full grid-cols-4 gap-8 md:grid-cols-6 lg:grid-cols-8"> |
|
54
|
{results.map((name) => { |
|
55
|
return ( |
|
56
|
<div |
|
57
|
key={name} |
|
58
|
className="flex h-full w-full items-center justify-center" |
|
59
|
> |
|
60
|
<CopyButton text={name} tooltipText={name} handleTouch> |
|
61
|
<div |
|
62
|
className={clsx( |
|
63
|
"border-medusa-border-base", |
|
64
|
"flex h-10 w-10 items-center justify-center rounded-lg border" |
|
65
|
)} |
|
66
|
> |
|
67
|
<span className="sr-only">Icon named {name}</span> |
|
68
|
<div |
|
69
|
className={clsx( |
|
70
|
"bg-medusa-bg-component text-medusa-fg-base", |
|
71
|
"flex h-8 w-8 items-center justify-center rounded-[4px]" |
|
72
|
)} |
|
73
|
> |
|
74
|
{React.createElement(Icons[name as keyof typeof Icons])} |
|
75
|
</div> |
|
76
|
</div> |
|
77
|
</CopyButton> |
|
78
|
</div> |
|
79
|
) |
|
80
|
})} |
|
81
|
</div> |
|
82
|
) |
|
83
|
} |
|
84
|
|
|
85
|
// https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js |
|
86
|
function escapeStringRegexp(string: unknown) { |
|
87
|
if (typeof string !== "string") { |
|
88
|
throw new TypeError("Expected a string") |
|
89
|
} |
|
90
|
|
|
91
|
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d") |
|
92
|
} |
|
93
|
|
|
94
|
export { IconSearch } |