Skip to content

Commit a3df074

Browse files
committed
feat: 把Tabbar高度设置为NestedTabView的默认stickyHeight
1 parent e882b06 commit a3df074

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

example/src/pages/NestedTabViewExample.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const HeadTabViewExample: React.FC<TabViewExampleProps> = (props) => {
3535
<NestedTabView
3636
ref={ref}
3737
style={{ flex: 1 }}
38-
stickyHeight={55} //TabBar的高度
3938
renderHeader={() => (
4039
<View
4140
style={{

src/components/NestedTabView/NestedScene.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const NestedScene: React.FC<NestedSceneProps> = (props) => {
6969
};
7070

7171
const snap = useCallback(() => {
72+
// reanimated防抖
7273
'worklet';
7374
if (!snapEnabled) return;
7475
cancelAnimation(checkEndScroll);

src/components/NestedTabView/NestedTabView.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const NestedTabView = forwardRef<NestedTabViewRef, NestedTabViewProps>(
4343
(props, ref) => {
4444
const {
4545
renderHeader,
46-
stickyHeight = 0,
46+
stickyHeight: initialStickyHeight,
4747
children,
4848
initialIndex = 0,
4949
style,
@@ -85,6 +85,8 @@ const NestedTabView = forwardRef<NestedTabViewRef, NestedTabViewProps>(
8585
const sharedTranslate = useSharedValue(0);
8686
const [headerHeight, setHeaderHeight] = useState(0);
8787
const [tabViewHeight, setTabViewHeight] = useState(0);
88+
const [stickyHeight, setStickyHeight] = useState(0);
89+
8890
const refreshStatus = useSharedValue<RefreshStatus>(RefreshStatus.Idle);
8991
const isTouching = useSharedValue(false);
9092

@@ -192,6 +194,17 @@ const NestedTabView = forwardRef<NestedTabViewRef, NestedTabViewProps>(
192194
[tabViewHeight]
193195
);
194196

197+
const handleTabBarHeightLayout = useCallback(
198+
(e: LayoutChangeEvent) => {
199+
if (typeof initialStickyHeight === 'number') {
200+
setStickyHeight(initialStickyHeight);
201+
} else {
202+
setStickyHeight(e.nativeEvent.layout.height);
203+
}
204+
},
205+
[stickyHeight, initialStickyHeight]
206+
);
207+
195208
const childMinHeight = useMemo(() => {
196209
return headerHeight + tabViewHeight - stickyHeight;
197210
}, [headerHeight, tabViewHeight, stickyHeight]);
@@ -431,6 +444,7 @@ const NestedTabView = forwardRef<NestedTabViewRef, NestedTabViewProps>(
431444
<TabBar
432445
{...{ ...tabProps }}
433446
ref={tabRef}
447+
onLayout={handleTabBarHeightLayout}
434448
onTabPress={(index) => {
435449
pageRef.current && pageRef.current?.setPage(index);
436450
onTabPress && onTabPress(index);

src/components/TabBar/TabBar.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
3030
separatorComponent,
3131
sliderComponent,
3232
onTabPress,
33+
onLayout,
3334
activeTextColor,
3435
inactiveTextColor,
3536

@@ -70,6 +71,10 @@ const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
7071
onTabPress && onTabPress(index);
7172
};
7273

74+
const handleLayout = (e: LayoutChangeEvent) => {
75+
onLayout && onLayout(e);
76+
};
77+
7378
const onTabBarItemLayout = (index: number, layout: TabBarItemLayout) => {
7479
layouts[index] = layout;
7580
const length = layouts.filter((layout) => layout.width > 0).length;
@@ -141,7 +146,7 @@ const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
141146
});
142147

143148
return (
144-
<View style={[{ height: 55 }, style]}>
149+
<View style={[{ height: 55 }, style]} onLayout={handleLayout}>
145150
<ScrollView
146151
ref={scrollRef}
147152
horizontal

src/components/TabBar/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DimensionValue, TextStyle, ViewStyle } from 'react-native';
1+
import { DimensionValue, LayoutChangeEvent, TextStyle, ViewStyle } from 'react-native';
22
import { SharedValue } from 'react-native-reanimated';
33

44
export interface TabBarProps {
@@ -20,6 +20,7 @@ export interface TabBarProps {
2020
inactiveTextColor?: string;
2121

2222
onTabPress?: (index: number) => void;
23+
onLayout?: (e: LayoutChangeEvent) => void;
2324
}
2425

2526
export interface TabBarRef {

0 commit comments

Comments
 (0)