Skip to content

Commit 36f87ac

Browse files
committed
Refactor slot children builder
1 parent 64fe424 commit 36f87ac

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

Sources/LiveViewNative/LiveContext.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ public struct LiveContext<R: CustomRegistry> {
4747
return coordinator.builder.fromNodes(element.children(), context: self)
4848
}
4949

50-
public func buildChildren(
51-
of element: ElementNode,
52-
`where` condition: (NodeChildrenSequence.Element) -> Bool
53-
) -> some View {
54-
ForEach(element.children().filter(condition).map({ ($0.id, $0.children()) }), id: \.0) { subChildren in
55-
coordinator.builder.fromNodes(subChildren.1, context: self)
56-
}
57-
}
58-
5950
public func buildChildren(
6051
of element: ElementNode,
6152
withTagName tagName: String,
62-
namespace: String? = nil
53+
namespace: String? = nil,
54+
includeDefaultSlot: Bool = false
6355
) -> some View {
64-
buildChildren(of: element, where: { child in
56+
let children = element.children()
57+
let namedSlotChildren = children.filter({ child in
6558
if case let .element(element) = child.data,
6659
element.namespace == namespace,
6760
element.tag == tagName
@@ -71,15 +64,21 @@ public struct LiveContext<R: CustomRegistry> {
7164
return false
7265
}
7366
})
74-
}
75-
76-
public func buildChildren(of element: ElementNode, defaultSlotFor tagName: String) -> some View {
77-
buildChildren(of: element, where: {
67+
let defaultSlotChildren = children.filter({
7868
if case let .element(element) = $0.data {
79-
return element.namespace != "menu"
69+
return element.namespace != tagName
8070
} else {
8171
return true
8272
}
8373
})
74+
if namedSlotChildren.isEmpty && includeDefaultSlot {
75+
return ForEach(defaultSlotChildren.map({ ($0.id, $0.children()) }), id: \.0) { subChildren in
76+
coordinator.builder.fromNodes(subChildren.1, context: self)
77+
}
78+
} else {
79+
return ForEach(namedSlotChildren.map({ ($0.id, $0.children()) }), id: \.0) { subChildren in
80+
coordinator.builder.fromNodes(subChildren.1, context: self)
81+
}
82+
}
8483
}
8584
}

Sources/LiveViewNative/Views/Controls and Indicators/Menus/Menu.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ struct Menu<R: CustomRegistry>: View {
1919
SwiftUI.Menu {
2020
context.buildChildren(of: element, withTagName: "content", namespace: "menu")
2121
} label: {
22-
context.buildChildren(of: element, defaultSlotFor: "menu")
23-
context.buildChildren(of: element, withTagName: "label", namespace: "menu")
22+
context.buildChildren(of: element, withTagName: "label", namespace: "menu", includeDefaultSlot: true)
2423
}
2524
}
2625
}

0 commit comments

Comments
 (0)