Skip to content

Commit

Permalink
Provide a convenience for creating a change handler that just updates
Browse files Browse the repository at this point in the history
some external state using a binding.
  • Loading branch information
lukeredpath committed Mar 16, 2021
1 parent f68de6d commit 1be0dd8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Demo Project/ResponsiveTextFieldDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ struct ContentView: View {
isSecure: hidePassword,
firstResponderDemand: $passwordResponderDemand.animation(),
configuration: .combine(.password, .lastOfChain),
onFirstResponderStateChanged: FirstResponderStateChangeHandler { isFirstResponder in
isPasswordFirstResponder = isFirstResponder
}.animation(),
onFirstResponderStateChanged: FirstResponderStateChangeHandler
.updates($isPasswordFirstResponder)
.animation(),
handleReturn: { passwordResponderDemand = .shouldResignFirstResponder },
handleDelete: {
if $0.isEmpty {
Expand Down
14 changes: 14 additions & 0 deletions Sources/ResponsiveTextField/ResponsiveTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ public struct FirstResponderStateChangeHandler {
}
}

extension FirstResponderStateChangeHandler {
/// Returns a change handler that updates a `Bool` binding with the `isFirstResponder`
/// value whenever it changes.
///
/// - Parameters:
/// - binding: A binding to some Boolean state property that should be updated.
///
public static func updates(_ binding: Binding<Bool>) -> Self {
.init { isFirstResponder in
binding.wrappedValue = isFirstResponder
}
}
}

/// Represents a request to change the text field's first responder state.
///
public enum FirstResponderDemand: Equatable {
Expand Down

0 comments on commit 1be0dd8

Please sign in to comment.