Skip to content
Focsd homeFocsd homeFocsd
Forms

Text field

A single-line text input with its label, hint and error built in.

  • Tag: focsd-text-field
  • Pattern: Native input
  • Category: Forms

Example

HTML
<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>

API

Attributes

AttributePropertyTypeDefault
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevaluestring—
typetype'text' | 'email' | 'password' | 'search' | 'tel' | 'url'text
placeholderplaceholderstring—
autocompleteautocompleteAutoFill—
minlengthminlengthnumber—
maxlengthmaxlengthnumber—
patternpatternstring | undefined—
sizesizenumber | undefined—
dirnamedirnamestring | undefined—
inputmodeinputMode——
enterkeyhintenterKeyHint——
autocapitalizeautocapitalize——
autocorrectautocorrect——
spellcheckspellcheck——

Methods

MethodDescription
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

EventDescription
input—
changeFired 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).

PartDescription
layoutThe box around the label and the body; `data-position` is the label position.
labelThe label element.
bodyThe control and its messages.
controlThe control element.
hintThe hint element.
messageThe shown message; also `error`, `warning` or `success` for its kind.
message-iconThe icon in the message.
announcerThe 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 an autocomplete token (WCAG 1.3.5). Not supported: list (datalist), because the ID reference cannot cross into shadow DOM. Use focsd-combobox.
  • The label, hint and error render in the same shadow root as the <input>, so for and aria-describedby always resolve.
  • An error sets aria-invalid and 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, warning and success shows, in that order. warning and success never block submission.
  • Built-in validation: error-message is the text shown when the field fails required, type, pattern, min and the like (on submit, or validate()); it clears once the value is fixed. warning-pattern with warning-message shows a non-blocking warning while the value does not match. Inside a form with focsd-error-summary, no script is needed.
  • Never use the placeholder as the label.
  • Form-associated: participates in FormData, validation, reset and fieldset disabled like a native control.

Keyboard

KeyAction
EnterSubmits the owning form