inputs/DSTextField

DSTextField

inputs
since v1.0

Text field with support for label, placeholder, leading/trailing icons, hint, error message, focus and disabled states.

iOS 17+macOS 14+

Purpose

Primary text input for the system. Covers 90% of data-entry use cases in forms.

States

default

Subtle border, no focus.

focused

Primary-colour border + shadow sm.

error

Red border + error message below.

disabled

50% opacity. No interaction.

Props

PropTypeDefaultDescription
labelString?nilLabel above the field.
placeholderString""Placeholder text.
textreqBinding<String>Binding to the field value.
sizeDSInputSize.mdField height and font size.
leadingIconString?nilSF Symbol on the left.
trailingIconString?nilSF Symbol on the right (when empty).
hintString?nilHelper text below the field.
errorString?nilError message. Overrides hint.
isDisabledBoolfalseDisables the field.
onSubmit(() -> Void)?nilAction when Return is pressed.

Examples

Email field

Input with icon and validation.

swift
DSTextField(
    label: "Email",
    placeholder: "name@company.com",
    text: $email,
    leadingIcon: "envelope",
    error: emailError
)

Inline search field

No label, with icon and submit.

swift
DSTextField(
    placeholder: "Search products...",
    text: $query,
    leadingIcon: "magnifyingglass",
    onSubmit: { viewModel.search(query) }
)

Form composition

Complete form with validation.

swift
VStack(alignment: .leading, spacing: DSSpacing.md) {
    DSTextField(
        label: "Full Name",
        placeholder: "John Doe",
        text: $name,
        error: name.isEmpty ? "Name is required" : nil
    )
    
    DSTextField(
        label: "Email",
        placeholder: "john@example.com",
        text: $email,
        leadingIcon: "envelope",
        error: !isValidEmail(email) ? "Invalid email format" : nil
    )
    
    DSButton("Submit", variant: .primary) {
        submitForm()
    }
}

Usage Guidelines

  • Always provide a visible label or equivalent aria-label.
  • Validate on .onSubmit to avoid unnecessary network calls.
  • Never show an error without also explaining how to fix it.

When to Use

✓ Use DSTextField for:

  • Single-line text input in forms (name, email, username)
  • Search fields with onSubmit action
  • Numeric input with text keyboard (use TextField with .numberPad)
  • Fields with validation and error states

✗ Avoid using for:

  • Multi-line text (use DSTextArea)
  • Password input (use DSSecureField)
  • Dedicated search UI (use DSSearchBar with better styling)
  • Phone numbers (use DSPhoneField with formatting)

Consider instead:

DSTextArea

For multi-line text input (comments, descriptions)

DSSecureField

For password or sensitive data

DSSearchBar

For search with clear button and search styling

Accessibility

VoiceOver

Announces label, current value, and hint/error (e.g., 'Email, Text field, name@company.com')

Keyboard

  • Type to input
  • Return to submit (if onSubmit set)
  • Tab to next field

Dynamic Type

✓ Supported

Contrast

WCAG AA compliant (border 3:1, text 4.5:1)

Traits

.isTextFieldLabel via accessibilityLabelHint via accessibilityHint

Related Components