Add SearchInput component for debounced text input with customizable placeholder and change handler

This commit is contained in:
Norbert Maciaszek 2025-08-05 19:50:48 +02:00
parent 3dd776f119
commit 1154e0287f
1 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,36 @@
"use client";
import { FC, useEffect, useState } from "react";
type Props = {
defaultValue?: string;
placeholder?: string;
onChange?: (value: string) => void;
debounce?: number;
};
export const SearchInput: FC<Props> = ({
defaultValue = "",
placeholder = "Search",
onChange,
debounce = 500,
}) => {
const [value, setValue] = useState(defaultValue);
useEffect(() => {
const timer = setTimeout(() => {
onChange?.(value ?? "");
}, debounce);
return () => clearTimeout(timer);
}, [value]);
return (
<input
type="text"
placeholder={placeholder}
className="w-full p-2 rounded-md border border-gray-600 "
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
};