From 1c4cf325511915422bf8e639eb98d9aef5a8f2f6 Mon Sep 17 00:00:00 2001 From: Francisco Rodriguez Date: Fri, 12 Jul 2024 09:51:02 +0200 Subject: [PATCH 1/2] feat(InputField): add option to set textContentType --- .../Components/Inputfield/InputField.swift | 34 ++++++++++++++++++- .../Inputfield/LegacyTextField.swift | 6 ++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift b/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift index 640fe950..dc047473 100644 --- a/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift +++ b/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift @@ -26,6 +26,7 @@ public struct InputField: View { public enum Style: Equatable { case text + case numeric case email case secure case phone(code: String) @@ -175,7 +176,8 @@ private extension InputField { isResponder: $editing, isSecured: secure, keyboard: keyboard, - inputStyle: inputStyle + inputStyle: inputStyle, + textContentType: textContentType ) } @@ -185,6 +187,7 @@ private extension InputField { return secureActivated case .phone, .text, + .numeric, .email, .dropdown, .date, @@ -202,6 +205,7 @@ private extension InputField { case .phone, .secure, .text, + .numeric, .email, .search: return .text @@ -220,6 +224,24 @@ private extension InputField { return .phonePad case .email: return .emailAddress + case .numeric: + return .numberPad + } + } + + var textContentType: UITextContentType? { + switch style { + case .secure, + .text, + .dropdown, + .search, + .numeric, + .date: + return nil + case .phone: + return .telephoneNumber + case .email: + return .emailAddress } } @@ -261,6 +283,12 @@ public extension InputField { view.style = style return view } + + func textContentType(_ textContentType: UITextContentType?) -> InputField { + var view = self + view.textField.textContentType(textContentType) + return view + } } // MARK: Previews @@ -275,6 +303,10 @@ struct InputField_Previews: PreviewProvider { InputField(placeholder: "Email", text: $text1) .style(.email) .padding() + + InputField(placeholder: "Numeric", text: $text1) + .style(.numeric) + .padding() InputField(placeholder: "Normal", text: $text1, assistiveText: $assistiveText) .style(.email) diff --git a/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift b/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift index da9f5306..6dce6c0f 100644 --- a/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift +++ b/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift @@ -25,6 +25,7 @@ struct LegacyTextField: UIViewRepresentable { var isSecured: Bool var keyboard: UIKeyboardType var inputStyle: LegacyTextFieldInputStyle + var textContentType: UITextContentType? func makeUIView(context: UIViewRepresentableContext) -> UITextField { let textField = ActionsTextField(frame: .zero) @@ -70,6 +71,7 @@ struct LegacyTextField: UIViewRepresentable { textField.autocapitalizationType = .none textField.autocorrectionType = .no textField.keyboardType = keyboard + textField.textContentType = textContentType textField.delegate = context.coordinator textField.textColor = Color.textPrimary.uiColor @@ -89,6 +91,10 @@ struct LegacyTextField: UIViewRepresentable { toolbar.sizeToFit() textField.inputAccessoryView = toolbar } + + mutating func textContentType(_ contentType: UITextContentType?) { + textContentType = contentType + } } // MARK: Coordinator From a1ba9a7939f208c6a2a8905694870653b0f552c5 Mon Sep 17 00:00:00 2001 From: franciscojrp Date: Fri, 12 Jul 2024 09:58:29 +0200 Subject: [PATCH 2/2] Run swiftformat --- .../MisticaSwiftUI/Components/Inputfield/InputField.swift | 8 ++++---- .../Components/Inputfield/LegacyTextField.swift | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift b/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift index dc047473..b61368b5 100644 --- a/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift +++ b/Sources/MisticaSwiftUI/Components/Inputfield/InputField.swift @@ -176,7 +176,7 @@ private extension InputField { isResponder: $editing, isSecured: secure, keyboard: keyboard, - inputStyle: inputStyle, + inputStyle: inputStyle, textContentType: textContentType ) } @@ -228,7 +228,7 @@ private extension InputField { return .numberPad } } - + var textContentType: UITextContentType? { switch style { case .secure, @@ -283,7 +283,7 @@ public extension InputField { view.style = style return view } - + func textContentType(_ textContentType: UITextContentType?) -> InputField { var view = self view.textField.textContentType(textContentType) @@ -303,7 +303,7 @@ struct InputField_Previews: PreviewProvider { InputField(placeholder: "Email", text: $text1) .style(.email) .padding() - + InputField(placeholder: "Numeric", text: $text1) .style(.numeric) .padding() diff --git a/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift b/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift index 6dce6c0f..4181be8d 100644 --- a/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift +++ b/Sources/MisticaSwiftUI/Components/Inputfield/LegacyTextField.swift @@ -91,10 +91,10 @@ struct LegacyTextField: UIViewRepresentable { toolbar.sizeToFit() textField.inputAccessoryView = toolbar } - + mutating func textContentType(_ contentType: UITextContentType?) { textContentType = contentType - } + } } // MARK: Coordinator