Add SearchInput component for debounced text input with customizable placeholder and change handler
This commit is contained in:
parent
3dd776f119
commit
1154e0287f
|
|
@ -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)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue