Skip to content

Commit

Permalink
feat: use ref control TabBar
Browse files Browse the repository at this point in the history
  • Loading branch information
mahaaoo committed Sep 21, 2024
1 parent a70b743 commit 073dfc8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
20 changes: 5 additions & 15 deletions src/components/TabBar/TabBar.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import React, {
forwardRef,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import React, { forwardRef, useImperativeHandle, useMemo, useRef } from 'react';
import { View, ScrollView, LayoutChangeEvent, StyleSheet } from 'react-native';
import TabBarItem from './TabBarItem';
import Animated, {
runOnUI,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import TabBarSlider from './TabBarSlider';
import Separator from './Separator';
import { TabBarProps, TabBarItemLayout } from './type';
import { TabBarProps, TabBarItemLayout, TabBarRef } from './type';
import { useVerifyProps } from './hook';

interface TabBarRef {
setTab: (index: number) => void;
}

const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
const {
tabs,
Expand All @@ -44,6 +32,7 @@ const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
contentSize,
} = useVerifyProps(props);

const currentIndex = useSharedValue(initialTab);
const sliderOffset = useSharedValue(0);
const scrollSize = useRef(0);
const scrollRef = useRef<ScrollView>(null);
Expand All @@ -54,12 +43,13 @@ const TabBar = forwardRef<TabBarRef, TabBarProps>((props, ref) => {
ref,
() => ({
setTab: handleOnPress,
getCurrent: () => currentIndex.value,
}),
[]
);

const handleOnPress = (index: number) => {
// setCurrentIndex(index);
currentIndex.value = index;
calculateSliderOffset(index);
onPress && onPress(index);
};
Expand Down
5 changes: 5 additions & 0 deletions src/components/TabBar/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface TabBarProps {
onPress?: (index: number) => void;
}

export interface TabBarRef {
setTab: (index: number) => void;
getCurrent: () => number;
}

export interface TabBarItemProps {
index: number;
title: string;
Expand Down
1 change: 0 additions & 1 deletion src/components/TabView/TabView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const TabView: React.FC<TabViewProps> = (props) => {
// currentIndex.value = translate;
// const page = pageRef.current && pageRef.current?.getCurrentPage();
// console.log('translate', page);

}}
scrollEnabled={true}
bounces={true}
Expand Down

0 comments on commit 073dfc8

Please sign in to comment.