import { twMerge } from "tailwind-merge"; import Input from "../form/Input"; import Button from "../layout/Button"; import Row from "../layout/Row"; import { Search as SearchIcon } from "lucide-react"; import React, { type ComponentProps, type DetailedHTMLProps, type ReactNode, } from "react"; let timeout: any; export type SearchProps = DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement > & { dispatch?: (value?: string) => void; changeHandler?: (value?: string) => void; delay?: number; inputProps?: ComponentProps; buttonProps?: Omit, "title">; loading?: boolean; placeholder?: string; componentRef?: React.RefObject; beforeSearchIconComponent?: ReactNode; no_search_button?: boolean; }; /** * # Search Component * @className_wrapper twui-search-wrapper * @className_circle twui-search-input * @className_circle twui-search-button */ export default function Search({ dispatch, changeHandler, delay = 500, inputProps, buttonProps, loading, placeholder, componentRef, beforeSearchIconComponent, no_search_button, ...props }: SearchProps) { const [input, setInput] = React.useState( props.defaultValue?.toString() || "", ); React.useEffect(() => { clearTimeout(timeout); timeout = setTimeout(() => { dispatch?.(input); changeHandler?.(input); }, delay); }, [input]); const inputRef = componentRef || React.useRef(null); // React.useEffect(() => { // if (props.autoFocus) { // inputRef.current?.focus(); // } // }, []); return ( { setInput(e.target.value); inputProps?.onChange?.(e as any); }} className={twMerge( "rounded-r-none!", "twui-search-input", inputProps?.className, )} wrapperProps={{ className: "rounded-r-none!", }} componentRef={inputRef} /> {beforeSearchIconComponent} {no_search_button ? null : ( )} ); }