Skip to content

Commit

Permalink
feat: add binding on change function
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed May 8, 2021
1 parent 522e5b7 commit 48c1b67
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Sources/ExtensionKit/SwiftUI/Binding.swift
@@ -0,0 +1,33 @@
import SwiftUI

public extension Binding where Value: Equatable {

/**
```
Toggle(
"Foo",
isOn: $foo.onChange {
bar.isEnabled = $0
}
)
```
*/

/// Update or run action after binding has changed
/// - Parameter action: Action to run
/// - Returns: Binding
func onChange(_ action: @escaping (Value) -> Void) -> Self {
.init(
get: { wrappedValue },
set: {
let oldValue = wrappedValue
wrappedValue = $0
let newValue = wrappedValue
if newValue != oldValue {
action(newValue)
}
}
)
}

}

0 comments on commit 48c1b67

Please sign in to comment.