Add item group
This commit is contained in:
parent
e1d804fb6c
commit
c4e0f91e98
|
@ -6,7 +6,7 @@ import PackageDescription
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "UIPreview",
|
name: "UIPreview",
|
||||||
platforms: [
|
platforms: [
|
||||||
.iOS(.v13)
|
.iOS(.v11)
|
||||||
],
|
],
|
||||||
products: [
|
products: [
|
||||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
#if canImport(SwiftUI)
|
||||||
|
import SwiftUI
|
||||||
|
#endif
|
||||||
|
|
||||||
|
@available(iOS 13, *)
|
||||||
|
struct GroupItem: View {
|
||||||
|
typealias Element = (title: String, builder: () -> AnyView)
|
||||||
|
|
||||||
|
let items: [Element]
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack {
|
||||||
|
ForEach(items, id: \.title) { item in
|
||||||
|
Group {
|
||||||
|
Text(item.title)
|
||||||
|
.font(.headline)
|
||||||
|
.fontWeight(.bold)
|
||||||
|
Divider()
|
||||||
|
item.builder()
|
||||||
|
.frame(maxWidth: .infinity)
|
||||||
|
}
|
||||||
|
}.padding(.bottom, 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
@available(iOS 13, *)
|
||||||
|
struct GroupItem_Previews: PreviewProvider {
|
||||||
|
static var previews: some View {
|
||||||
|
ScrollView {
|
||||||
|
GroupItem(items: [
|
||||||
|
("Group 1", { AnyView(Text("Preview")) } ),
|
||||||
|
("Group 2", { AnyView(Image(systemName: "square.and.pencil")) } )
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
|
@ -29,6 +29,21 @@ public extension UICatalog.PreviewDescriptor {
|
||||||
title: String? = nil) where Content: UIViewCatalogPresentable {
|
title: String? = nil) where Content: UIViewCatalogPresentable {
|
||||||
id = "\(content)"
|
id = "\(content)"
|
||||||
self.title = title ?? "\(content)"
|
self.title = title ?? "\(content)"
|
||||||
self.builder = { AnyView(CatalogItem<Content>(configuration: configuration)) }
|
builder = { AnyView(CatalogItem<Content>(configuration: configuration)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
init(_ content: UICatalog.PreviewDescriptor...,
|
||||||
|
configuration: UICatalog.PreviewConfiguration = .init(),
|
||||||
|
title: String? = nil) {
|
||||||
|
self.init(content, configuration: configuration, title: title)
|
||||||
|
}
|
||||||
|
|
||||||
|
init(_ content: [UICatalog.PreviewDescriptor],
|
||||||
|
configuration: UICatalog.PreviewConfiguration = .init(),
|
||||||
|
title: String? = nil) {
|
||||||
|
id = content.map(\.id).joined()
|
||||||
|
self.title = title ?? content.map(\.title).joined(separator: " ")
|
||||||
|
let items: [GroupItem.Element] = content.map { ($0.title, $0.builder) }
|
||||||
|
builder = { AnyView(GroupItem(items: items)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue