TextField

A text field allows a user to enter a plain text value.

Specialized text fields are available for different scenarios:

  • If you want a text field with type="search", use the SearchField component.
  • If you want a text field that shows/hides a password, use the PasswordField component.

Props

prefix?

An icon or text to display at the start of the input.

showCharacterCount?

True to display the number of remaining allowed characters on the right of the input. Requires a valid value in the "maxLength" prop.

Defaults to false.
isClearable?

Whether the TextField is clearable.

placeholder?

The placeholder text when the TextField is empty.

allowExceedingMaxLength?

This should only be used with the showCharacterCount prop. If true, the TextField will allow the text to go over the max length, but it will add an error look to the character count.

size?

The size of the TextField.

Defaults to md.
onClear?

Handler that is called when the clear button is pressed.

isFluid?

If true, the TextField will take all available width.

Defaults to false.
inputRef?

A ref for the HTML input element.

necessityIndicator?

Whether the required state should be shown as an asterisk or a label, which would display (Optional) on all non required field labels.

inputGroupProps?

The props for the InputGroup.

remainingCharacterCountProps?

The props for the RemainingCharacterCount.

style?

The inline style for the element. A function may be provided to compute the style based on component state.

pattern?

Regex pattern that the value of the input must match to be valid. See MDN.

validationBehavior?

Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.

Defaults to 'native'.
isDisabled?

Whether the input is disabled.

isReadOnly?

Whether the input can be selected but not changed by the user.

isRequired?

Whether user input is required on the input before form submission.

isInvalid?

Whether the value is invalid.

validate?

A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.

autoFocus?

Whether the element should receive focus on render.

value?

The current value (controlled).

defaultValue?

The default value (uncontrolled).

onChange?

Handler that is called when the value changes.

id?

The element's unique identifier. See MDN.

autoComplete?

Describes the type of autocomplete functionality the input should provide if any. See MDN.

maxLength?

The maximum number of characters supported by the input. See MDN.

minLength?

The minimum number of characters required by the input. See MDN.

type?

The type of input to render. See MDN.

inputMode?

Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN.

name?

The name of the input element, used when submitting an HTML form. See MDN.

className?

The CSS className for the element. A function may be provided to compute the class based on component state.

children?

The children of the component. A function may be provided to alter the children based on component state.

onFocus?

Handler that is called when the element receives focus.

onBlur?

Handler that is called when the element loses focus.

onFocusChange?

Handler that is called when the element's focus status changes.

onKeyDown?

Handler that is called when a key is pressed.

onKeyUp?

Handler that is called when a key is released.

onCopy?

Handler that is called when the user copies text. See MDN.

onCut?

Handler that is called when the user cuts text. See MDN.

onPaste?

Handler that is called when the user pastes text. See MDN.

onCompositionStart?

Handler that is called when a text composition system starts a new text composition session. See MDN.

onCompositionEnd?

Handler that is called when a text composition system completes or cancels the current text composition session. See MDN.

onCompositionUpdate?

Handler that is called when a new character is received in the current text composition session. See MDN.

onSelect?

Handler that is called when text in the input is selected. See MDN.

onBeforeInput?

Handler that is called when the input value is about to be modified. See MDN.

onInput?

Handler that is called when the input value is modified. See MDN.

aria-activedescendant?

Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.

aria-autocomplete?

Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made.

aria-haspopup?

Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.

aria-label?

Defines a string value that labels the current element.

aria-labelledby?

Identifies the element (or elements) that labels the current element.

aria-describedby?

Identifies the element (or elements) that describes the object.

aria-details?

Identifies the element (or elements) that provide a detailed, extended description for the object.

excludeFromTabOrder?

Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.

aria-errormessage?

Identifies the element that provides an error message for the object.

slot?

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Anatomy

Composed Components

A TextField uses the following components.

Examples

Disabled

A text field in a disabled state shows that an input field exists, but is not available in that circumstance. This can be used to maintain layout continuity and communicate that a field may become available later.

ReadOnly

The isReadOnly prop makes the text field's text content immutable. Unlike isDisabled, the text field remains focusable and the contents can still be copied. See the MDN docs for more information.

Error

A text field can be displayed in an error state to indicate that the user input is invalid.

Clearable

The isClearable prop can be set to true to display a clear button at the end of the input.

Sizes

Text fields have multiple sizes to choose from.

Labeling

If a visible label isn't specified, an aria-label must be provided to the text field for accessibility. If the field is labeled by a separate element, an aria-labelledby prop must be provided using the ID of the labeling element instead.

Description

A text field with a helper message.

Icon Prefix

An icon can be displayed at the start of the input.

Text Prefix

A short text can be displayed at the start of the input.

Character Count

A character count can be displayed at the end of the input. The character count is based on the maxLength prop. If the maxLength prop is not set, the character count will not be displayed.

Max Length Exceeded

To exceed the max length, set the allowExceedingMaxLength prop to true – default is false. When the character count is displayed, it turns red if the max length is exceeded.

Note: If allowExceedingMaxLength is set, a character count is required to be set as well.

Fluid

A text field can take the width of its container.

Migration Notes

Coming from Orbiter, you should be aware of the following changes:

  • disabled has been renamed isDisabled.
  • fluid has been renamed isFluid.
  • readOnly has been renamed isReadOnly.
  • There is no longer a loading state.
  • icon prop has been renamed to prefix.
  • Button props have been removed. To add a clear button, use the isClearable prop. For a more complex use case, create your own input using InputGroup.
  • wrapperProps no longer exists.
  • validationState has been changed to isInvalid.
  • showCharacterCount has been added.