SPB Git

Repository Public MIT

example-glass-otp-field.txt 1.7 KB 49 lines text

1import SwiftUI2import SPGlassKit34public struct GlassOTPFieldExample: GlassExample {5    public static let componentID = "glass-otp-field"6    /// The demo accepts this fictitious code only.7    private static let expected = "246810"89    @State private var code = ""10    @State private var shortCode = ""11    @State private var status = "Enter the 6-digit demo code 246810."12    @State private var rejected = false1314    public init() {}1516    public var body: some View {17        ScrollView {18            VStack(alignment: .leading, spacing: 20) {19                GlassOTPField(code: $code, hasError: rejected) { entered in20                    rejected = entered != Self.expected21                    status = rejected ? "That code is not valid. Try again." : "Code accepted."22                }23                .onChange(of: code) { _, newValue in24                    if newValue.count < 6 { rejected = false }25                }2627                Label(status, systemImage: rejected ? "xmark.octagon" : "info.circle")28                    .font(.callout)29                    .foregroundStyle(.secondary)3031                HStack {32                    Button("Simulate paste “246 810”") { code = GlassOTPSanitizer.sanitize("246 810", length: 6) }33                    Button("Clear") { code = "" }34                }3536                Text("Four digits").font(.headline)37                GlassOTPField(code: $shortCode, length: 4, label: "PIN")3839                Text("Disabled").font(.headline)40                GlassOTPField(code: .constant("12"), length: 4)41                    .disabled(true)42            }43            .padding()44        }45    }46}4748#Preview("GlassOTPField") { GlassOTPFieldExample() }49
Files main
Menu

Theme