MDUIDocs
Copy llms.txt linkCopy llms-full.txt linkView this page in MarkdownDiscuss this page with ChatGPTDiscuss full project docs with ChatGPT
Preset Colors
Custom Color
Extract from Wallpaper
Please select a wallpaper
Getting Started
AI-Assisted Development
Styles
Integration with Frameworks
Components
Avatar Badge BottomAppBar Button ButtonIcon Card Checkbox Chip CircularProgress Collapse Dialog Divider Dropdown Fab Icon Layout LinearProgress List Menu NavigationBar NavigationDrawer NavigationRail Radio RangeSlider Select SegmentedButton Slider Snackbar Switch Tabs TextField Tooltip TopAppBar
Functions
Libraries

TextField

Text fields, typically used in forms and dialogs, allow users to input text.

Usage

Import the component:

import 'mdui/components/text-field.js';

Import the TypeScript type:

import type { TextField } from 'mdui/components/text-field.js';

Example:

<mdui-text-field label="Text Field"></mdui-text-field>

Examples

Variant

The variant attribute modifies the shape of the text field.

Helper Text

Use the label attribute to display a label above the text field.

Use the placeholder attribute to display placeholder text when there is no value.

Use the helper attribute or helper slot to display helper text at the bottom of the text field. To display the helper text only when the input is focused, use the helper-on-focus attribute.

Clearable

The clearable attribute displays a clear button on the right when the text field has a value.

End-Aligned Text

The end-aligned attribute aligns the text to the right.

Prefix, Suffix, Icons

The icon and end-icon attributes or slots add Material Icons to the left and right of the text field, respectively.

The prefix and suffix attributes or slots add text to the left and right of the text field. They are displayed only when the text field is focused or has a value.

Readonly

The readonly attribute makes the text field read-only.

Disabled

The disabled attribute disables the text field.

Multi-line Text Field

The rows attribute specifies the number of rows for a multi-line text field.

To automatically adjust the height of the text field based on the length of the input, use the autosize attribute. The min-rows and max-rows attributes specify the minimum and maximum number of rows.

Character Counter

The maxlength attribute sets the maximum number of characters for the text field. To display a character counter below the text field, use the counter attribute.

Password Field

For password fields (type="password"), the toggle-password attribute adds a button on the right to toggle the visibility of the password.

API

Properties

AttributePropertyReflectTypeDefault
variantvariant'filled' | 'outlined''filled'

Defines the text field variant. Default is filled. Possible values:

  • filled: Text field with a background color for stronger visual emphasis.
  • outlined: Text field with a border for subtler visual emphasis.
typetype'text' | 'number' | 'password' | 'url' | 'email' | 'search' | 'tel' | 'hidden' | 'date' | 'datetime-local' | 'month' | 'time' | 'week''text'

Specifies the text field type. Default is text. Possible values:

  • text: Standard text field.
  • number: Allows numeric input only. Virtual keyboards on mobile devices show a numeric layout.
  • password: Hides the password as you type.
  • url: Validates URL format. Virtual keyboards on mobile devices show a URL-specific layout.
  • email: Validates email format. Virtual keyboards on mobile devices show an email-specific layout.
  • search: Shows a search icon on the Enter key in virtual keyboards.
  • tel: Displays a phone keypad on virtual keyboards.
  • hidden: Hides the control, but its value is still submitted to the server.
  • date: Opens a date picker or a numeric scroll wheel for year, month, and day in supported browsers.
  • datetime-local: Activates a date and time picker in supported browsers, without a time zone.
  • month: Allows entering a year and month without a time zone.
  • time: Allows time input without a time zone.
  • week: Allows entering a year and week without a time zone.
namenamestring''

The name of the text field, which is submitted with form data.

valuevaluestring''

The value of the text field, which is submitted with form data.

defaultValuestring''

The default value. The text field resets to this value on form reset. JavaScript only.

labellabelstring-

Label text.

placeholderplaceholderstring-

Placeholder text.

helperhelperstring-

The helper text displayed at the bottom of the text field. Alternatively, use slot="helper".

helper-on-focushelperOnFocusbooleanfalse

Shows the helper text only when the text field is focused.

clearableclearablebooleanfalse

Makes the text field clearable.

clear-iconclearIconstring-

Material Icons name shown on the right when the text field is clearable. Alternatively, use slot="clear-icon".

end-alignedendAlignedbooleanfalse

Aligns the text to the right.

prefixprefixstring-

The prefix text for the text field. It is only displayed when the text field is focused or has a value. Alternatively, use slot="prefix".

suffixsuffixstring-

The suffix text for the text field. It is only displayed when the text field is focused or has a value. Alternatively, use slot="suffix".

iconiconstring-

Material Icons name for the prefix icon of the text field. Alternatively, use slot="icon".

end-iconendIconstring-

Material Icons name for the suffix icon of the text field. Alternatively, use slot="end-icon".

error-iconerrorIconstring-

Material Icons name displayed on the right side of the text field when form field validation fails. Alternatively, use slot="error-icon".

formformstring-

Associates the text field with a <form> element. Set this to the id of a <form> in the same document. If omitted, the text field uses its parent <form>, if any.

This lets the text field work with any form in the document, not just the one it is nested in.

readonlyreadonlybooleanfalse

Makes the text field read-only.

disableddisabledbooleanfalse

Disables the text field.

requiredrequiredbooleanfalse

The field must be filled out before the form is submitted.

rowsrowsnumber-

The number of rows in the text field.

autosizeautosizebooleanfalse

Automatically adjusts the height of the text field based on its content.

min-rowsminRowsnumber-

