Add support for ForEach
This commit is contained in:
parent
cccf048d67
commit
b17f05a6a0
@ -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: {
|
||||
|
||||
63
Sources/Core/View/ForEach.swift
Normal file
63
Sources/Core/View/ForEach.swift
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -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)")
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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)")
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)")
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user