Skip to content

Commit 985b434

Browse files
committed
client: add useDebounceValue hook and use it inside the SelectInput.tsx component to filter the values
1 parent b7977df commit 985b434

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

client/src/components/input/SelectInput.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { FC, useEffect, useRef, useState } from "react";
22
import { FiCheck } from "react-icons/fi";
3+
import { IoMdClose } from "react-icons/io";
4+
import { useDebounceValue } from "../../hooks/useDebounceValue";
35
import { useOuterClick } from "../../hooks/useOuterClick";
46

57
interface SelectInputProps {
@@ -15,23 +17,28 @@ const SelectInput: FC<SelectInputProps> = (props) => {
1517
const [isActive, setIsActive] = useState<boolean>(false);
1618
const [filterValue, setFilterValue] = useState<string>("");
1719
const [options, setOptions] = useState<string[]>([...props.options]);
20+
const debounceFilterValue = useDebounceValue(filterValue, 250);
1821

1922
const ref = useOuterClick(() => setIsActive(false));
2023
const inputRef = useRef<HTMLInputElement>(null);
2124
const activeRef = useRef<HTMLSpanElement>(null);
2225

2326
useEffect(() => {
24-
if (filterValue.trim() === "") {
27+
if (debounceFilterValue.trim().length < 1) {
2528
setOptions([...props.options]);
29+
activeRef.current?.scrollIntoView({
30+
block: "nearest",
31+
inline: "start",
32+
});
2633
return;
2734
}
2835

2936
setOptions(
3037
[...props.options].filter((o) =>
31-
o.includes(filterValue.toLowerCase().trim())
38+
o.includes(debounceFilterValue.toLowerCase().trim())
3239
)
3340
);
34-
}, [filterValue, props.options]);
41+
}, [debounceFilterValue, props.options]);
3542

3643
const selectValue = (value: string): void => {
3744
setIsActive(false);
@@ -136,6 +143,19 @@ const SelectInput: FC<SelectInputProps> = (props) => {
136143
</li>
137144
);
138145
})}
146+
147+
{options.length < 1 && (
148+
<li
149+
className="flex items-center gap-4 py-1 px-2 transition-all duration-150
150+
text-gh-text min-w-[10rem]"
151+
>
152+
<span>
153+
<IoMdClose />
154+
</span>
155+
156+
<span>No theme found</span>
157+
</li>
158+
)}
139159
</ul>
140160
</div>
141161
</div>

client/src/hooks/useDebounceValue.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect, useState } from "react";
2+
3+
export const useDebounceValue = <T>(value: T, time: number = 200): T => {
4+
const [debounceValue, setDebounceValue] = useState<T>(value);
5+
6+
useEffect(() => {
7+
const timeout = setTimeout(() => setDebounceValue(value), time);
8+
9+
return () => clearTimeout(timeout);
10+
}, [value, time]);
11+
12+
return debounceValue;
13+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-readme-tech-stack",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "Display your favorite technologies, tools, or the tech stack your project uses with these fully customizable, good-looking cards on your GitHub README!",
55
"main": "src/app.ts",
66
"scripts": {

0 commit comments

Comments
 (0)