import React, { forwardRef, InputHTMLAttributes, ReactNode, useEffect, useRef, useState, } from "react" import { SearchIcon, XIcon } from "lucide-react" import { cn } from "../../design-lib/utils" import KBD from "./keyboard" import { Note, NoteMessage } from "./note" interface InputProps extends Omit, "prefix" | "suffix"> { type?: "text" | "password" | "email" | "number" | "search" | "tel" | "url" prefix?: ReactNode suffix?: ReactNode disablePrefixStyling?: boolean disableSuffixStyling?: boolean errored?: boolean errorWithMessage?: string info?: string } interface SearchInputProps extends InputHTMLAttributes { clearButton?: boolean removeStyling?: boolean keyboard?: string | "alt" | "ctrl" | "shift" | "command" } const Input = forwardRef( ( { className, type = "text", prefix, suffix, disablePrefixStyling = false, disableSuffixStyling = false, errored, errorWithMessage, info, ...props }, ref ) => { const prefixRef = useRef(null) const [prefixWidth, setPrefixWidth] = useState(null) useEffect(() => { if (prefixRef.current) { const { width } = prefixRef.current.getBoundingClientRect() setPrefixWidth(!disablePrefixStyling ? width + 18 : width) } }, [prefix, disablePrefixStyling]) const inputStyle = { paddingLeft: prefix ? `${prefixWidth}px` : "20px", } return (
{prefix && (
svg]:h-3.5 [&>svg]:w-3.5", { " border-r bg-accent": !disablePrefixStyling } )} > {prefix}
)} {suffix && (
svg]:h-3.5 [&>svg]:w-3.5", { "border-l bg-accent": !disableSuffixStyling } )} > {suffix}
)}
{errorWithMessage && ( {errorWithMessage} )} {info && ( {info} )}
) } ) Input.displayName = "Input" const SearchInput = forwardRef( ( { className, clearButton = false, removeStyling = false, keyboard, ...props }, ref ) => { const [value, setValue] = useState("") const inputRef = useRef(null) const handleClear = () => { setValue("") if (inputRef.current) { inputRef.current.focus() } } useEffect(() => { if (ref && "current" in ref) { ref.current = inputRef.current } }, [ref]) return (
{keyboard && (
{keyboard !== "command" && keyboard !== "shift" && keyboard !== "alt" && keyboard !== "ctrl" ? keyboard : ""}
)} setValue(e.target.value)} {...props} /> {clearButton && value && ( )}
) } ) SearchInput.displayName = "SearchInput" export { Input, SearchInput }