Files
TUIkit/Sources/TUIKit/Views/ForEach.swift
T
phranck 45e73faafb refactor: Rename package from SwiftTUI to TUIKit
BREAKING CHANGE: Package name changed due to name collision with
existing rensbreur/SwiftTUI package.

Changes:
- Rename package from SwiftTUI to TUIKit in Package.swift
- Rename Sources/SwiftTUI to Sources/TUIKit
- Rename Sources/SwiftTUIExample to Sources/TUIKitExample
- Rename Tests/SwiftTUITests to Tests/TUIKitTests
- Rename SwiftTUI.swift to TUIKit.swift
- Update all imports: import SwiftTUI -> import TUIKit
- Update all code references: SwiftTUI.renderToBuffer -> TUIKit.renderToBuffer
- Update documentation comments
- Rename swiftTUIVersion to tuiKitVersion

All 181 tests passing.
2026-01-28 19:32:09 +01:00

108 lines
2.8 KiB
Swift

//
// ForEach.swift
// TUIKit
//
// Iteration over data collections for view generation.
//
/// A view that generates views from a collection of data.
///
/// `ForEach` iterates over a collection and creates a view for each
/// element. The collection elements must be `Identifiable` or an
/// explicit ID key path must be provided.
///
/// # Example with Identifiable
///
/// ```swift
/// struct Item: Identifiable {
/// let id: String
/// let name: String
/// }
///
/// let items = [Item(id: "1", name: "One"), Item(id: "2", name: "Two")]
///
/// VStack {
/// ForEach(items) { item in
/// Text(item.name)
/// }
/// }
/// ```
///
/// # Example with explicit ID key path
///
/// ```swift
/// let names = ["Anna", "Bob", "Clara"]
///
/// VStack {
/// ForEach(names, id: \.self) { name in
/// Text(name)
/// }
/// }
/// ```
public struct ForEach<Data: RandomAccessCollection, ID: Hashable, Content: View>: View {
/// The underlying data collection.
public let data: Data
/// The key path to the unique ID of each element.
public let idKeyPath: KeyPath<Data.Element, ID>
/// The closure that creates a view for each element.
public let content: (Data.Element) -> Content
/// Creates a ForEach with an explicit ID key path.
///
/// - Parameters:
/// - data: The collection to iterate over.
/// - id: The key path to the unique ID of each element.
/// - content: The closure that creates the view for each element.
public init(
_ data: Data,
id: KeyPath<Data.Element, ID>,
@ViewBuilder content: @escaping (Data.Element) -> Content
) {
self.data = data
self.idKeyPath = id
self.content = content
}
public var body: Never {
fatalError("ForEach renders its children directly")
}
}
// MARK: - ForEach with Identifiable
extension ForEach where Data.Element: Identifiable, ID == Data.Element.ID {
/// Creates a ForEach for Identifiable elements.
///
/// - Parameters:
/// - data: The collection with Identifiable elements.
/// - content: The closure that creates the view for each element.
public init(
_ data: Data,
@ViewBuilder content: @escaping (Data.Element) -> Content
) {
self.data = data
self.idKeyPath = \Data.Element.id
self.content = content
}
}
// MARK: - ForEach with Range
extension ForEach where Data == Range<Int>, ID == Int {
/// Creates a ForEach over an integer range.
///
/// - Parameters:
/// - data: The range, e.g., `0..<10`.
/// - content: The closure that creates the view for each index.
public init(
_ data: Range<Int>,
@ViewBuilder content: @escaping (Int) -> Content
) {
self.data = data
self.idKeyPath = \.self
self.content = content
}
}