|
| 1 | +import FioriSwiftUICore |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +struct TextInputExample: View { |
| 5 | + @State var showsCharCount = true |
| 6 | + @State var allowsBeyondLimit = true |
| 7 | + |
| 8 | + @State private var phoneNumber: String = "" |
| 9 | + @State private var price: String = "" |
| 10 | + @State private var numeric: String = "" |
| 11 | + @State private var date: String = "" |
| 12 | + @State private var genericText: String = "" |
| 13 | + @State private var genericText2: String = "" |
| 14 | + |
| 15 | + @State private var valueText: String = "" |
| 16 | + |
| 17 | + // the below ids used to scroll to related items |
| 18 | + private var phoneNumberId = UUID() |
| 19 | + private var priceId = UUID() |
| 20 | + private var numericId = UUID() |
| 21 | + private var dateId = UUID() |
| 22 | + private var genericTextId = UUID() |
| 23 | + private var genericText2Id = UUID() |
| 24 | + private var valueTextId = UUID() |
| 25 | + |
| 26 | + var body: some View { |
| 27 | + ScrollViewReader { _ in |
| 28 | + List { |
| 29 | + Section { |
| 30 | + TextFieldFormView(title: "Phone Number", text: self.$phoneNumber, formatter: self.phoneNumberFormatter, placeholder: "(123) 456-7890", errorMessage: AttributedString(""), maxTextLength: 13, hintText: AttributedString("(###) ###-####"), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 31 | + .id(self.phoneNumberId) |
| 32 | + } header: { |
| 33 | + Text("Phone Number Formatter - Text field form view") |
| 34 | + .font(.fiori(forTextStyle: .subheadline)) |
| 35 | + .foregroundStyle(Color.preferredColor(.secondaryLabel)) |
| 36 | + } |
| 37 | + Section { |
| 38 | + TextFieldFormView(title: "Price", text: self.$price, formatter: self.priceFormatter, placeholder: "$ 0.00", errorMessage: AttributedString(""), hintText: AttributedString(""), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 39 | + .id(self.priceId) |
| 40 | + |
| 41 | + TextFieldFormView(title: "Number", text: self.$numeric, formatter: self.numberFormatter, placeholder: "1.234lbs", errorMessage: AttributedString(""), hintText: AttributedString(""), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 42 | + .id(self.numericId) |
| 43 | + } header: { |
| 44 | + Text("Custom Number Formatter - Text field form view") |
| 45 | + .font(.fiori(forTextStyle: .subheadline)) |
| 46 | + .foregroundStyle(Color.preferredColor(.secondaryLabel)) |
| 47 | + } |
| 48 | + Section { |
| 49 | + TextFieldFormView(title: "Date", text: self.$date, formatter: self.dateFormatter, placeholder: "AA 2025/01/01", errorMessage: AttributedString(""), maxTextLength: 13, hintText: AttributedString("AA YYYY/MM/DD"), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 50 | + .id(self.dateId) |
| 51 | + |
| 52 | + TextFieldFormView(title: "Generic Text", text: self.$genericText, formatter: self.genericTextFormatter, placeholder: "+AA 123 456# a 2025/01/01 A*", errorMessage: AttributedString(""), maxTextLength: 28, hintText: AttributedString("+AA NNN NNNS X YYYY/MM/DD A*"), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 53 | + .id(self.genericTextId) |
| 54 | + |
| 55 | + TextFieldFormView(title: "Generic Text2", text: self.$genericText2, formatter: self.genericText2Formatter, placeholder: "123+A* 456 789 A*", errorMessage: AttributedString(""), maxTextLength: 18, hintText: AttributedString("NNN+A* NNN NNN A*"), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit, isRequired: false, actionIcon: nil, action: nil) |
| 56 | + .id(self.genericText2Id) |
| 57 | + |
| 58 | + } header: { |
| 59 | + Text("Generic Text Formatter - Text field form view") |
| 60 | + .font(.fiori(forTextStyle: .subheadline)) |
| 61 | + .foregroundStyle(Color.preferredColor(.secondaryLabel)) |
| 62 | + } |
| 63 | + Section { |
| 64 | + TitleFormView(text: self.$valueText, formatter: self.genericTextFormatter, placeholder: "+AA 123 456# a 2025/01/01 A*", hintText: AttributedString("+AA NNN NNNS X YYYY/MM/DD A*"), isCharCountEnabled: self.showsCharCount, allowsBeyondLimit: self.allowsBeyondLimit) |
| 65 | + .padding(.leading, -4) |
| 66 | + .padding(.trailing, -4) |
| 67 | + .id(self.valueTextId) |
| 68 | + } header: { |
| 69 | + Text("Generic Text Formatter - Title form view") |
| 70 | + .font(.fiori(forTextStyle: .subheadline)) |
| 71 | + .foregroundStyle(Color.preferredColor(.secondaryLabel)) |
| 72 | + } |
| 73 | + .listRowSeparator(.hidden, edges: .bottom) |
| 74 | + .environment(\.defaultMinListRowHeight, 0) |
| 75 | + .environment(\.defaultMinListHeaderHeight, 0) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + var phoneNumberFormatter: PhoneNumberFormatter { |
| 81 | + let formatter = PhoneNumberFormatter() |
| 82 | + return formatter |
| 83 | + } |
| 84 | + |
| 85 | + var priceFormatter: CustomNumberFormatter { |
| 86 | + let formatter = CustomNumberFormatter() |
| 87 | + formatter.numberStyle = .currency |
| 88 | + return formatter |
| 89 | + } |
| 90 | + |
| 91 | + var numberFormatter: CustomNumberFormatter { |
| 92 | + let formatter = CustomNumberFormatter() |
| 93 | + formatter.numberStyle = .decimal |
| 94 | + formatter.maximumFractionDigits = 3 |
| 95 | + formatter.positiveSuffix = "lbs" |
| 96 | + return formatter |
| 97 | + } |
| 98 | + |
| 99 | + var dateFormatter: GenericTextFormatter { |
| 100 | + let formatter = GenericTextFormatter() |
| 101 | + formatter.format = "AA YYYY/MM/DD" |
| 102 | + formatter.yearMinium = 1000 |
| 103 | + formatter.yearMaximum = 3000 |
| 104 | + return formatter |
| 105 | + } |
| 106 | + |
| 107 | + var numericFormatter: GenericTextFormatter { |
| 108 | + let formatter = GenericTextFormatter() |
| 109 | + formatter.format = "+\\A\\* NNN NNN \\A\\*" |
| 110 | + return formatter |
| 111 | + } |
| 112 | + |
| 113 | + var genericTextFormatter: GenericTextFormatter { |
| 114 | + let formatter = GenericTextFormatter() |
| 115 | + formatter.format = "+AA NNN NNNS X YYYY/MM/DD \\A\\*" |
| 116 | + return formatter |
| 117 | + } |
| 118 | + |
| 119 | + var genericText2Formatter: GenericTextFormatter { |
| 120 | + let formatter = GenericTextFormatter() |
| 121 | + formatter.format = "NNN+\\A\\* NNN NNN \\A\\*" |
| 122 | + return formatter |
| 123 | + } |
| 124 | +} |
0 commit comments