This repository was archived by the owner on Jun 19, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathsearch.tsx
More file actions
184 lines (166 loc) · 5.49 KB
/
search.tsx
File metadata and controls
184 lines (166 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React, { ReactNode, useEffect, useState } from "react";
import clsx from "clsx";
import { GrClose } from "react-icons/gr";
import { FaSearch } from "react-icons/fa";
import { Spinner } from "../SpinLoader/spin-loader";
import { ScrollArea } from "../ScrollArea/scroll-area";
interface RichSearchSuggestion {
key: string;
node: ReactNode;
}
interface SearchProps {
name: string;
value?: string;
placeholder?: string;
autoFocus?: boolean;
className?: string;
onSearch?: (search?: string) => void;
suggestions?: string[] | RichSearchSuggestion[];
suggestionsLabel?: string;
onChange?: (value: string) => void;
onSelect?: (value: string) => void;
isLoading?: boolean;
isDisabled?: boolean;
labelText: string;
}
const suggestionsStyle = {
"-webkit-line-clamp": "1",
display: "-webkit-box",
"-webkit-box-orient": "vertical",
};
const Search = ({
placeholder,
name,
value,
autoFocus,
className,
onSearch,
suggestions = [],
suggestionsLabel,
onChange,
isLoading,
onSelect,
isDisabled,
labelText,
}: SearchProps): JSX.Element => {
const [cursor, setCursor] = useState(-1);
const [search, setSearch] = useState(value);
const [showSuggestions, setShowSuggestions] = useState(false);
const handleOnSearch = () => {
onSearch?.(search);
};
const handleEmpty = () => {
setSearch("");
onSearch?.("");
onChange?.("");
};
useEffect(() => {
setSearch(value);
}, [value]);
const handleOnSelect = (suggestion: string) => {
if (onSelect) {
onSelect(suggestion);
setSearch("");
} else {
setSearch(suggestion);
onSearch?.(suggestion);
}
setShowSuggestions(false);
setCursor(-1);
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearch(e.target.value);
onChange?.(e.target.value);
};
const handleKeyboardCtrl: React.KeyboardEventHandler<HTMLInputElement> = (e) => {
const resultsCount = suggestions?.length || 0;
if (resultsCount && e.key === "ArrowUp") {
e.preventDefault();
setCursor(cursor === 0 ? Math.min(resultsCount - 1, 9) : cursor - 1);
}
if (resultsCount && e.key === "ArrowDown") {
e.preventDefault();
setCursor(cursor === Math.min(resultsCount - 1, 9) ? 0 : cursor + 1);
}
if (resultsCount && e.key === "Enter") {
e.preventDefault();
if (document.querySelector("._cursorActive")) {
const { suggestion } = (document.querySelector("._cursorActive") as HTMLElement).dataset;
if (suggestion && suggestion.length > 0) {
handleOnSelect(suggestion);
setCursor(-1);
}
}
}
};
// using min-w-[15rem] to take into account the width of the X icon when it gets rendered
// to avoid the search input from expanding when someone starts typing.
return (
<label
className={`${
className && className
} flex bg-white py-1 px-3 shadow-input border transition focus-within:ring focus-within:border-orange-500 focus-within:ring-orange-100 rounded-lg ring-light-slate-6 items-center relative min-w-[15rem]`}
>
<span className="sr-only">{labelText}</span>
<FaSearch className="text-light-slate-9" fontSize={16} onClick={handleOnSearch} />
<input
className="w-full pl-2 placeholder:text-sm focus:outline-none placeholder:text-slate-400 disabled:cursor-not-allowed"
placeholder={placeholder}
name={name}
value={search}
type="search"
onChange={handleChange}
onKeyUp={(e) => {
if (e.code === "Enter") {
handleOnSearch();
}
}}
disabled={isDisabled}
onKeyDown={handleKeyboardCtrl}
onFocus={() => setShowSuggestions(true)}
onBlur={() => setTimeout(() => setShowSuggestions(false), 500)}
/>
{suggestions && suggestions.length > 0 && showSuggestions && (
<div className="absolute left-0 z-10 w-full pb-1 space-y-1 bg-white border rounded-lg cursor-pointer shadow-input border-light-slate-6 top-full">
<ScrollArea type="auto" className="h-60">
{suggestionsLabel && suggestions.length > 0 ? <div className="pl-5 pt-4">{suggestionsLabel}</div> : null}
{suggestions.map((suggestion, index) => (
<button
className={clsx(
cursor === index && "_cursorActive bg-slate-100",
"px-4 py-2 hover:bg-light-slate-2",
"[&_span]:max-w-[13rem]"
)}
style={suggestionsStyle}
key={index}
data-suggestion={typeof suggestion === "string" ? suggestion : suggestion.key}
onClick={(event) => {
const { suggestion } = (event.currentTarget as HTMLElement).dataset;
suggestion && handleOnSelect(suggestion);
}}
>
{typeof suggestion === "string" ? (
<span className="pl-5 text-sm md:mw-auto inline-block text-ellipsis truncate tracking-tighter">
{suggestion}
</span>
) : (
suggestion.node
)}
</button>
))}
</ScrollArea>
</div>
)}
{search && (
<>
{isLoading ? (
<Spinner className="w-5 h-5" />
) : (
<GrClose className="cursor-pointer text-light-slate-9" fontSize={16} onClick={handleEmpty} />
)}
</>
)}
</label>
);
};
export default Search;