Implemented textChanged listener
This commit is contained in:
parent
2e62a23e63
commit
014e19d4e8
|
@ -18,11 +18,13 @@ struct UIKitTextField: UIViewRepresentable {
|
||||||
|
|
||||||
private var actionOnReturnKeyPress: (() -> Bool)? = nil
|
private var actionOnReturnKeyPress: (() -> Bool)? = nil
|
||||||
|
|
||||||
|
private var textChanged: ((String) -> Void)? = nil
|
||||||
|
|
||||||
@State private var textField: UITextField? = nil
|
@State private var textField: UITextField? = nil
|
||||||
|
|
||||||
|
|
||||||
init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false,
|
init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false,
|
||||||
focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil) {
|
focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
|
||||||
self.placeHolder = titleKey
|
self.placeHolder = titleKey
|
||||||
_text = text
|
_text = text
|
||||||
|
|
||||||
|
@ -33,6 +35,7 @@ struct UIKitTextField: UIViewRepresentable {
|
||||||
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
|
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
|
||||||
|
|
||||||
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
||||||
|
self.textChanged = textChanged
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,7 +72,7 @@ struct UIKitTextField: UIViewRepresentable {
|
||||||
|
|
||||||
|
|
||||||
func makeCoordinator() -> UIKitTextField.Coordinator {
|
func makeCoordinator() -> UIKitTextField.Coordinator {
|
||||||
return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, actionOnReturnKeyPress: actionOnReturnKeyPress)
|
return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,19 +92,30 @@ struct UIKitTextField: UIViewRepresentable {
|
||||||
private var focusNextTextFieldOnReturnKeyPress: Bool
|
private var focusNextTextFieldOnReturnKeyPress: Bool
|
||||||
|
|
||||||
private var actionOnReturnKeyPress: (() -> Bool)?
|
private var actionOnReturnKeyPress: (() -> Bool)?
|
||||||
|
|
||||||
|
private var textChanged: ((String) -> Void)?
|
||||||
|
|
||||||
|
|
||||||
init(text: Binding<String>, focusNextTextFieldOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil) {
|
init(text: Binding<String>, focusNextTextFieldOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
|
||||||
_text = text
|
_text = text
|
||||||
|
|
||||||
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
|
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
|
||||||
|
|
||||||
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
self.actionOnReturnKeyPress = actionOnReturnKeyPress
|
||||||
|
|
||||||
|
self.textChanged = textChanged
|
||||||
}
|
}
|
||||||
|
|
||||||
func textFieldDidChangeSelection(_ textField: UITextField) {
|
func textFieldDidChangeSelection(_ textField: UITextField) {
|
||||||
|
let newText = textField.text ?? ""
|
||||||
|
let didTextChange = newText != text // e.g. if just the cursor has been placed to another position then textFieldDidChangeSelection() gets called but text didn't change
|
||||||
|
|
||||||
DispatchQueue.main.async { // to not update state during view update
|
DispatchQueue.main.async { // to not update state during view update
|
||||||
self.text = textField.text ?? ""
|
self.text = newText
|
||||||
|
|
||||||
|
if didTextChange {
|
||||||
|
self.textChanged?(newText)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,14 +128,14 @@ struct EnterTanDialog: View {
|
||||||
.padding(.vertical, 2)
|
.padding(.vertical, 2)
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
UIKitTextField("Enter TAN:", text: $enteredTan) {
|
UIKitTextField("Enter TAN:", text: $enteredTan, actionOnReturnKeyPress: {
|
||||||
if self.isRequiredDataEntered() {
|
if self.isRequiredDataEntered() {
|
||||||
self.enteringTanDone()
|
self.enteringTanDone()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
|
|
|
@ -90,8 +90,7 @@ struct TransferMoneyDialog: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
UIKitTextField("Remittee Name", text: $remitteeName, focusOnStart: true, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress)
|
UIKitTextField("Remittee Name", text: $remitteeName, focusOnStart: true, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) { newValue in
|
||||||
.onReceive(Just(remitteeName)) { newValue in
|
|
||||||
self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank
|
self.isValidRemitteeNameEntered = self.remitteeName.isNotBlank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,16 +104,14 @@ struct TransferMoneyDialog: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UIKitTextField("Remittee IBAN", text: $remitteeIban, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress)
|
UIKitTextField("Remittee IBAN", text: $remitteeIban, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) { 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
UIKitTextField("Amount", text: $amount, keyboardType: .decimalPad, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress)
|
UIKitTextField("Amount", text: $amount, keyboardType: .decimalPad, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) { newValue in
|
||||||
.onReceive(Just(amount)) { newValue in
|
|
||||||
// TODO: implement DecimalTextField / NumericTextField
|
// TODO: implement DecimalTextField / NumericTextField
|
||||||
let filtered = newValue.filter { "0123456789,".contains($0) }
|
let filtered = newValue.filter { "0123456789,".contains($0) }
|
||||||
if filtered != newValue {
|
if filtered != newValue {
|
||||||
|
@ -124,8 +121,7 @@ struct TransferMoneyDialog: View {
|
||||||
self.isValidAmountEntered = self.amount.isNotBlank
|
self.isValidAmountEntered = self.amount.isNotBlank
|
||||||
}
|
}
|
||||||
|
|
||||||
UIKitTextField("Usage", text: $usage, actionOnReturnKeyPress: handleReturnKeyPress)
|
UIKitTextField("Usage", text: $usage, actionOnReturnKeyPress: handleReturnKeyPress) { newValue in
|
||||||
.onReceive(Just($usage)) { newValue in
|
|
||||||
self.isValidUsageEntered = true
|
self.isValidUsageEntered = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +137,7 @@ struct TransferMoneyDialog: View {
|
||||||
Spacer()
|
Spacer()
|
||||||
Button(action: { self.transferMoney() },
|
Button(action: { self.transferMoney() },
|
||||||
label: { Text("Transfer Money") })
|
label: { Text("Transfer Money") })
|
||||||
.disabled(!self.isRequiredDataEntered())
|
.disabled( !self.isRequiredDataEntered())
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue