SPB Git

Repository Public MIT

doc-glass-text-field.md 3.5 KB 87 lines markdown

1# GlassTextField23| | |4|---|---|5| Identifier | `glass-text-field` |6| Module | `SPGlassForms` |7| Kind | `content-component` — a system `TextField` on an opaque themed surface with `glassFieldOutline` |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Forms/GlassTextFieldExample.swift` |1011## Minimal usage1213```swift14@State private var name = ""1516GlassTextField("Name", text: $name)17```1819## Variants2021| Variant | Use |22|---|---|23| `prompt:` | Placeholder shown while the field is empty. |24| `helper:` | Explanation under the field, replaced by the error when there is one. |25| `validation:` | Rules checked in order: `.required()`, `.minLength(_:)`, `.maxLength(_:)`, `.custom(message:_:)`. |26| `validateOn:` | `.afterEditing` (default: once the field was left or submitted), `.always`, `.never`. |27| `errorMessage:` | An error decided by the host (server answer). It replaces the validation message. |28| `input:` | `.standard`, `.email`, `.username`, `.url`, `.number`, `.name`, or a custom `GlassTextInputConfiguration`. |2930## Parameters3132| Parameter | Type | Default | Meaning |33|---|---|---|---|34| `label` | `LocalizedStringKey` | — | Label above the field; also its accessibility label. |35| `text` | `Binding<String>` | — | The value. |36| `prompt` | `LocalizedStringKey?` | `nil` | Placeholder. |37| `helper` | `LocalizedStringKey?` | `nil` | Helper text. |38| `validation` | `GlassFieldValidation` | `.none` | Rules of the field. |39| `validateOn` | `GlassValidationTrigger` | `.afterEditing` | When the message starts to show. |40| `errorMessage` | `String?` | `nil` | External error. |41| `input` | `GlassTextInputConfiguration` | `.standard` | Autocorrection, capitalisation, keyboard, content type. |42| `focus` | `FocusState<Bool>.Binding?` | `nil` | Focus owned by the host. |4344`GlassTextInputConfiguration` holds `autocorrectionDisabled`, `capitalization`, `keyboard` and `contentKind`. Capitalisation and keyboard apply on iOS and iPadOS only.4546## States4748Idle, focused (outline in the theme's `focusRing` colour), error (outline in `destructive`, icon and message under the field), disabled (`.disabled(_:)`, dimmed). A required field shows an asterisk next to its label and announces "Required".4950## Advanced example5152```swift53@State private var email = ""54@State private var serverError: String?55@FocusState private var emailFocused: Bool5657GlassTextField(58    "Email", text: $email, prompt: "name@example.com",59    validation: GlassFieldValidation(60        .required(),61        .custom(message: String(localized: "Enter a valid address")) { $0.contains("@") }62    ),63    errorMessage: serverError,64    input: .email,65    focus: $emailFocused66)67.submitLabel(.next)68.onSubmit { register() }69```7071The rules are pure and can be checked before submitting: `validation.isValid(email)`.7273## Accessibility7475- The visible label is the accessibility label of the field; "Required" is appended for required fields.76- An error is an icon plus a sentence, read as "Error: …"; it is never colour alone.77- Label, value and messages follow Dynamic Type and wrap.78- Default messages are localised (English, French); lengths count user-perceived characters.7980## Limits8182- Single line; use `GlassTextArea` for multiline text.83- Validation is synchronous. Asynchronous checks (availability of a name) go through `errorMessage`.84- Length and custom rules accept an empty value; add `.required()` to refuse it.85- On macOS, capitalisation and keyboard settings have no effect, and only some content types exist.86- Focus changes and the keyboard were not exercised on a running device here.87
Files main
Menu

Theme