import React, { InputHTMLAttributes, forwardRef } from "react"; import { BodyShort, ErrorMessage, Label } from "../../typography"; import { omit } from "../../utils-external"; import { cl } from "../../utils/helpers"; import { ReadOnlyIcon } from "../ReadOnlyIcon"; import { FormFieldProps, useFormField } from "../useFormField"; export interface TextFieldProps extends FormFieldProps, Omit, "size"> { /** * Controlled value */ value?: string | number; /** * Defaults input-value without needing controlled-state */ defaultValue?: string | number; /** * Exposes the HTML size attribute */ htmlSize?: number; /** * If enabled shows the label and description for screenreaders only */ hideLabel?: boolean; /** * TextField label */ label: React.ReactNode; /** * Type of form control. Picking the correct type helps user fill inn their required information * @default "text" */ type?: "email" | "number" | "password" | "tel" | "text" | "url" | "time"; } /** * A component that displays a text input field with a label. * * @see [📝 Documentation](https://aksel.nav.no/komponenter/core/textfield) * @see 🏷️ {@link TextFieldProps} * * @example * ```jsx * * ``` */ export const TextField = forwardRef( (props, ref) => { const { inputProps, errorId, showErrorMsg, hasError, size, inputDescriptionId, } = useFormField(props, "textField"); const { label, className, description, htmlSize, hideLabel = false, type = "text", readOnly, ...rest } = props; return ( {readOnly && } {label} {!!description && ( {description} )} {showErrorMsg && ( {props.error} )} ); }, ); export default TextField;