The minimum number of rows when autosize is enabled.

max-rowsmaxRowsnumber-

The maximum number of rows when autosize is enabled.

minlengthminlengthnumber-

The minimum number of characters for input.

maxlengthmaxlengthnumber-

The maximum number of characters for input.

countercounterbooleanfalse

Displays the character count when maxlength is specified.

minminnumber-

The minimum value when type is number.

maxmaxnumber-

The maximum value when type is number.

stepstepnumber-

The step interval for increment/decrement when type is number.

patternpatternstring-

The regular expression for form validation.

toggle-passwordtogglePasswordbooleanfalse

Adds a toggle button to show or hide the password when type is password.

show-password-iconshowPasswordIconstring-

Material Icons name for the visible password toggle button. Alternatively, use slot="show-password-icon".

hide-password-iconhidePasswordIconstring-

Material Icons name for the hidden password toggle button. Alternatively, use slot="hide-password-icon".

autocapitalizeautocapitalize'none' | 'sentences' | 'words' | 'characters'-

A non-standard iOS attribute for automatic capitalization. Possible values:

  • none: Disables automatic capitalization.
  • sentences: Capitalizes the first letter of each sentence.
  • words: Capitalizes the first letter of each word.
  • characters: Capitalizes all letters.
autocorrectautocorrectstring-

The autocorrect attribute of the input element.

autocompleteautocompletestring-

The autocomplete attribute of the input element.

enterkeyhintenterkeyhint'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'-

Customizes the Enter key text or icon on the virtual keyboard. The effect varies by device and language. Possible values:

  • enter: Inserts a new line, typically used in a multi-line text field.
  • done: Indicates completion and closes the virtual keyboard.
  • go: Navigates to the target of the entered text.
  • next: Moves to the next text field.
  • previous: Moves to the previous text field.
  • search: Navigates to search results.
  • send: Sends a text message.
spellcheckspellcheckbooleanfalse

Enables spell checking.

inputmodeinputmode'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url'-

Customizes the virtual keyboard. Possible values:

  • none: No virtual keyboard. Useful for custom input controls.
  • text: Standard text input keyboard.
  • decimal: Decimal input keyboard. This includes a period . or comma , and numbers.
  • numeric: Numeric keyboard. This displays numbers 0–9.
  • tel: Phone number keyboard. This includes numbers 0–9, asterisk *, and hash # keys.
  • search: Search-optimized virtual keyboard. 'Search' appears on the submit button.
  • email: Email-optimized virtual keyboard. This typically includes @ and ..
  • url: URL-optimized virtual keyboard. This typically includes ., /, and #.
validityValidityState-

A ValidityState object that represents the element's validity states.

validationMessagestring-

Validation message. Empty string when valid.

valueAsNumbernumber-

Gets or sets the value as a number. Returns NaN if the value cannot be converted.

autofocusautofocusbooleanfalse

Whether the element is focused when the page loads.

tabindextabIndexnumber-

The element's tab order when navigating with the Tab key.

Methods

NameParametersReturns
select
void

Selects the content of the text field.

setSelectionRange
  • start: number
  • end: number
  • direction: 'forward' | 'backward' | 'none'
void

Selects a specific range in the text field.

setRangeText
  • replacement: string
  • start: number
  • end: number
  • selectMode: 'select' | 'start' | 'end' | 'preserve'
void

Replaces a specific range in the text field with new text.

checkValidity
boolean

Checks the validity of the form field. If it is invalid, it triggers an invalid event and returns false. If valid, it returns true.

reportValidity
boolean

Checks the validity of the form field. If it is invalid, it triggers an invalid event, returns false, and displays a validation message. If valid, it returns true.

setCustomValidity
  • message: string
void

Sets a custom error message. If the message is non-empty, the field is considered invalid.

click
void

Simulates a mouse click on the element.

focus
  • options: FocusOptions (Optional)
void

Sets focus on the element. An optional object parameter may include a preventScroll property. If preventScroll is set to true, the page will not scroll to bring the focused element into view.

blur
void

Removes focus from the element.

Events

Name
focus

Emitted when the text field gains focus.

blur

Emitted when the text field loses focus.

change

Emitted when the value changes and the text field loses focus.

input

Emitted when the value changes.

invalid

Emitted when the form control's validity is checked and it does not meet the constraints.

clear

Emitted when the clear button is clicked. Can be prevented with event.preventDefault().

Slots

Name
icon

Icon on the left side.

end-icon

Icon on the right side.

error-icon

Icon on the right side for validation failure.

prefix

Text on the left side.

suffix

Text on the right side.

clear-button

Clear button.

clear-icon

Icon in the clear button.

toggle-password-button

Button to toggle password visibility.

show-password-icon

Icon in the password visibility toggle button (show password state).

hide-password-icon

Icon in the password visibility toggle button (hide password state).

helper

Helper text shown below the field.

CSS Parts

Name
container

Container for the text field.

icon

Icon on the left side.

end-icon

Icon on the right side.

error-icon

Icon on the right side for validation failure.

prefix

Text on the left side.

suffix

Text on the right side.

label

Label text above the field.

input

Internal <input> or <textarea> element.

clear-button

Clear button.

clear-icon

Icon in the clear button.

toggle-password-button

Button to toggle password visibility.

show-password-icon

Icon in the password visibility toggle button (show password state).

hide-password-icon

Icon in the password visibility toggle button (hide password state).

supporting

Container for supporting text at the bottom of the field, including helper, error, and counter.

helper

Helper text shown below the field.

error

Text at the bottom for error.

counter

Character count in the bottom right corner.

On this page