import { ChangeEvent, ComponentType, forwardRef, InputHTMLAttributes, useState, } from "react"; import { attributes } from "../utils/attributes"; import { classnames } from "../utils/classnames"; import "./input.css"; type Props = { id: string; label?: string; /** * Helper text to add indication about your input. */ helper?: string; /** * Add an icon on the left. */ Icon?: ComponentType; /** * If the mode is "auto", the component will check the invalid state * on change and add an invalid state if it is invalid. */ mode?: "auto"; isInvalid?: boolean; /** * Apply a class to the input element */ inputClassName?: string; inputContainerClassName?: string; } & InputHTMLAttributes; export const Input = forwardRef( ( { id, label, helper, style, Icon, inputClassName, inputContainerClassName = "", disabled = false, onChange, mode, isInvalid = false, ...rest }, ref ) => { const [invalid, setInvalid] = useState(isInvalid); const onInternalChange = (e: ChangeEvent) => { if (mode === "auto") { setInvalid(e.currentTarget.checkValidity() !== true); } onChange?.(e); }; return (
{label && }
{Icon && (
)}
{helper && {helper}}
); } );