david-swift e989bea14e
All checks were successful
Deploy Docs / publish (push) Successful in 2m38s
Initial commit
2024-12-02 22:14:13 +01:00

52 lines
1.4 KiB
Swift

//
// List.swift
// MacBackend
//
// Created by david-swift on 23.11.2024.
//
import SwiftUI
/// A list widget.
public struct List<Element>: SwiftUIWidget where Element: Identifiable {
/// The elements.
var elements: [Element]
/// The selected element.
var selection: Meta.Binding<Element.ID?>
/// The content for an element.
var content: (Element) -> Body
/// The wrapped views.
public var wrappedViews: [String: any Meta.AnyView] {
elements.reduce(into: [:]) { partialResult, element in
partialResult["\(element.id)"] = content(element)
}
}
/// Initialize a list widget.
/// - Parameters:
/// - elements: The elements.
/// - selection: The selected element.
/// - content: The content for an element.
public init(
_ elements: [Element],
selection: Meta.Binding<Element.ID?>,
@Meta.ViewBuilder content: @escaping (Element) -> Body
) {
self.elements = elements
self.content = content
self.selection = selection
}
/// Get the SwiftUI view.
/// - Parameter properties: The widget data.
/// - Returns: The SwiftUI view.
public static func view(properties: Self) -> some SwiftUI.View {
SwiftUI.List(properties.elements, selection: properties.selection.swiftUI) { element in
MacBackendView("\(element.id)")
}
}
}