Add item group

This commit is contained in:
Alexandr Goncharov 2020-10-16 16:21:22 +03:00
parent e1d804fb6c
commit c4e0f91e98
3 changed files with 56 additions and 2 deletions

View File

@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "UIPreview",
platforms: [
.iOS(.v13)
.iOS(.v11)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.

View File

@ -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

View File

@ -29,6 +29,21 @@ public extension UICatalog.PreviewDescriptor {
title: String? = nil) where Content: UIViewCatalogPresentable {
id = "\(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)) }
}
}