SPB Git

Repository Public MIT

GlassOTPFieldExample.swift 3.3 KB 89 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Forms/GlassOTPFieldExample.swift8// Module           : Examples9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassKit4344public struct GlassOTPFieldExample: GlassExample {45    public static let componentID = "glass-otp-field"46    /// The demo accepts this fictitious code only.47    private static let expected = "246810"4849    @State private var code = ""50    @State private var shortCode = ""51    @State private var status = "Enter the 6-digit demo code 246810."52    @State private var rejected = false5354    public init() {}5556    public var body: some View {57        ScrollView {58            VStack(alignment: .leading, spacing: 20) {59                GlassOTPField(code: $code, hasError: rejected) { entered in60                    rejected = entered != Self.expected61                    status = rejected ? "That code is not valid. Try again." : "Code accepted."62                }63                .onChange(of: code) { _, newValue in64                    if newValue.count < 6 { rejected = false }65                }6667                Label(status, systemImage: rejected ? "xmark.octagon" : "info.circle")68                    .font(.callout)69                    .foregroundStyle(.secondary)7071                HStack {72                    Button("Simulate paste “246 810”") { code = GlassOTPSanitizer.sanitize("246 810", length: 6) }73                    Button("Clear") { code = "" }74                }7576                Text("Four digits").font(.headline)77                GlassOTPField(code: $shortCode, length: 4, label: "PIN")7879                Text("Disabled").font(.headline)80                GlassOTPField(code: .constant("12"), length: 4)81                    .disabled(true)82            }83            .padding()84        }85    }86}8788#Preview("GlassOTPField") { GlassOTPFieldExample() }89
Files main
Menu

Theme