diff --git a/Development/Development/BookCollectionView.swift b/Development/Development/BookCollectionView.swift index 37caa2f..f128056 100644 --- a/Development/Development/BookCollectionView.swift +++ b/Development/Development/BookCollectionView.swift @@ -418,3 +418,41 @@ struct BookPlatformList: View, PreviewProvider { return Preview() } + +#Preview("Simple grid layout") { + + struct Book: View { + + @State var selected: Set = .init() + + var body: some View { + + CollectionView( + layout: .grid, + content: { + SelectableForEach( + data: Item.mock(), + selection: .multiple( + selected: selected, + canSelectMore: selected.count < 3, + onChange: { e, action in + switch action { + case .selected: + selected.insert(e) + case .deselected: + selected.remove(e) + } + } + ), + selectionIdentifier: \.id, + cell: { index, item in + Cell(index: index, item: item) + } + ) + } + ) + } + } + + return Book() +} diff --git a/Sources/CollectionView/CollectionViewLayout.swift b/Sources/CollectionView/CollectionViewLayout.swift index 6cd1b72..c3125c3 100644 --- a/Sources/CollectionView/CollectionViewLayout.swift +++ b/Sources/CollectionView/CollectionViewLayout.swift @@ -155,36 +155,73 @@ public enum CollectionViewLayouts { } public struct Grid: CollectionViewLayoutType { + + public let gridItems: [GridItem] - public let columns: [GridItem] - public let spacing: CGFloat? + public let direction: CollectionViewListDirection + + public var showsIndicators: Bool = false + public var contentPadding: EdgeInsets + public var spacing: CGFloat? + public init( - columns: [GridItem], + gridItems: [GridItem], + direction: CollectionViewListDirection, spacing: CGFloat? = nil, contentPadding: EdgeInsets = .init() ) { - self.columns = columns + self.direction = direction + self.contentPadding = contentPadding + self.gridItems = gridItems self.spacing = spacing self.contentPadding = contentPadding } + public consuming func contentPadding(_ contentPadding: EdgeInsets) -> Self { + + self.contentPadding = contentPadding + + return self + } + + public consuming func showsIndicators(_ showsIndicators: Bool) -> Self { + + self.showsIndicators = showsIndicators + + return self + } + public func body(content: Content) -> some View { - ScrollView { - LazyVGrid(columns: columns, spacing: spacing) { - content + switch direction { + case .vertical: + + ScrollView(.vertical, showsIndicators: showsIndicators) { + LazyVGrid( + columns: gridItems, + spacing: spacing + ) { + content + } + .padding(contentPadding) } - .padding(contentPadding) + + case .horizontal: + + ScrollView(.horizontal, showsIndicators: showsIndicators) { + LazyHGrid( + rows: gridItems, + spacing: spacing + ) { + content + } + .padding(contentPadding) + } + } } - public consuming func contentPadding(_ contentPadding: EdgeInsets) -> Self { - - self.contentPadding = contentPadding - - return self - } } } @@ -202,10 +239,15 @@ extension CollectionViewLayoutType where Self == CollectionViewLayouts.List Self { - CollectionViewLayouts.Grid(columns: columns, spacing: spacing) + CollectionViewLayouts.Grid( + gridItems: gridItems, + direction: direction, + spacing: spacing + ) } }