SPB Git

Repository Public MIT

doc-glass-otp-field.md 3.5 KB 78 lines markdown

1# GlassOTPField23| | |4|---|---|5| Identifier | `glass-otp-field` |6| Module | `SPGlassForms` |7| Kind | `content-component` — one system `TextField` under a row of opaque boxes |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Forms/GlassOTPFieldExample.swift` |1011## Minimal usage1213```swift14@State private var code = ""1516GlassOTPField(code: $code) { code in17    verify(code)18}19```2021## Variants2223| Variant | Use |24|---|---|25| `length: 4` … `8` | Number of digits. Six by default. |26| `hasError: true` | Outlines the boxes in the `destructive` colour after a rejected code. |27| `label:` | Accessibility label when "Verification code" is not the right name (a PIN, for example). |2829## Parameters3031| Parameter | Type | Default | Meaning |32|---|---|---|---|33| `code` | `Binding<String>` | — | Digits entered so far, ASCII only. |34| `length` | `Int` | `6` | Number of digits. |35| `label` | `LocalizedStringKey?` | "Verification code" | Accessibility label. |36| `hasError` | `Bool` | `false` | Error outline. |37| `onComplete` | `@MainActor (String) -> Void` | `{ _ in }` | Called each time the code becomes complete. |3839## States4041Empty, partially filled (the box that takes the next digit is underlined; while the field has focus it shows a caret and the focus ring), complete (`onComplete` fires), error (digits and outlines in `destructive`), disabled. Clearing the binding (`code = ""`) resets the field.4243Codes of six or eight digits are drawn as two groups separated by a short dash ("428 – 913"); `GlassOTPLayout.groupBoundary(length:)` gives the index where the second group starts. The field never shows a placeholder: its label is for assistive technologies only (in 1.0 the label was drawn across the boxes).4445## Advanced example4647```swift48GlassOTPField(code: $code, length: 6, hasError: model.codeRejected) { entered in49    Task { await model.verify(entered) }50}51.onChange(of: code) { _, _ in model.codeRejected = false }52.disabled(model.isVerifying)53```5455Input is cleaned by the pure `GlassOTPSanitizer.sanitize(_:length:)`:5657```swift58GlassOTPSanitizer.sanitize("Your code is 482 913.", length: 6)   // "482913"59GlassOTPSanitizer.sanitize("١٢٣٤٥٦", length: 6)                  // "123456"60```6162## Accessibility6364- There is one text field, so VoiceOver meets one element: its label and the value "3 of 6 digits entered". The boxes are hidden from assistive technologies and the digits are not read one box at a time by GlassKit.65- The field uses the one-time-code content type, so the system can offer the code received by message, and the number pad on iOS.66- Pasting a whole code works as in any text field: long-press or Command-V; separators and letters are dropped.67- Boxes share the available width up to a square, their height follows Dynamic Type, and the digits shrink rather than clip.68- The caret of the active box blinks, except with Reduce Motion.69- Digits are always laid out left to right, including in right-to-left languages.7071## Limits7273- Digits only; alphanumeric codes are not supported.74- The code lives only in the host's binding. The component stores, logs and copies nothing; clear the binding when you are done.75- The text field is drawn transparent over the boxes, so the system caret and selection highlight are not visible; the outlined box shows the position instead.76- Digits of other scripts are converted to ASCII; symbols that only look like numbers (fractions, Roman numerals, circled digits) are dropped.77- AutoFill from Messages, the paste menu and VoiceOver output need a device and were not exercised here.78
Files main
Menu

Theme