import { cx } from "class-variance-authority" import { type ForwardedRef, forwardRef, useId, useImperativeHandle, useRef, } from "react" import { Input as AriaInput, Label as AriaLabel } from "react-aria-components" import { InputLabel } from "../InputLabel" import styles from "./input.module.css" import { MaterialIcon } from "../Icons/MaterialIcon" import { Typography } from "../Typography" import type { InputProps } from "./types" import { clearInput, useInputHasValue } from "./utils" const InputComponent = forwardRef(function AriaInputWithLabelComponent( { label, labelPosition = "floating", leftIcon, rightIcon, // eslint-disable-next-line @typescript-eslint/no-unused-vars onRightIconClick, showClearContentIcon, placeholder, id, required, "data-validation-state": validationState, ...props }: InputProps & { "data-validation-state"?: string }, ref: ForwardedRef ) { // Create an internal ref that we can access const internalRef = useRef(null) // Generate a unique ID for the input // This is used to ensure the input is properly associated with the label // when the label is positioned above the input const generatedId = useId() // Forward the ref properly useImperativeHandle(ref, () => internalRef.current!, []) // Track whether input has a value (for showing/hiding clear button) const hasValue = useInputHasValue(props.value, internalRef) const onClearContent = () => { clearInput({ inputRef: internalRef, onChange: props.onChange, value: props.value, }) } // When labelPosition is 'top', restructure to have label outside container // We need an ID for proper label-input association if (labelPosition === "top") { const inputId = id || generatedId return ( <> {label}
{leftIcon && (
{leftIcon}
)} {showClearContentIcon && hasValue && (
)} {rightIcon && !(showClearContentIcon && hasValue) && (
{rightIcon}
)}
) } // Floating label (default behavior) - label inside container return (
{leftIcon &&
{leftIcon}
} {label} {showClearContentIcon && hasValue && (
)} {rightIcon && !(showClearContentIcon && hasValue) && (
{rightIcon}
)}
) }) export const Input = InputComponent as React.ForwardRefExoticComponent< InputProps & React.RefAttributes >