Add support for ForEach
All checks were successful
Deploy Docs / publish (push) Successful in 3m39s
SwiftLint / SwiftLint (push) Successful in 5s

This commit is contained in:
david-swift 2024-12-31 09:55:02 +01:00
parent cccf048d67
commit b17f05a6a0
6 changed files with 70 additions and 7 deletions

View File

@ -101,7 +101,7 @@ public struct Alert: SwiftUIWidget {
public static func view(properties: Self) -> some SwiftUI.View {
MacBackendView(.mainContent)
.alert(properties.title, isPresented: properties.isPresented.swiftUI) {
ForEach(Array(properties.actions.enumerated()), id: \.offset) { action in
SwiftUI.ForEach(Array(properties.actions.enumerated()), id: \.offset) { action in
action.element.button
}
} message: {

View File

@ -0,0 +1,63 @@
//
// ForEach.swift
// MacBackend
//
// Created by david-swift on 31.12.2024.
//
import SwiftUI
/// Align a dynamic number of items horizontally or vertically.
public struct ForEach<Item>: SwiftUIWidget where Item: Identifiable {
/// The data.
var data: [Item]
/// The content.
var content: (Item) -> Body
/// Whether the items are aligned horizontally.
var horizontal: Bool
/// The wrapped views.
public var wrappedViews: [String: Meta.AnyView] {
data.reduce([:]) { partialResult, item in
var partialResult = partialResult
partialResult[item.id.hashValue.description] = content(item)
return partialResult
}
}
/// Initialize a ``ForEach`` view.
/// - Parameters:
/// - data: The data.
/// - horizontal: Whether the items are aligned horizontally.
/// - content: The content for each data item.
public init(
_ data: [Item],
horizontal: Bool = false,
@Meta.ViewBuilder content: @escaping (Item) -> Body
) {
self.data = data
self.content = content
self.horizontal = horizontal
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
@SwiftUI.ViewBuilder
public static func view(properties: Self) -> some SwiftUI.View {
let forEach = SwiftUI.ForEach(properties.data) { item in
MacBackendView(item.id.hashValue.description)
}
if properties.horizontal {
SwiftUI.HStack {
forEach
}
} else {
SwiftUI.VStack {
forEach
}
}
}
}

View File

@ -31,7 +31,7 @@ public struct HStack: SwiftUIWidget {
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
SwiftUI.HStack {
ForEach(properties.content.indices, id: \.self) { index in
SwiftUI.ForEach(properties.content.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}

View File

@ -35,7 +35,7 @@ public struct Picker<Item>: SwiftUIWidget where Item: Hashable, Item: CustomStri
/// - Returns: The SwiftUI view.
public static func view(properties: Picker<Item>) -> some SwiftUI.View {
let picker = SwiftUI.Picker(properties.label, selection: properties._selection.swiftUI) {
ForEach(properties.items, id: \.hashValue) { item in
SwiftUI.ForEach(properties.items, id: \.hashValue) { item in
SwiftUI.Text(item.description)
.tag(item.description)
}

View File

@ -55,7 +55,7 @@ public struct ToolbarView: SwiftUIWidget {
case .end:
MacBackendView(.mainContent)
.toolbar {
ForEach(properties.toolbarViews.indices, id: \.self) { index in
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
@ -64,7 +64,7 @@ public struct ToolbarView: SwiftUIWidget {
.toolbar {
ToolbarItem(placement: .navigation) {
SwiftUI.HStack {
ForEach(properties.toolbarViews.indices, id: \.self) { index in
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}
@ -75,7 +75,7 @@ public struct ToolbarView: SwiftUIWidget {
.toolbar {
ToolbarItem(placement: .principal) {
SwiftUI.HStack {
ForEach(properties.toolbarViews.indices, id: \.self) { index in
SwiftUI.ForEach(properties.toolbarViews.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}

View File

@ -31,7 +31,7 @@ public struct VStack: SwiftUIWidget, Wrapper {
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
SwiftUI.VStack {
ForEach(properties.content.indices, id: \.self) { index in
SwiftUI.ForEach(properties.content.indices, id: \.self) { index in
MacBackendView("\(index)")
}
}