Skip to content

TextInput

A text input is a form element that lets users input and edit a single-line text value.

Guidelines

When to use text inputs

Through text input fields, users can input text, numbers, symbols or mixed-format strings (unless specifically restricted).

Use the TextInput component in forms when the user’s answer to a prompt can not easily be predicted, or when there is significant variability in potential inputs. Text inputs should also be used when other form components prove more difficult (require additional steps) to achieve the same result (e.g. selecting a date from a calendar versus typing in the date). The text input used for search queries, known as the search type, is documented in SearchInput.

You can provide autocomplete options that are tailored to the user’s input by using Lookup.

Specifications

Specification of TextInput.

  1. Icon (optional)
    Icons can add to simple identification of specific user inputs. Examples are 'search' magnifying glass for search bars or 'user avatar' for user login input fields.
  2. Placeholder text (optional)
    The placeholder text provides an example of what type of information is being requested in the input. The placeholder text should further illustrate and support the text input label. However, it should never be the only input description.
  3. End icon (optional)
    A secondary end icon can be included if needed.
  4. Clear button (optional)
    A 'clear' indicator can be included to remove the typed content within the input field.

Component limitations

The base min-width for the TextInput component is set at @size-1600 (equivalent to 256px in the default Codex theme), but it can be customized to a smaller width if needed. There is no maximum width limit.

If the text entered in the input exceeds the available space, it will overflow horizontally.

Refer to the TextInput component in Codex Figma.

Types

The text input can accommodate the following types of inputs:

Input types: Text, Search, Number, Email, Password, Telephone, URL, Week, Month, Date, Date and time, and Time.

  1. Text
  2. Search
  3. Number
  4. Email
  5. Password
  6. Telephone
  7. URL
  8. Week
  9. Month
  10. Date
  11. Date and time
  12. Time

Interaction states

Text inputs have the following visually separate states:

States of the Text Input component: default, hover, active-focus, filled, disabled, read-only, error default, error focus, and error filled.

  1. Default
  2. Hover
  3. Active - Focus
  4. Filled
  5. Disabled
  6. Read-only
  7. Error default
  8. Error hover
  9. Error focus
  10. Error filled

Best practices

Consider the following recommendations when using text inputs.

Usage

TextInput included within a Field.

Do:
  • Integrate the TextInput within a Field component to use all available properties of Field, such as label, helper text, and error messages.

Standalone TextInput.

Don't:
  • Use standalone TextInput outside of a Field component.
  • Use TextInputs without a label, as the label is essential for accessibility and ease of scanning.

Icon

TextInput with a recognizable icon.

Do:
  • Use a start icon that meets the input's requirements.

TextInput with a complex icon.

Don't:
  • Use icons that are difficult to understand or do not clearly convey their purpose.

Keyboard navigation

KeyFunction
Left arrow / Right arrowThe left and right arrows navigate through the text content within the input.
Up arrow / Down arrowUp arrow moves the focus from the last position of the input to the first one, while down arrow moves it from the first position to the last.

Demos

Configurable

NameValue
Props
startIcon
endIcon
clearable
status
disabled
readonly
placeholder
View
Reading direction
Note: For icon properties, the relevant icon also needs to be imported from the @wikimedia/codex-icons package. See the usage documentation for more information.

Default

This simple example demonstrates how to bind a reactive reference to the input's value via v-model. The reactive reference will automatically update when the input value changes.

Open up the browser console to see events emitted on input, change, focus, and blur.

Input value:

In this example, the parent component sets an initial value and has a reset button that will restore that initial value on click.

Input value: Initial value

Input type

Use the inputType prop to set the native <input> type attribute. Some inputType values will result in a browser provided UI like the date inputType provided below.

Input value:

Clearable

Including the clearable prop will add a clear button to the end of the icon when the input is not empty. On click, the clear button will set the input value to an empty string.

Input value:

With icons

A TextInput can have a startIcon, an endIcon, or both. Any Codex icon can be used.

Note that a clearable TextInput with an endIcon will display both the clear button and the endIcon when the input is not empty. To see this behavior, type in the input below.

With placeholder

To add placeholder text, add a placeholder attribute.

Disabled

To disable the input, add the disabled attribute.

Form field

A TextInput can be wrapped in the Field component to add features like a semantic label, description and help text, validation messages, and more. See the Field page for more information.

Enter your signature using plain text or wikitext
Use "~~~~" on talk pages to include your signature and a timestamp

Vue usage

v-model is used to track the current value of the input. See the events docs for details on emitted events and their properties.

Attributes passed to input

This component will pass any HTML attributes applied to it, except for CSS class, to the <input> element within the component.

Props

Prop nameDescriptionTypeValuesDefault
modelValueCurrent value of the input.

Provided by v-model binding in the parent component.
string|number-''
inputTypetype attribute of the input.TextInputType'text', 'search', 'number', 'email', 'password', 'tel', 'url', 'week', 'month', 'date', 'datetime-local', 'time''text'
statusstatus attribute of the input.ValidationStatusType-'default'
disabledWhether the input is disabled.boolean-false
startIconAn icon at the start of the input element. Similar to a ::before pseudo-element.Icon|undefined-undefined
endIconAn icon at the end of the input element. Similar to an ::after pseudo-element.Icon|undefined-undefined
clearableAdd a clear button at the end of the input element.

When the clear button is pressed, the input's value is set to an empty string. The clear button is displayed when input text is present.
boolean-false

Methods

Method nameDescriptionSignature
focusFocus the component's input element.Returns: void
blurBlur the component's input element.Returns: void

Events

Event namePropertiesDescription
update:modelValuemodelValue string|number - The new model valueWhen the input value changes
keydownundefined KeyboardEventWhen the user presses a key.

This event is not emitted when the user presses the Home or End key (T314728), but is emitted for Ctrl/Cmd+Home and Ctrl/Cmd+End.
inputevent InputEventWhen the input value changes via direct use of the input
changeevent EventWhen an input value change is committed by the user (e.g. on blur)
focusevent FocusEventWhen the input comes into focus
blurevent FocusEventWhen the input loses focus
clearevent MouseEventWhen the input value is cleared through the use of the clear button

CSS-only version

Markup structure

The CSS-only TextInput component consists of a <div> wrapping an <input> element.

With icons

You can use CSS-only icons to add start and end icons to the input.

You'll need the following CSS classes on the root element:

  • Start icon: .cdx-text-input--has-start-icon
  • End icon: .cdx-text-input--has-end-icon

The icons will be <span> elements with the .cdx-text-input__icon class, plus:

  • Start icon: .cdx-text-input__start-icon
  • End icon: .cdx-text-input__end-icon

You will need to add your own CSS classes to set the icon styles and background image.

Disabled

Add the disabled attribute to the <input> element for a disabled text input.

Error state

Add the .cdx-text-input--status-error class to the root element to show error styles.