Text field
A single-line text input with its label, hint and error built in.
Example
<div class="stack"> <focsd-text-field label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></focsd-text-field> <focsd-text-field label="Billing email" type="email" autocomplete="email"></focsd-text-field> <focsd-text-field label="Coupon code" value="AUTUMN" error="This code expired on October 1. Try a current code."></focsd-text-field> <focsd-text-field label="Username" value="northwind" success="That username is free."></focsd-text-field> <focsd-text-field label="Support phone" value="555 0142" warning="Add a country code so overseas customers can call."></focsd-text-field> <focsd-text-field label="Display name" hint="Letters, numbers and spaces." warning-pattern="[A-Za-z0-9 ]*" warning-message="Some apps cannot show special characters. You can still save it."></focsd-text-field></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-text-field.js';</script>
<focsd-text-field label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></focsd-text-field>import { TextField } from '@focsd-dev/react';
export function Example() { return <TextField onChange={(e) => console.log(e.target)} label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></TextField>;}<script setup>import '@focsd-dev/components/focsd-text-field.js';</script>
<template> <focsd-text-field @change="onChange" label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></focsd-text-field></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-text-field.js';
// template<focsd-text-field (change)="onChange($event)" label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></focsd-text-field><script> import '@focsd-dev/components/focsd-text-field.js';</script>
<focsd-text-field onchange={(e) => console.log(e.target)} label="Workspace name" value="Northwind Studio" hint="Shown on invoices and invitations." required></focsd-text-field>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | — |
type | type | 'text' | 'email' | 'password' | 'search' | 'tel' | 'url' | text |
placeholder | placeholder | string | — |
autocomplete | autocomplete | AutoFill | — |
minlength | minlength | number | — |
maxlength | maxlength | number | — |
pattern | pattern | string | undefined | — |
size | size | number | undefined | — |
dirname | dirname | string | undefined | — |
inputmode | inputMode | — | — |
enterkeyhint | enterKeyHint | — | — |
autocapitalize | autocapitalize | — | — |
autocorrect | autocorrect | — | — |
spellcheck | spellcheck | — | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
select() | Select all text in the input, like native. |
setSelectionRange(start, end, direction) | Set the text selection in the input, like native. |
setRangeText(replacement, start, end, selectMode) | Replace text in the control (like the native method); updates value without firing input. |
Events
| Event | Description |
|---|---|
input | — |
change | Fired when the user commits a change to the value or state. |
CSS parts
Style the inside of the component with ::part(), for example focsd-text-field::part(layout).
| Part | Description |
|---|---|
layout | The box around the label and the body; `data-position` is the label position. |
label | The label element. |
body | The control and its messages. |
control | The control element. |
hint | The hint element. |
message | The shown message; also `error`, `warning` or `success` for its kind. |
message-icon | The icon in the message. |
announcer | The visually hidden status region that announces a new message. |
Accessibility
- Native parity:
minlength,maxlength,pattern,size,inputmode,enterkeyhint,autocapitalize,autocorrect,spellcheck,dirname,readonly,setCustomValidity()and the selection APIs (select(),setSelectionRange(),setRangeText()). Give personal-data fields anautocompletetoken (WCAG 1.3.5). Not supported:list(datalist), because the ID reference cannot cross into shadow DOM. Usefocsd-combobox. - The label, hint and error render in the same shadow root as the
<input>, soforandaria-describedbyalways resolve. - An
errorsetsaria-invalidand is read with the field; native constraint failures are announced after a failed submit. - Hints and messages are part of the description; a new message is announced politely.
- Only one of
error,warningandsuccessshows, in that order.warningandsuccessnever block submission. - Built-in validation:
error-messageis the text shown when the field failsrequired,type,pattern,minand the like (on submit, orvalidate()); it clears once the value is fixed.warning-patternwithwarning-messageshows a non-blocking warning while the value does not match. Inside a form withfocsd-error-summary, no script is needed. - Never use the placeholder as the label.
- Form-associated: participates in
FormData, validation, reset and fieldsetdisabledlike a native control.
Keyboard
| Key | Action |
|---|---|
| Enter | Submits the owning form |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.