Text fields

Usage

Text fields function similarly to <input> elements, serving as containers with labels to facilitate user input.

<TextField label="Label" value="Value" />
<TextField label="Label" value="Value" variant="outlined" />

Input type

The type attribute of a text field changes how the text field works, such as displaying a different keyboard or providing default validation.

  • type="text" (default)
  • type="email"
  • type="password"
  • type="url"
  • type="number"
  • type="search"
  • type="tel"
  • type="textarea"
<TextField label="Email" type="email" variant="outlined" />
<TextField label="Password" type="password" variant="outlined" />

Disabled

<TextField label="Label" disabled value="Value" />
<TextField label="Label" disabled value="Value" variant="outlined" />

Labels

<TextField label="Country" variant="outlined" />
<TextField placeholder="email@domain.com" variant="outlined" />
<label id="city-label">
	City
	<TextField variant="outlined" aria-labelledby="city-label" />
</label>
bookmark Nesting text fields in labels without aria-labelledby is not currently supported. If you want to avoid using an id, you can use aria-label instead.

Textarea

<TextField label="Resize" type="textarea" />
<TextField label="Resize" type="textarea" variant="outlined" />

Icons

<TextField placeholder="Search" type="search" variant="outlined">
	{#snippet start()}<Icon>search</Icon>{/snippet}
</TextField>
<TextField label="Password" type="password" variant="outlined">
	{#snippet end()}
		<IconButton toggle title="Toggle visibility">
			{#snippet selectedIcon()}
				<Icon>visibility_off</Icon>
			{/snippet}
			<Icon>visibility</Icon>
		</IconButton>
	{/snippet}
</TextField>
<TextField label="Username" variant="outlined" error>
	{#snippet end()}
		<Icon>error</Icon>
	{/snippet}
</TextField>

Prefix and suffix

<TextField
	label="Amount"
	value={0}
	prefixText="$"
	suffixText=".00"
	type="number"
	variant="outlined"
/>

Supporting text

<TextField label="Username" supportingText="Your username is your unique identifier." />
<TextField
	label="Email"
	type="email"
	inputmode="email"
	required
	supportingText="Email is required"
/>

Character counter

<TextField label="Name" maxlength={10} />

Validation

Constraint validation

<script>
	const reportValidity = (event: Event) => {
		const textField = event.target as HTMLInputElement
		textField.reportValidity()
	}
</script>
<form>
	<TextField label="First name" required onchange={reportValidity} />
	<TextField
		label="Last name"
		required
		pattern="[a-zA-Z]+"
		supportingText="Characters only"
		onchange={reportValidity}
	/>
	<Button>Submit</Button>
</form>

Manual validation

<TextField label="Username" error value="eric20" errorText="Username is not available" />

API

Attributes

AttributeTypeDefaultDescription
type'text' | 'password' | 'email' | 'number' | 'search' | 'tel' | 'url' | 'textarea''text'Specifies the type of the field.
variant'outlined' | 'filled''filled'Visual appearance
labelstring | undefinedundefinedLabel of the text field.
supportingTextstring''Provides additional information below the text field, such as usage guidelines.
startSnippet | undefinedundefinedIcon displayed at the beginning of the text field.
endSnippet | undefinedundefinedIcon displayed at the end of the text field.
disabledbooleanfalseDisables the text field.
noAsteriskbooleanfalseDisables the asterisk on the floating label when the text field is required.
errorbooleanfalseGets or sets whether or not the text field is in a visually invalid state.
errorTextstring''The error message that replaces supporting text when error is true. If errorText is an empty string, then the supporting text will continue to show.
prefixTextstring''An optional prefix to display before the input value.
suffixTextstring''An optional suffix to display after the input value.

Bindables

AttributeTypeDescription
valueanyValue of the input or textarea.
elementHTMLElementA reference to the root DOM element of the component. This variable is bound using bind:this, allowing direct access to the underlying HTML element for manipulation or querying within the component's logic.