Skip to content

Commit 325c4ae

Browse files
committed
fix: clear search on reset
1 parent a0a46e0 commit 325c4ae

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

components/molecules/Search/index.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import { FC, FormEventHandler, useId } from "react";
2+
import { FC, FormEventHandler, useEffect, useId, useRef } from "react";
33
import classNames from "classnames";
44
import { usePathname, useRouter, useSearchParams } from "next/navigation";
55
import { useTranslation } from "react-i18next";
@@ -16,9 +16,16 @@ const Search: FC<SearchProps> = ({ className }) => {
1616
const searchParams = useSearchParams();
1717
const pathname = usePathname();
1818
const { t } = useTranslation();
19+
const formRef = useRef<HTMLFormElement>(null);
1920

2021
const searchText = searchParams?.get("search") || "";
2122

23+
useEffect(() => {
24+
if (searchText === "") {
25+
formRef?.current?.reset();
26+
}
27+
}, [searchText]);
28+
2229
const handleSubmit: FormEventHandler<HTMLFormElement> = (event) => {
2330
event.preventDefault();
2431
const search = new FormData(event.target as HTMLFormElement).get("search");
@@ -56,6 +63,7 @@ const Search: FC<SearchProps> = ({ className }) => {
5663

5764
return (
5865
<form
66+
ref={formRef}
5967
onSubmit={handleSubmit}
6068
className={classNames(styles.searchFieldset, className)}
6169
>

0 commit comments

Comments
 (0)