Skip to main content

Text field

The text field component captures single-line text input. Use it to collect short, free-form information like names, titles, or identifiers.

The component supports various input configurations including placeholders, character limits, and validation. For multi-line text entry, use the text area component.


Configure the following properties on the TextField component.

Anchor to details
details
string

The additional text to provide context or guidance for the field. This text is displayed along with the field and its label to offer more information or instructions to the user. This will also be exposed to screen reader users.

Anchor to disabled
disabled
boolean
Default: false

Whether the field is disabled, preventing user interaction. Use when the field is temporarily unavailable due to application state, permissions, or dependencies.

Anchor to error
error
string

An error message to indicate a problem to the user. The field will be given specific stylistic treatment to communicate issues that must be resolved immediately.

string

A unique identifier for the element used for targeting with CSS, JavaScript, or accessibility features.

Anchor to label
label
string

The content to use as the field label that describes the text information being requested.

Anchor to maxLength
maxLength
number
Default: Infinity

The maximum number of characters allowed in the text field.

Anchor to placeholder
placeholder
string

A short hint that provides guidance about the expected value of the field.

Anchor to required
required
boolean
Default: false

Whether the field needs a value. This requirement adds semantic value to the field but doesn't cause an error to appear automatically. Use the error property to present validation errors.

Anchor to value
value
string

The current text content entered in the field. An empty string means no text is entered.

The text field component supports slots for additional content placement within the field. Learn more about using slots.

Anchor to accessory
accessory
HTMLElement

The additional content to be displayed in the field. Commonly used to display clickable text or action elements. Only button and clickable components with text content only are supported in this slot. Use the slot="accessory" attribute to place elements in this area.

The text field component provides event callbacks for handling user interactions. Learn more about handling events.

(event: <"s-text-field">) => void

Called when the element loses focus.

Anchor to change
change
(event: <"s-text-field">) => void

Called after editing completes, typically on blur.

Anchor to focus
focus
(event: <"s-text-field">) => void

Called when the element receives focus.

Anchor to input
input
(event: <"s-text-field">) => void

Called when the user makes any changes in the field.


Anchor to Capture text input with a text fieldCapture text input with a text field

Capture single-line text input using a text field component with validation support. This example shows a basic text field with label and placeholder text.

Capture text input with a text field

Capture single-line text input using a text field component with validation support. This example shows a basic text field with label and placeholder text.

Capture text input with a text field

<s-text-field
label="Store name"
value="Snowdevil"
required
/>

Anchor to Add accessory buttonsAdd accessory buttons

Add action buttons to the text field using the accessory slot for quick actions like clearing text or submitting input. This example shows how to use s-button and s-clickable components with text content in the accessory slot, enabling inline actions without leaving the input context.

Add accessory buttons

<s-text-field label="Discount code" value="SUMMER2024">
<s-button slot="accessory" onClick={() => console.log('Apply discount')}>
Apply
</s-button>
</s-text-field>;

Anchor to Configure validation and guidanceConfigure validation and guidance

Configure common text field properties for validation, character limits, and user guidance. This example demonstrates using props like maxlength, required, helperText, and error to create a well-guided input experience with proper validation feedback.

Configure validation and guidance

<s-text-field
label="Product SKU"
value={sku}
placeholder="Enter SKU"
details="Use format: ABC-123"
error={hasError ? "SKU already exists" : undefined}
disabled={isProcessing}
required
maxLength={20}
onChange={(event) => setSku(event.currentTarget.value)}
/>

Subscribe to text field events including onInput, onFocus, onBlur, and onChange to respond to user interactions. This example shows how to handle different input events for real-time validation, autosave functionality, or dynamic form behavior.

Handle input events

<s-text-field
label="Product SKU"
placeholder="Enter SKU"
onInput={(event) => {
console.log('Input:', event.currentTarget.value);
console.log('Current error:', event.currentTarget.error);
}}
onFocus={(event) => {
console.log('Focused with value:', event.currentTarget.value);
}}
onBlur={(event) => {
console.log('Blurred with value:', event.currentTarget.value);
}}
onChange={(event) => {
console.log('Changed to:', event.currentTarget.value);
}}
/>

  • Use for single-line text input: Choose text field for short values like names, titles, or identifiers. For multi-line content, use text area.
  • Show character limit feedback: When approaching maxLength, display remaining characters in the details text.
  • Write descriptive labels: Use specific labels like "Product Name" or "Reference Code" rather than generic terms.

The accessory slot supports only button and clickable components with text content only—other component types or complex layouts can't be used for field accessories.


Was this page helpful?