UNPKG

@octopusdeploy/design-system-components

Version:
168 lines (167 loc) 6.72 kB
import * as React from "react"; import type { IconOnlyButtonElement, IconOnlyButtonProps } from "../../Button"; import type { PopoverBasicHelpProps } from "../../Popover"; import type { ValidationState } from "../Primitives/InputValidationMessage"; import type { DescriptionContent } from "../utils/descriptionWithLinks"; /** * The action buttons displayed inside a `TextField`, which allows up to three. `Select` * intentionally stays capped at two via `SelectActions`. */ export type TextFieldActions = readonly [] | readonly [IconOnlyButtonElement] | readonly [IconOnlyButtonElement, IconOnlyButtonElement] | readonly [IconOnlyButtonElement, IconOnlyButtonElement, IconOnlyButtonElement]; type BaseTextFieldProps<T> = { value?: T; /** * The label for the input field */ label: string; /** * The placeholder text to display when input is empty */ placeholder?: string; /** * The description text to display below the input */ description?: string | DescriptionContent; /** * Validation message to display. * * This will always be displayed as an error unless the component supports the `validationState` prop and changes it to `success`. */ validationMessage?: string; /** * The validation state, used to style the validation message and input border. * Only applied when a `validationMessage` is provided; defaults to `"error"` in that case. */ validationState?: ValidationState; /** * Whether the field is disabled */ disabled?: boolean; /** * Whether the field is required */ hasRequiredMarker?: boolean; /** * Whether to show optional marker */ hasOptionalMarker?: boolean; /** * Whether this field has a default value marker */ hasDefaultMarker?: boolean; /** * PopoverBasicHelp component to display additional help information */ popover?: React.ReactElement<PopoverBasicHelpProps>; /** * Whether to autofocus the input */ autoFocus?: boolean; /** * Whether the input is readonly */ readOnly?: boolean; /** * Whether the input is required */ required?: boolean; /** * The name attribute for the input */ name?: string; /** * The prefix to display before the input - can be a string or a icon it cannot be interactive */ prefix?: string | React.ReactElement; /** * The suffix to display after the input - can be a string or a button with ghost importance and medium size only */ suffix?: string | React.ReactElement<IconOnlyButtonProps>; /** * Up to three action buttons displayed inside the input field, after the value (and the clear button, if * shown). The three-action limit is enforced by the type. Pass raw icon-only `Button` elements with * ghost importance and `size="small"` */ actions?: TextFieldActions; /** * Ref to the underlying `<input>`. Provide this when an action needs to read or mutate the * input directly (e.g. inserting text at the caret); otherwise it can be omitted. */ inputRef?: React.RefObject<HTMLInputElement>; /** * The action to perform when the form control field is changed. */ onChange: (newValue: T) => void; /** * The action to perform when the input loses focus. */ onBlur?: (event: React.FocusEvent<HTMLInputElement>) => void; /** * When provided, a clear (✕) button is shown at the start of the suffix area while the * field has a value. Clicking it calls this handler (typically to reset the value). */ onClear?: () => void; }; type TextFieldPropsWithNumberType<T> = BaseTextFieldProps<T> & { /** * The input type must be "number" to use min, max, and step */ type: "number"; /** * The minimum value allowed */ min?: number; /** * The maximum value allowed */ max?: number; /** * The step value for the number input */ step?: number | "any"; }; type TextFieldPropsRemainingTypes<T> = BaseTextFieldProps<T> & { /** * The input type (text, email, password, etc.) */ type?: "text" | "email" | "password" | "tel" | "url" | "search" | "time"; /** * The minimum length allowed */ minLength?: number; /** * The maximum length allowed */ maxLength?: number; }; export type TextFieldProps<T> = TextFieldPropsWithNumberType<T> | TextFieldPropsRemainingTypes<T>; /** * TextField component * @remarks Only use in pre-approved areas, as in #project-form-uplift if unsure before using. * * @param props - TextFieldProps * @param props.label - The label for the input field * @param props.value - The current value of the input * @param props.type - The input type (text, email, password, etc.) * @param props.placeholder - The placeholder text to display when input is empty * @param props.description - The description text to display below the input * @param props.validationMessage - Validation message to display * @param props.validationState - The validation state ('error' | 'success'). Only applied when a `validationMessage` is provided; defaults to 'error' in that case. * @param props.disabled - Whether the field is disabled * @param props.hasRequiredMarker - Whether the field is required * @param props.hasOptionalMarker - Whether to show optional marker * @param props.hasDefaultMarker - Whether this field has a default value marker * @param props.popover - PopoverBasicHelp component to display additional help information e.g. <PopoverBasicHelp placement="right-start" description="A popover" /> * @param props.autoFocus - Whether to autofocus the input * @param props.readOnly - Whether the input is readonly * @param props.name - The name attribute for the input * @param props.prefix - The prefix to display before the input - can be a string or a icon it cannot be interactive * @param props.suffix - The suffix to display after the input - can be a string or a button with ghost importance and medium size only * @param props.onChange - The action to perform when the form control field is changed * @param props.onClear - A clear (✕) button is shown at the start of the suffix to clear the field. * @param props.actions - Raw icon-only `Button` elements displayed inside the field (max two recommended). Prebuilt/declarative actions live in `@octopusdeploy/design-system-octopus-components`. * * @returns TextField component */ export declare function TextField<T extends string | number | undefined>(props: TextFieldProps<T>): import("react/jsx-runtime").JSX.Element; export {};