-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #807 from kiwicom/806-expose-bindingsource-as-public
Expose `BindingSource`
- Loading branch information
Showing
3 changed files
with
41 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,43 @@ | ||
import SwiftUI | ||
|
||
/// A view that provides a binding to its content. | ||
/// A binding source for the ``OptionalBinding``. | ||
public enum OptionalBindingSource<Value> { | ||
case binding(Binding<Value>) | ||
case state(Value) | ||
} | ||
|
||
/// A view that provides either a binding to its content or an internal state, based on provided ``OptionalBindingSource`` value. | ||
/// | ||
/// This binding can either be supplied, in which case it is used directly, | ||
/// or one is derived from internal state (starting with `defaultValue`). | ||
/// The binding can either be supplied, in which case it is used directly, | ||
/// or one is derived from internal state. | ||
/// | ||
/// This is is useful for components that can manage their own state, | ||
/// but we also want to make it possible for that state to be driven | ||
/// from the outside if a binding is passed. | ||
struct BindingSource<Value, Content: View>: View { | ||
|
||
let outer: Binding<Value>? | ||
@State var inner: Value | ||
/// This is is useful for components that need to manage their own state, | ||
/// but also allow that state to be overridden | ||
/// using the binding provided from the outside. | ||
public struct OptionalBinding<Value, Content: View>: View { | ||
let binding: Binding<Value>? | ||
@State var state: Value | ||
let content: (Binding<Value>) -> Content | ||
|
||
var body: some View { | ||
content(outer ?? $inner) | ||
public var body: some View { | ||
content(binding ?? $state) | ||
} | ||
|
||
init( | ||
_ binding: Binding<Value>?, | ||
fallbackInitialValue: Value, | ||
/// Create a view with either a binding to its content or an internal state. | ||
public init( | ||
_ source: OptionalBindingSource<Value>, | ||
@ViewBuilder content: @escaping (Binding<Value>) -> Content | ||
) { | ||
self.outer = binding | ||
self._inner = State(wrappedValue: fallbackInitialValue) | ||
switch source { | ||
case .binding(let binding): | ||
self.binding = binding | ||
self._state = .init(wrappedValue: binding.wrappedValue) | ||
case .state(let value): | ||
self.binding = nil | ||
self._state = State(wrappedValue: value) | ||
} | ||
|
||
self.content = content | ||
} | ||
} |