import clsx from 'clsx'; import { forwardRef, FocusEvent, useState, Ref } from 'react'; import { Controller } from 'react-hook-form'; import { PatternFormat, PatternFormatProps } from 'react-number-format'; import { InputProps } from '../types'; import { twMerge } from 'tailwind-merge'; const PatternFormatWithRef = forwardRef( (props: PatternFormatProps, ref: Ref) => { return ; } ); PatternFormatWithRef.displayName = 'PatternFormatWithRef'; export const Input = forwardRef< HTMLInputElement, InputProps & Pick< PatternFormatProps, 'mask' | 'allowEmptyFormatting' | 'onValueChange' > & { format?: string; defaultValue?: string; type?: string; } >((props, ref) => { const [focused, setFocused] = useState(false); const [hasValue, setHasValue] = useState(false); const { id, label, labelStyle, error, mask, format, required = false, ...rest } = props; const hasFloatingLabel = label && labelStyle === 'floating'; const inputClass = twMerge( clsx( 'text-xs border px-2.5 h-10 placeholder:text-gray-600 peer', 'focus-visible:outline-none', // disable outline on focus error ? 'border-error focus:border-error' : 'border-gray-500 hover:border-black focus:border-black' ), props.className ); const inputProps: any = { id, ref, className: inputClass, onFocus: () => setFocused(true), onBlur: (event: FocusEvent) => { setFocused(false); setHasValue(!!event.target.value); } }; const Label = () => { if (!label) return null; return ( ); }; return (
{props.format ? ( <> {labelStyle !== 'floating' &&
{error && ( {String(error.message)} )}
); }); Input.displayName = 'Input';