StartFoundationsComponentsContent

Field

A field gives one form control a visible label, help text, and an error message, and names the control for screen readers.

Stable
OverviewUsageAPIAccessibility
<tk-field label="Email address" helper="We only use this for the receipt">
  <tk-input type="email" name="email" autocomplete="email"></tk-input>
</tk-field>

Anatomy

Part What it is
tk-field the wrapper; no Tab stop and no form value of its own
label the label text and, when required, an asterisk
help text the helper text, between the label and the control
default slot the control, exactly one
error the error message with an icon, under the control

The Field draws the label, help text, and error from its attributes. The control keeps its value, its Tab stop, and its keyboard.

States

<tk-field label="Company name" required>
  <tk-input name="company"></tk-input>
</tk-field>
<tk-field label="Email address" required error="Enter an email address, like name@example.com">
  <tk-input type="email" name="email" value="ada@"></tk-input>
</tk-field>
<tk-field label="Username" helper="Usernames can't be changed" disabled>
  <tk-input name="username" value="ada"></tk-input>
</tk-field>
  • required: adds an asterisk after the label and makes the control required. The asterisk is gray, not red: an empty required field hasn't failed yet.
  • error: shows the message under the control, turns its border red, and marks it invalid. Remove error to clear it.
  • disabled: the control can't be focused or edited. The label and help text stay readable, so the help text can say why the field is locked.
  • Set required and disabled on the Field, not on the control. The Field overwrites the control's own values.

The Field doesn't validate. You decide when to set error and what it says.

Label, help text, and errors

<tk-field label="Card number" helper="The 16 digits on the front of your card">
  <tk-input inputmode="numeric" name="card" autocomplete="cc-number"></tk-input>
</tk-field>
<tk-input aria-label="Filter by name"></tk-input>
  • The Field names the control from label, and adds the help text and error to its description. Clicking the label moves focus into the control.
  • Without a visible label, skip the Field and set aria-label on the control. Use it only when the purpose is clear from what's around the field.
  • Every control needs one or the other. A placeholder is never a label.
  • A Search field names itself "Search" on its own; inside a Field, the Field's label replaces that name.