import { twMerge } from "tailwind-merge"; import Input, { InputProps } from "../form/Input"; import Button from "../layout/Button"; import Row from "../layout/Row"; import { Search as SearchIcon } from "lucide-react"; import React, { DetailedHTMLProps, InputHTMLAttributes, TextareaHTMLAttributes, } from "react"; let timeout: any; export type SearchProps = DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement > & { dispatch?: (value?: string) => void; delay?: number; inputProps?: InputProps; buttonProps?: DetailedHTMLProps< React.ButtonHTMLAttributes, HTMLButtonElement >; }; /** * # Search Component * @className_wrapper twui-search-wrapper * @className_circle twui-search-input * @className_circle twui-search-button */ export default function Search({ dispatch, delay = 500, inputProps, buttonProps, ...props }: SearchProps) { const [input, setInput] = React.useState(""); React.useEffect(() => { clearTimeout(timeout); timeout = setTimeout(() => { dispatch?.(input); }, delay); }, [input]); const inputRef = React.useRef(); React.useEffect(() => { if (props.autoFocus) { inputRef.current?.focus(); } }, []); return ( setInput(e.target.value)} className={twMerge( "rounded-r-none", "twui-search-input", inputProps?.className )} wrapperProps={{ className: "rounded-r-none", }} componentRef={inputRef} /> ); }