Last active
November 3, 2022 10:51
-
-
Save nkmrh/4c1f213af138f56c2674ebc0a8e152c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class _TextFieldCoordinator: NSObject { | |
var control: _TextField | |
init(_ control: _TextField) { | |
self.control = control | |
super.init() | |
control.textField.addTarget(self, action: #selector(textFieldEditingDidBegin(_:)), for: .editingDidBegin) | |
control.textField.addTarget(self, action: #selector(textFieldEditingDidEnd(_:)), for: .editingDidEnd) | |
control.textField.addTarget(self, action: #selector(textFieldEditingChanged(_:)), for: .editingChanged) | |
control.textField.addTarget(self, action: #selector(textFieldEditingDidEndOnExit(_:)), for: .editingDidEndOnExit) | |
} | |
@objc private func textFieldEditingDidBegin(_ textField: UITextField) { | |
control.onEditingChanged(true) | |
} | |
@objc private func textFieldEditingDidEnd(_ textField: UITextField) { | |
control.onEditingChanged(false) | |
} | |
@objc private func textFieldEditingChanged(_ textField: UITextField) { | |
control.text = textField.text ?? "" | |
} | |
@objc private func textFieldEditingDidEndOnExit(_ textField: UITextField) { | |
control.onCommit() | |
} | |
} | |
struct _TextField: UIViewRepresentable { | |
private let title: String? | |
@Binding var text: String | |
let onEditingChanged: (Bool) -> Void | |
let onCommit: () -> Void | |
let textField = UITextField() | |
init(title: String?, text: Binding<String>, onEditingChanged: @escaping (Bool) -> Void = { _ in }, onCommit: @escaping () -> Void = {}) { | |
self.title = title | |
self._text = text | |
self.onEditingChanged = onEditingChanged | |
self.onCommit = onCommit | |
} | |
func makeCoordinator() -> _TextFieldCoordinator { | |
_TextFieldCoordinator(self) | |
} | |
func makeUIView(context: Context) -> UITextField { | |
textField.placeholder = title | |
textField.delegate = context.coordinator | |
return textField | |
} | |
func updateUIView(_ uiView: UITextField, context: Context) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment