Style group item

This commit is contained in:
Alexandr Goncharov 2020-10-16 17:57:33 +03:00
parent c4e0f91e98
commit 73a2f20d7d
3 changed files with 77 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
@available(iOS 13, *)
struct CatalogItem<Content: UIViewCatalogPresentable>: View {
@ -22,12 +23,35 @@ struct CatalogItem<Content: UIViewCatalogPresentable>: View {
.fontWeight(.bold)
Content.preview(with: model)
.frame(maxWidth: .infinity)
// .previewLayout(.sizeThatFits)
.background(Color(.systemBackground))
.colorScheme(scheme)
Divider()
.background(Color.secondary)
}
.padding()
}
}
#if DEBUG
@available(iOS 13, *)
struct CatalogItem_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
ScrollView {
CatalogItem<TestView>(configuration: .init())
}.navigationBarTitle("TestView")
}
}
}
private final class TestView: UILabel, UICatalogPresentable {
static var previewModels = [
"Hello world",
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
]
func apply(previewModel: String) {
text = previewModel
}
}
#endif

View File

@ -4,25 +4,58 @@ import SwiftUI
@available(iOS 13, *)
struct GroupItem: View {
typealias Element = (title: String, builder: () -> AnyView)
struct Model: Identifiable {
let id = UUID().uuidString
var isExpanded = false
let title: String
let content: () -> AnyView
}
let items: [Element]
let items: [Model]
var body: some View {
ForEach(items) { item in
GroupItemRow(model: item).buttonStyle(PlainButtonStyle())
}
}
}
@available(iOS 13, *)
struct GroupItemRow: View {
@State var model: GroupItem.Model
var body: some View {
VStack {
ForEach(items, id: \.title) { item in
Group {
Text(item.title)
Button(action: {
model.isExpanded.toggle()
}, label: {
HStack {
Text(model.title)
.font(.headline)
.fontWeight(.bold)
Divider()
item.builder()
if model.isExpanded {
Image(systemName: "chevron.up")
} else {
Image(systemName: "chevron.down")
}
}
.offset(x: 10)
.frame(maxWidth: .infinity)
}
}.padding(.bottom, 10)
.padding()
.background(Color.secondary)
.cornerRadius(8)
})
if model.isExpanded {
model.content()
.frame(maxWidth: .infinity)
.padding([.top, .bottom], /*@START_MENU_TOKEN@*/10/*@END_MENU_TOKEN@*/)
}
}
}
}
#if DEBUG
@available(iOS 13, *)
@ -30,8 +63,10 @@ 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")) } )
.init(title: "Group 1",
content: { AnyView(Text("Preview")) } ),
.init(title: "Group 2",
content: { AnyView(Image(systemName: "square.and.pencil")) } )
])
}
}

View File

@ -43,7 +43,7 @@ public extension UICatalog.PreviewDescriptor {
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) }
let items = content.map { GroupItem.Model(title: $0.title, content: $0.builder) }
builder = { AnyView(GroupItem(items: items)) }
}
}