Skip to content

Commit 2a15cde

Browse files
committed
refactor: use different TabView depending on os version
1 parent 5de6709 commit 2a15cde

File tree

1 file changed

+16
-49
lines changed

1 file changed

+16
-49
lines changed

packages/react-native-bottom-tabs/ios/TabViewImpl.swift

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@ struct TabViewImpl: View {
1313
#else
1414
@Weak var tabBar: UITabBar?
1515
#endif
16-
16+
17+
@ViewBuilder
18+
var tabContent: some View {
19+
if #available(iOS 18, macOS 15, visionOS 2, tvOS 18, *) {
20+
NewTabView(
21+
props: props, tabBar: tabBar, onLayout: onLayout, onSelect: onSelect,
22+
updateTabBarAppearance: updateTabBarAppearance)
23+
} else {
24+
LegacyTabView(
25+
props: props, tabBar: tabBar, onLayout: onLayout, onSelect: onSelect,
26+
updateTabBarAppearance: updateTabBarAppearance)
27+
}
28+
}
29+
1730
var onSelect: (_ key: String) -> Void
1831
var onLongPress: (_ key: String) -> Void
1932
var onLayout: (_ size: CGSize) -> Void
2033
var onTabBarMeasured: (_ height: Int) -> Void
21-
34+
2235
var body: some View {
23-
TabView(selection: $props.selectedPage) {
24-
ForEach(props.children.indices, id: \.self) { index in
25-
renderTabItem(at: index)
26-
}
27-
.measureView { size in
28-
onLayout(size)
29-
}
30-
}
36+
tabContent
3137
#if !os(tvOS) && !os(macOS) && !os(visionOS)
3238
.onTabItemEvent { index, isLongPress in
3339
let item = props.filteredItems[safe: index]
@@ -74,45 +80,6 @@ struct TabViewImpl: View {
7480
}
7581
}
7682

77-
@ViewBuilder
78-
private func renderTabItem(at index: Int) -> some View {
79-
let tabData = props.items[safe: index]
80-
let isHidden = tabData?.hidden ?? false
81-
let isFocused = props.selectedPage == tabData?.key
82-
83-
if !isHidden || isFocused {
84-
let child = props.children[safe: index] ?? PlatformView()
85-
let icon = props.icons[index]
86-
87-
RepresentableView(view: child)
88-
.ignoresSafeArea(.container, edges: .all)
89-
.tabItem {
90-
TabItem(
91-
title: tabData?.title,
92-
icon: icon,
93-
sfSymbol: tabData?.sfSymbol,
94-
labeled: props.labeled
95-
)
96-
.accessibilityIdentifier(tabData?.testID ?? "")
97-
}
98-
.tag(tabData?.key)
99-
.tabBadge(tabData?.badge)
100-
.hideTabBar(props.tabBarHidden)
101-
.onAppear {
102-
#if !os(macOS)
103-
updateTabBarAppearance(props: props, tabBar: tabBar)
104-
#endif
105-
106-
#if os(iOS)
107-
guard index >= 4,
108-
let key = tabData?.key,
109-
props.selectedPage != key else { return }
110-
onSelect(key)
111-
#endif
112-
}
113-
}
114-
}
115-
11683
func emitHapticFeedback(longPress: Bool = false) {
11784
#if os(iOS)
11885
if !props.hapticFeedbackEnabled {

0 commit comments

Comments
 (0)