Skip to content

Commit c83655c

Browse files
authored
chore: improve search UX (#1037)
chore: improve UX for search
1 parent c2a3acc commit c83655c

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

app/_components/search.tsx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,57 @@
22

33
import { faMagnifyingGlass } from "@fortawesome/free-solid-svg-icons";
44
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
5-
import { Input } from "@heroui/react";
5+
import { Input, Spinner } from "@heroui/react";
66
import { useRouter } from "next/navigation";
7-
import { useState } from "react";
7+
import { useState, useTransition } from "react";
88

99
export function SearchButton() {
1010
const [value, setValue] = useState("");
11+
const [isPending, startTransition] = useTransition();
1112
const router = useRouter();
1213

1314
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
14-
if (event.key === "Enter") {
15-
router.push(`/search?q=${value}`);
15+
if (event.key === "Enter" && value.trim()) {
16+
startTransition(() => {
17+
router.push(`/search?q=${encodeURIComponent(value.trim())}`);
18+
});
19+
}
20+
};
21+
22+
const handleSubmit = (event: React.FormEvent) => {
23+
event.preventDefault();
24+
if (value.trim()) {
25+
startTransition(() => {
26+
router.push(`/search?q=${encodeURIComponent(value.trim())}`);
27+
});
1628
}
1729
};
1830

1931
return (
20-
<Input
21-
type="search"
22-
placeholder="Search packages"
23-
aria-label="Search packages"
24-
labelPlacement="outside"
25-
startContent={
26-
<FontAwesomeIcon
27-
className="text-default-600 dark:text-default-500"
28-
icon={faMagnifyingGlass}
29-
width={13}
30-
/>
31-
}
32-
value={value}
33-
onValueChange={setValue}
34-
onKeyDown={handleKeyDown}
35-
>
36-
Search packages
37-
</Input>
32+
<form onSubmit={handleSubmit}>
33+
<Input
34+
type="search"
35+
placeholder="Search packages"
36+
aria-label="Search packages"
37+
labelPlacement="outside"
38+
startContent={
39+
isPending ? (
40+
<Spinner size="sm" className="text-default-600" />
41+
) : (
42+
<FontAwesomeIcon
43+
className="text-default-600 dark:text-default-500"
44+
icon={faMagnifyingGlass}
45+
width={13}
46+
/>
47+
)
48+
}
49+
value={value}
50+
onValueChange={setValue}
51+
onKeyDown={handleKeyDown}
52+
isDisabled={isPending}
53+
>
54+
Search packages
55+
</Input>
56+
</form>
3857
);
3958
}

app/search/loading.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Loading } from "../_components/loading";
2+
3+
export default function SearchLoading() {
4+
return <Loading />;
5+
}

0 commit comments

Comments
 (0)