Skip to content

Commit 4e6764d

Browse files
committed
feat: create LegacyTabView implementation
1 parent b92c42b commit 4e6764d

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import SwiftUI
2+
3+
struct LegacyTabView: View {
4+
@ObservedObject var props: TabViewProps
5+
6+
#if os(macOS)
7+
var tabBar: NSTabView?
8+
#else
9+
var tabBar: UITabBar?
10+
#endif
11+
12+
var onLayout: (_ size: CGSize) -> Void
13+
var onSelect: (_ key: String) -> Void
14+
var updateTabBarAppearance: (_ props: TabViewProps, _ tabBar: UITabBar?) -> Void
15+
16+
@ViewBuilder
17+
var body: some View {
18+
TabView(selection: $props.selectedPage) {
19+
ForEach(props.children.indices, id: \.self) { index in
20+
renderTabItem(at: index)
21+
}
22+
.measureView { size in
23+
onLayout(size)
24+
}
25+
}
26+
.hideTabBar(props.tabBarHidden)
27+
}
28+
29+
@ViewBuilder
30+
private func renderTabItem(at index: Int) -> some View {
31+
if let tabData = props.items[safe: index] {
32+
let isFocused = props.selectedPage == tabData.key
33+
34+
if !tabData.hidden || isFocused {
35+
let icon = props.icons[index]
36+
let child = props.children[safe: index] ?? PlatformView()
37+
38+
RepresentableView(view: child)
39+
.ignoresSafeArea(.container, edges: .all)
40+
.tabItem {
41+
TabItem(
42+
title: tabData.title,
43+
icon: icon,
44+
sfSymbol: tabData.sfSymbol,
45+
labeled: props.labeled
46+
)
47+
.accessibilityIdentifier(tabData.testID ?? "")
48+
.tag(tabData.key)
49+
.tabBadge(tabData.badge)
50+
.onAppear {
51+
#if !os(macOS)
52+
updateTabBarAppearance(props, tabBar)
53+
#endif
54+
#if os(iOS)
55+
if index >= 4, !isFocused {
56+
onSelect(tabData.key)
57+
}
58+
#endif
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)