"use client"; import React, { ForwardedRef, forwardRef, useEffect, useRef } from "react"; import { fieldBackgroundDisabledMixin, fieldBackgroundHoverMixin, fieldBackgroundInvisibleMixin, fieldBackgroundMixin, focusedInvisibleMixin, } from "../styles"; import { InputLabel } from "./InputLabel"; import { cls } from "../util"; export type InputType = | "text" | "number" | "phone" | "email" | "password" | "search" | "url" | "date" | "time" | "datetime-local" | "month" | "week" | "color"; export type TextFieldProps = { type?: InputType; value?: T; onChange?: (event: React.ChangeEvent) => void; label?: React.ReactNode; multiline?: boolean; disabled?: boolean; invisible?: boolean; error?: boolean; endAdornment?: React.ReactNode; autoFocus?: boolean; placeholder?: string; size?: "smallest" | "small" | "medium" | "large"; className?: string; style?: React.CSSProperties; inputClassName?: string; inputStyle?: React.CSSProperties; inputRef?: React.ForwardedRef; /** * Maximum number of rows to display. */ maxRows?: number | string; /** * Minimum number of rows to display. * @default 1 */ minRows?: number | string; } & Omit, "size">; export const TextField = forwardRef>( ( { value, onChange, label, type = "text", multiline = false, invisible, maxRows, minRows, disabled, error, endAdornment, autoFocus, placeholder, size = "large", className, style, inputClassName, inputStyle, inputRef: inputRefProp, ...inputProps }: TextFieldProps, ref: ForwardedRef ) => { const inputRef = inputRefProp ?? useRef(null); const [focused, setFocused] = React.useState(false); const hasValue = value !== undefined && value !== null && value !== ""; useEffect(() => { // @ts-ignore if (inputRef.current && document.activeElement === inputRef.current) { setFocused(true); } }, []); useEffect(() => { if (type !== "number") return; const handleWheel = (event: any) => { if (event.target instanceof HTMLElement) event.target.blur(); }; const element = "current" in inputRef ? inputRef.current : inputRef; element?.addEventListener("wheel", handleWheel); return () => { element?.removeEventListener("wheel", handleWheel); }; }, [inputRef, type]); const input = multiline ? (