Skip to content

Commit 27e7acd

Browse files
committed
Add support fot handling delete key presses.
Using a sub-class with an overridden deleteBackward() allows detection of delete presses on empty fields which can be useful for navigating back to a previous field.
1 parent 4fd6dd9 commit 27e7acd

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Demo Project/ResponsiveTextFieldDemo/ContentView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct ContentView: View {
5151
isEditing: $isEditingPassword.animation(),
5252
isSecure: hidePassword,
5353
configuration: .combine(.password, .lastOfChain),
54-
handleReturn: { isEditingPassword = false }
54+
handleReturn: { isEditingPassword = false },
55+
handleDelete: { isEditingEmail = $0.isEmpty }
5556
)
5657
.fixedSize(horizontal: false, vertical: true)
5758
.disabled(!isEnabled)

ResponsiveTextField.xcworkspace/xcshareddata/xcschemes/ResponsiveTextField.xcscheme

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
shouldUseLaunchSchemeArgsEnv = "YES">
3030
<Testables>
3131
<TestableReference
32-
skipped = "NO">
32+
skipped = "NO"
33+
testExecutionOrdering = "random">
3334
<BuildableReference
3435
BuildableIdentifier = "primary"
3536
BlueprintIdentifier = "ResponsiveTextFieldTests"

Sources/ResponsiveTextField/ResponsiveTextField.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public struct ResponsiveTextField {
5959
/// If this is not set, the textfield delegate will indicate that the return key is not handled.
6060
var handleReturn: (() -> Void)?
6161

62+
/// A callback function that will be called when the user deletes backwards.
63+
///
64+
/// Takes a single argument - a `String` - which will be the current text when the user
65+
/// hits the delete key (but before any deletion occurs).
66+
///
67+
/// If this is an empty string, it indicates that the user tapped delete inside an empty field.
68+
var handleDelete: ((String) -> Void)?
69+
70+
/// A callback function that can be used to control whether or not text should change.
71+
///
72+
/// Takes two `String` arguments - the text prior to the change and the new text if
73+
/// the change is permitted.
74+
///
75+
/// Return `true` to allow the change or `false` to prevent the change.
6276
var shouldChange: ((String, String) -> Bool)?
6377

6478
public init(
@@ -68,6 +82,7 @@ public struct ResponsiveTextField {
6882
isSecure: Bool = false,
6983
configuration: Configuration = .empty,
7084
handleReturn: (() -> Void)? = nil,
85+
handleDelete: ((String) -> Void)? = nil,
7186
shouldChange: ((String, String) -> Bool)? = nil
7287
) {
7388
self.placeholder = placeholder
@@ -76,6 +91,7 @@ public struct ResponsiveTextField {
7691
self.isSecure = isSecure
7792
self.configuration = configuration
7893
self.handleReturn = handleReturn
94+
self.handleDelete = handleDelete
7995
self.shouldChange = shouldChange
8096
}
8197
}
@@ -84,9 +100,10 @@ public struct ResponsiveTextField {
84100

85101
extension ResponsiveTextField: UIViewRepresentable {
86102
public func makeUIView(context: Context) -> UITextField {
87-
let textField = UITextField()
103+
let textField = DeleteHandlingTextField()
88104
configuration.configure(textField)
89105
// This stops the text field from expanding if the text overflows the frame width
106+
textField.handleDelete = handleDelete
90107
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
91108
textField.placeholder = placeholder
92109
textField.text = text.wrappedValue
@@ -181,6 +198,15 @@ extension ResponsiveTextField: UIViewRepresentable {
181198
}
182199
}
183200

201+
private class DeleteHandlingTextField: UITextField {
202+
var handleDelete: ((String) -> Void)?
203+
204+
override func deleteBackward() {
205+
handleDelete?(text ?? "")
206+
super.deleteBackward()
207+
}
208+
}
209+
184210
// MARK: - TextField Configurations
185211

186212
extension ResponsiveTextField {

0 commit comments

Comments
 (0)