Skip to content

Commit 77212f8

Browse files
committed
Add KeyPath-based List initializer overloads for older Swift
1 parent 498e9cd commit 77212f8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Sources/SwiftCrossUI/Views/List.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ public struct List<SelectionValue: Hashable, RowView: View>: TypeSafeView, View
4040
}
4141
}
4242

43+
public init<Data: RandomAccessCollection>(
44+
_ data: Data,
45+
id: KeyPath<Data.Element, SelectionValue>,
46+
selection: Binding<SelectionValue?>
47+
) where Data.Element: CustomStringConvertible, RowView == Text, Data.Index == Int {
48+
self.init(data, id: id, selection: selection) { item in
49+
return Text(item.description)
50+
}
51+
}
52+
53+
public init<Data: RandomAccessCollection>(
54+
_ data: Data,
55+
id: KeyPath<Data.Element, SelectionValue>,
56+
selection: Binding<SelectionValue?>,
57+
@ViewBuilder rowContent: @escaping (Data.Element) -> RowView
58+
) where Data.Index == Int {
59+
self.init(data, id: { $0[keyPath: id] }, selection: selection, rowContent: rowContent)
60+
}
61+
4362
public init<Data: RandomAccessCollection>(
4463
_ data: Data,
4564
id: @escaping (Data.Element) -> SelectionValue,

0 commit comments

Comments
 (0)