Skip to content

Commit d93c451

Browse files
committed
Replace state change callback closure with an actual type.
Closes #2
1 parent 6faa0e5 commit d93c451

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Demo Project/ResponsiveTextFieldDemo/ContentView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct ContentView: View {
6464
text: $email,
6565
firstResponderDemand: $emailResponderDemand.animation(),
6666
configuration: .email,
67-
onFirstResponderStateChanged: { isFirstResponder in
67+
onFirstResponderStateChanged: .init { isFirstResponder in
6868
withAnimation {
6969
isEmailFirstResponder = isFirstResponder
7070
}
@@ -90,7 +90,7 @@ struct ContentView: View {
9090
isSecure: hidePassword,
9191
firstResponderDemand: $passwordResponderDemand.animation(),
9292
configuration: .combine(.password, .lastOfChain),
93-
onFirstResponderStateChanged: { isFirstResponder in
93+
onFirstResponderStateChanged: .init { isFirstResponder in
9494
withAnimation {
9595
isPasswordFirstResponder = isFirstResponder
9696
}

Sources/ResponsiveTextField/ResponsiveTextField.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import SwiftUI
1010
/// A SwiftUI wrapper around UITextField that gives precise control over the responder state.
1111
///
1212
public struct ResponsiveTextField {
13-
/// Communicates the text field's first responder state using a boolean value (is first responder).
14-
public typealias FirstResponderStateChangeHandler = (Bool) -> Void
15-
1613
/// The text field placeholder string
1714
let placeholder: String
1815

@@ -145,6 +142,23 @@ public struct ResponsiveTextField {
145142

146143
// MARK: - Managing the first responder state
147144

145+
public struct FirstResponderStateChangeHandler {
146+
/// A closure that will be called when the first responder state changes.
147+
///
148+
/// - Parameters:
149+
/// - Bool: A boolean indicating if the text field is now the first responder or not.
150+
///
151+
public var handleStateChange: (Bool) -> Void
152+
153+
public init(handleStateChange: @escaping (Bool) -> Void) {
154+
self.handleStateChange = handleStateChange
155+
}
156+
157+
func callAsFunction(_ isFirstResponder: Bool) {
158+
handleStateChange(isFirstResponder)
159+
}
160+
}
161+
148162
/// Represents a request to change the text field's first responder state.
149163
///
150164
public enum FirstResponderDemand: Equatable {

0 commit comments

Comments
 (0)