TextInput β
A TextInput is a form element that lets users input and edit a single-line text value.
Name | Value |
---|---|
Props | |
startIcon | |
endIcon | |
clearable | |
inputType | Choose an option |
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. |
Overview β
When to use TextInput β
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. For search queries, use SearchInput instead.
You can provide autocomplete options that are tailored to the userβs input by using Lookup.
About TextInput β
With a TextInput, users can input text, numbers, symbols, or mixed-format strings like dates or email addresses. TextInput includes the following elements.
Start icon (optional) β
A start icon can be included to visually enhance the input's purpose. For example, the "user avatar" icon might be used in a username field.
- Use a simple icon that is easily understandable to users.
Input β
The following input types can be used:
- Text (default)
- Search
- Number
- Password
- Telephone
- URL
- Week
- Month
- Date
- Date and time
- Time
Placeholder text (optional) β
Placeholder text provides an example of what type of information is being requested in the input.
- Placeholder text should further illustrate and support the TextInput's label.
- Placeholder text should never replace the label nor be the input's only description.
End icon (optional) β
A secondary end icon can be included if needed.
Clear button (optional) β
A 'clear' button can be included to remove the typed content within the input field.
Examples β
Basic usage β
Developer notes
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 review events emitted on input, change, focus, and blur.
With initial value β
Input type β
Any of the native input types can be used. Some types may result in a browser-provided user interface, like a date picker for a date input.
Input value:
Developer notes
Use the inputType
prop to set the native <input>
type attribute.
Clearable β
A clearable TextInput will have a clear button when there is text in the input. When clicked, the clear button will set the input value to an empty string.
With icons β
A TextInput can have a start icon, end icon, or both. Any Codex icon can be used.
- Use simple icons that are easily understandable to users.
Note that a clearable TextInput with an end icon will display both the clear button and the end icon when the input is not empty. To review this behavior, type in the input below.
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. Refer to the Field page for more information.
Native validation β
The TextInput component exposes native constraint validation methods. Refer to the methods below to review all of the supported features.
This demo sets the input type to "email" and validates the input on blur. When the input is invalid, it sets the Field's status to "error" and passes the native validation message to the Field component for display.
Technical implementation β
Vue usage β
v-model
is used to track the current value of the input. Refer to the events table 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 name | Description | Type | Values | Default |
---|---|---|---|---|
modelValue | Current value of the input. Provided by v-model binding in the parent component. | string|number | - | '' |
inputType | type attribute of the input. | TextInputType | 'text' , 'search' , 'number' , 'email' , 'password' , 'tel' , 'url' , 'week' , 'month' , 'date' , 'datetime-local' , 'time' | 'text' |
status | status attribute of the input. | ValidationStatusType | - | 'default' |
disabled | Whether the input is disabled. | boolean | - | false |
startIcon | An icon at the start of the input element. Similar to a ::before pseudo-element. | Icon|undefined | - | undefined |
endIcon | An icon at the end of the input element. Similar to an ::after pseudo-element. | Icon|undefined | - | undefined |
clearable | Add 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 name | Description | Signature |
---|---|---|
focus | Focus the component's input element. | Returns: void |
blur | Blur the component's input element. | Returns: void |
checkValidity | Check the validity of the input element according to its constraint attributes. Emits an 'invalid' event if the input is invalid. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/checkValidity | Returns: boolean Whether the input is valid |
reportValidity | Check the validity of the input element and report it as a pop up on the UI. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/reportValidity | Returns: boolean Whether the input is valid |
setCustomValidity | Set custom validity and message for the input element. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity | Params:
void |
Events β
Event name | Properties | Description |
---|---|---|
update:modelValue | modelValue string|number - The new model value | When the input value changes |
keydown | undefined KeyboardEvent | When 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. |
input | event InputEvent | When the input value changes via direct use of the input |
change | event Event | When an input value change is committed by the user (e.g. on blur) |
focus | event FocusEvent | When the input comes into focus |
blur | event FocusEvent | When the input loses focus |
clear | event MouseEvent | When the input value is cleared through the use of the clear button |
invalid | event Event | When the input value is invalid according to the input's constraint attributes (e.g. min, max, pattern). See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/invalid_event |
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. This also works for the readonly
attribute.
Error state β
Add the .cdx-text-input--status-error
class to the root element to show error styles.
Keyboard navigation β
Key | Function |
---|---|
Left arrow / Right arrow | The left and right arrows navigate through the text content within the input. |
Up arrow / Down arrow | Up 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. |