Implemented isFocussedChanged

This commit is contained in:
dankito 2020-08-24 12:11:43 +02:00
parent 1da6686e86
commit 22c262f4c4
2 changed files with 21 additions and 4 deletions

View File

@ -24,6 +24,8 @@ struct LabelledUIKitTextField: View {
)
}
var isFocussedChanged: ((Bool) -> Void)? = nil
var isUserInputEnabled: Bool = true
@ -45,7 +47,7 @@ struct LabelledUIKitTextField: View {
UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField,
focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, focusTextField: focusTextFieldBinding,
textAlignment: .right, isUserInputEnabled: isUserInputEnabled,
isFocussedChanged: isFocussedChanged, textAlignment: .right, isUserInputEnabled: isUserInputEnabled,
actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged)
}
}

View File

@ -17,6 +17,8 @@ struct UIKitTextField: UIViewRepresentable {
private var focusNextTextFieldOnReturnKeyPress = false
@Binding private var focusTextField: Bool
private var isFocussedChanged: ((Bool) -> Void)? = nil
private var textAlignment: NSTextAlignment = .natural
private var isUserInputEnabled: Bool = true
@ -27,6 +29,7 @@ struct UIKitTextField: UIViewRepresentable {
init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false,
focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, focusTextField: Binding<Bool> = .constant(false),
isFocussedChanged: ((Bool) -> Void)? = nil,
textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true,
actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
self.placeholder = titleKey
@ -38,6 +41,7 @@ struct UIKitTextField: UIViewRepresentable {
self.focusOnStart = focusOnStart
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
self._focusTextField = focusTextField
self.isFocussedChanged = isFocussedChanged
self.textAlignment = textAlignment
self.isUserInputEnabled = isUserInputEnabled
@ -84,8 +88,8 @@ struct UIKitTextField: UIViewRepresentable {
func makeCoordinator() -> UIKitTextField.Coordinator {
return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, isUserInputEnabled: isUserInputEnabled,
actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged)
return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, isFocussedChanged: isFocussedChanged,
isUserInputEnabled: isUserInputEnabled, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged)
}
@ -95,6 +99,8 @@ struct UIKitTextField: UIViewRepresentable {
private var focusNextTextFieldOnReturnKeyPress: Bool
private var isFocussedChanged: ((Bool) -> Void)? = nil
private var isUserInputEnabled: Bool
private var actionOnReturnKeyPress: (() -> Bool)?
@ -102,11 +108,12 @@ struct UIKitTextField: UIViewRepresentable {
private var textChanged: ((String) -> Void)?
init(text: Binding<String>, focusNextTextFieldOnReturnKeyPress: Bool, isUserInputEnabled: Bool,
init(text: Binding<String>, focusNextTextFieldOnReturnKeyPress: Bool, isFocussedChanged: ((Bool) -> Void)? = nil, isUserInputEnabled: Bool,
actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
_text = text
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
self.isFocussedChanged = isFocussedChanged
self.isUserInputEnabled = isUserInputEnabled
self.actionOnReturnKeyPress = actionOnReturnKeyPress
@ -116,9 +123,17 @@ struct UIKitTextField: UIViewRepresentable {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if isUserInputEnabled {
isFocussedChanged?(true)
}
return isUserInputEnabled
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
isFocussedChanged?(false)
}
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