A wrapper to create any number of arbitrary state variables using parameter packs for testing bindable values in a preview.
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
import SwiftUI | |
struct PreviewState<each T, Content: View>: View { | |
@State private var value: (repeat each T) | |
var content: (Binding<(repeat each T)>) -> Content | |
init( | |
_ value: repeat each T, | |
@ViewBuilder content: @escaping (Binding<(repeat each T)>) -> Content | |
) { | |
self._value = State(initialValue: (repeat each value)) | |
self.content = content | |
} | |
var body: some View { | |
content($value) | |
} | |
} | |
#Preview { | |
VStack { | |
PreviewState(false) { | |
Toggle("First Value", isOn: $0) | |
} | |
PreviewState(false, 1, 10.0) { | |
Toggle("First Value", isOn: $0.0) | |
Stepper("Second Value", value: $0.1, in: 0...5) | |
Slider(value: $0.2, in: 0...100) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment