swiftui-navigation/Sources/SwiftUINavigation/WithState.swift

47 lines
1.2 KiB
Swift
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
/// A container view that provides a binding to another view.
///
/// This view is most helpful for creating Xcode previews of views that require bindings.
///
/// For example, if you wanted to create a preview for a text field, you cannot simply introduce
/// some `@State` to the preview since `previews` is static:
///
/// ```swift
/// struct TextField_Previews: PreviewProvider {
/// @State static var text = "" // @State static does not work.
///
/// static var previews: some View {
/// TextField("Test", text: self.$text)
/// }
/// }
/// ```
///
/// So, instead you can use ``WithState``:
///
/// ```swift
/// struct TextField_Previews: PreviewProvider {
/// static var previews: some View {
/// WithState(initialValue: "") { $text in
/// TextField("Test", text: $text)
/// }
/// }
/// }
/// ```
public struct WithState<Value, Content: View>: View {
@State var value: Value
@ViewBuilder let content: (Binding<Value>) -> Content
public init(
initialValue value: Value,
@ViewBuilder content: @escaping (Binding<Value>) -> Content
) {
self._value = State(wrappedValue: value)
self.content = content
}
public var body: some View {
self.content(self.$value)
}
}