import React, { useId, useState } from "react"; import { EyeClosedIcon, EyeIcon } from "@primer/octicons-react"; import { useTranslation } from "react-i18next"; import cn from "classnames"; import "./text-field.scss"; export interface TextFieldProps extends React.DetailedHTMLProps< React.InputHTMLAttributes, HTMLInputElement > { theme?: "primary" | "dark"; label?: string | React.ReactNode; hint?: string | React.ReactNode; textFieldProps?: React.DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; containerProps?: React.DetailedHTMLProps< React.HTMLAttributes, HTMLDivElement >; rightContent?: React.ReactNode | null; error?: string | React.ReactNode; } export const TextField = React.forwardRef( ( { theme = "primary", label, hint, textFieldProps, containerProps, rightContent = null, error, ...props }, ref ) => { const id = useId(); const [isFocused, setIsFocused] = useState(false); const [isPasswordVisible, setIsPasswordVisible] = useState(false); const { t } = useTranslation("forms"); const showPasswordToggleButton = props.type === "password"; const inputType = props.type === "password" && isPasswordVisible ? "text" : (props.type ?? "text"); const hintContent = error ? ( {error} ) : hint ? ( {hint} ) : null; const handleFocus: React.FocusEventHandler = (event) => { setIsFocused(true); props.onFocus?.(event); }; const handleBlur: React.FocusEventHandler = (event) => { setIsFocused(false); props.onBlur?.(event); }; const hasError = !!error; return (
{label && }
{showPasswordToggleButton && ( )}
{rightContent}
{hintContent}
); } ); TextField.displayName = "TextField";