Implemented UIKitTextField to be able to react to Return key presses
This commit is contained in:
parent
4512b024f8
commit
e7127aa88b
|
@ -15,6 +15,11 @@ extension String {
|
||||||
return !isBlank
|
return !isBlank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func localize() -> String {
|
||||||
|
return NSLocalizedString(self, comment: "")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Optional where Wrapped == String {
|
extension Optional where Wrapped == String {
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
|
||||||
|
struct UIKitTextField: UIViewRepresentable {
|
||||||
|
|
||||||
|
@Binding private var text: String
|
||||||
|
|
||||||
|
private var placeHolder: String
|
||||||
|
|
||||||
|
private var keyboardType: UIKeyboardType = .default
|
||||||
|
private var isPasswordField: Bool = false
|
||||||
|
|
||||||
|
private var actionOnReturnKeyPress: (() -> Void)? = nil
|
||||||
|
|
||||||
|
init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, actionOnReturnKeyPress: (() -> Void)? = nil) {
|
||||||
|
self.placeHolder = titleKey
|
||||||
|
_text = text
|
||||||
|
|
||||||
|
self.keyboardType = keyboardType
|
||||||
|
self.isPasswordField = isPasswordField
|
||||||
|
|
||||||
|
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func makeUIView(context: UIViewRepresentableContext<UIKitTextField>) -> UITextField {
|
||||||
|
let textField = UITextField(frame: .zero)
|
||||||
|
|
||||||
|
textField.placeholder = placeHolder.localize()
|
||||||
|
|
||||||
|
textField.keyboardType = keyboardType
|
||||||
|
textField.isSecureTextEntry = isPasswordField
|
||||||
|
|
||||||
|
textField.delegate = context.coordinator
|
||||||
|
|
||||||
|
return textField
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeCoordinator() -> UIKitTextField.Coordinator {
|
||||||
|
return Coordinator(text: $text, actionOnReturnKeyPress: actionOnReturnKeyPress /*, nextResponder: $nextResponder, isResponder: $isResponder */)
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<UIKitTextField>) {
|
||||||
|
uiView.text = text
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Coordinator: NSObject, UITextFieldDelegate {
|
||||||
|
|
||||||
|
@Binding private var text: String
|
||||||
|
|
||||||
|
private var actionOnReturnKeyPress: (() -> Void)?
|
||||||
|
|
||||||
|
|
||||||
|
init(text: Binding<String>, actionOnReturnKeyPress: (() -> Void)? = nil) {
|
||||||
|
_text = text
|
||||||
|
|
||||||
|
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
||||||
|
}
|
||||||
|
|
||||||
|
func textFieldDidChangeSelection(_ textField: UITextField) {
|
||||||
|
text = textField.text ?? ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
||||||
|
actionOnReturnKeyPress?()
|
||||||
|
|
||||||
|
textField.resignFirstResponder()
|
||||||
|
|
||||||
|
return actionOnReturnKeyPress != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
struct UIKitTextView_Previews: PreviewProvider {
|
||||||
|
|
||||||
|
@State static private var text = ""
|
||||||
|
|
||||||
|
static var previews: some View {
|
||||||
|
UIKitTextField("Test label", text: $text)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -37,9 +37,17 @@ struct AddAccountDialog: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
TextField("Customer ID", text: $customerId)
|
UIKitTextField("Customer ID", text: $customerId) {
|
||||||
|
if self.isRequiredDataEntered() {
|
||||||
|
self.addAccount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SecureField("Password", text: $password)
|
UIKitTextField("Password", text: $password, isPasswordField: true) {
|
||||||
|
if self.isRequiredDataEntered() {
|
||||||
|
self.addAccount()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
|
|
|
@ -128,7 +128,11 @@ struct EnterTanDialog: View {
|
||||||
.padding(.vertical, 2)
|
.padding(.vertical, 2)
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
TextField("Enter TAN:", text: $enteredTan)
|
UIKitTextField("Enter TAN:", text: $enteredTan) {
|
||||||
|
if self.enteredTan.isNotBlank {
|
||||||
|
self.enteringTanDone()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
|
|
|
@ -90,7 +90,11 @@ struct TransferMoneyDialog: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
TextField("Remittee Name", text: $remitteeName)
|
UIKitTextField("Remittee Name", text: $remitteeName) {
|
||||||
|
if self.isRequiredDataEntered() {
|
||||||
|
self.transferMoney()
|
||||||
|
}
|
||||||
|
}
|
||||||
.onReceive(Just(remitteeName)) { newValue in
|
.onReceive(Just(remitteeName)) { newValue in
|
||||||
self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank
|
self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank
|
||||||
}
|
}
|
||||||
|
@ -105,7 +109,11 @@ struct TransferMoneyDialog: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TextField("Remittee IBAN", text: $remitteeIban)
|
UIKitTextField("Remittee IBAN", text: $remitteeIban) {
|
||||||
|
if self.isRequiredDataEntered() {
|
||||||
|
self.transferMoney()
|
||||||
|
}
|
||||||
|
}
|
||||||
.onReceive(Just(remitteeIban)) { newValue in
|
.onReceive(Just(remitteeIban)) { newValue in
|
||||||
self.isValidRemitteeIbanEntered = newValue.count > 14 // TODO: implement real check if IBAN is valid
|
self.isValidRemitteeIbanEntered = newValue.count > 14 // TODO: implement real check if IBAN is valid
|
||||||
self.tryToGetBicFromIban(newValue)
|
self.tryToGetBicFromIban(newValue)
|
||||||
|
@ -113,9 +121,13 @@ struct TransferMoneyDialog: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
TextField("Amount", text: $amount)
|
UIKitTextField("Amount", text: $amount, keyboardType: .decimalPad) {
|
||||||
.keyboardType(.decimalPad)
|
if self.isRequiredDataEntered() {
|
||||||
|
self.transferMoney()
|
||||||
|
}
|
||||||
|
}
|
||||||
.onReceive(Just(amount)) { newValue in
|
.onReceive(Just(amount)) { newValue in
|
||||||
|
// TODO: implement DecimalTextField / NumericTextField
|
||||||
let filtered = newValue.filter { "0123456789,".contains($0) }
|
let filtered = newValue.filter { "0123456789,".contains($0) }
|
||||||
if filtered != newValue {
|
if filtered != newValue {
|
||||||
self.amount = filtered
|
self.amount = filtered
|
||||||
|
@ -124,7 +136,11 @@ struct TransferMoneyDialog: View {
|
||||||
self.isValidAmountEntered = self.amount.isNotBlank
|
self.isValidAmountEntered = self.amount.isNotBlank
|
||||||
}
|
}
|
||||||
|
|
||||||
TextField("Usage", text: $usage)
|
UIKitTextField("Usage", text: $usage) {
|
||||||
|
if self.isRequiredDataEntered() {
|
||||||
|
self.transferMoney()
|
||||||
|
}
|
||||||
|
}
|
||||||
.onReceive(Just($usage)) { newValue in
|
.onReceive(Just($usage)) { newValue in
|
||||||
self.isValidUsageEntered = true
|
self.isValidUsageEntered = true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue