Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitBhalla committed Jul 5, 2023
1 parent 642b692 commit 1a3c441
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 51 deletions.
17 changes: 8 additions & 9 deletions example/src/views/tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { Tabs, useTabsInternal } from '@rneui/base/dist/Tab/Tab';
import { Tabs, TabsRef } from '@rneui/base/dist/Tab/Tab';
import { Button, Tab as TabBar, TabView, Text } from '@rneui/themed';
import React from 'react';
import React, { useRef } from 'react';
import { ScrollView } from 'react-native';
import { Header } from '../components/header';

const SubComponent = () => {
const { changeIndex } = useTabsInternal();
return <Button onPress={() => changeIndex(2)}>Jump to Tab 3</Button>;
};

export default () => {
const tabRef = useRef<TabsRef>();

return (
<>
<Header title="Tab" />
<Tabs onChange={console.log} value={1}>
<Tabs ref={tabRef}>
<TabBar
indicatorStyle={{
backgroundColor: 'white',
Expand Down Expand Up @@ -59,7 +56,9 @@ export default () => {
<TabView onSwipeStart={console.log}>
<TabView.Item style={{ backgroundColor: 'red', width: '100%' }}>
<ScrollView>
<SubComponent />
<Button onPress={() => tabRef.current.changeIndex(2)}>
Jump to Tab 3
</Button>
<Text h1>{Math.random()}</Text>
<Text h1>Recent 0</Text>
<Text h1>Recent 0</Text>
Expand Down
109 changes: 67 additions & 42 deletions packages/base/src/Tab/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { forwardRef, useImperativeHandle } from 'react';
import {
Animated,
Easing,
Expand Down Expand Up @@ -85,45 +85,64 @@ const TabContext = React.createContext(
}
);

export const Tabs = ({
children,
animationType = 'spring',
animationConfig = {},
}) => {
const translateX = React.useRef(new Animated.Value(0));
const currentIndex = React.useRef(0);
const onIndexChangeRef = React.useRef((value: number) => value);

const animate = React.useCallback(
(toValue: number, onDone) => {
currentIndex.current = toValue;
onIndexChangeRef.current?.(toValue);
Animated[animationType](translateX.current, {
toValue,
useNativeDriver: true,
easing: Easing.inOut(Easing.ease),
duration: 150,
...animationConfig,
}).start();
onDone?.(toValue);
},
[animationConfig, animationType]
);

return (
<TabContext.Provider
value={{
changeIndex: animate,
__translateX: translateX,
__currentIndex: currentIndex,
__onIndexChangeRef: onIndexChangeRef,
}}
>
{children}
</TabContext.Provider>
);
export type TabsRef = {
changeIndex: (toValue: number) => void;
};

interface TabsProps {
children: React.ReactNode;
animationType?: 'timing' | 'spring';
animationConfig?: Partial<
Animated.TimingAnimationConfig | Animated.SpringAnimationConfig
>;
}

export const Tabs = forwardRef(
(
{ children, animationType = 'spring', animationConfig = {} }: TabsProps,
ref
) => {
const translateX = React.useRef(new Animated.Value(0));
const currentIndex = React.useRef(0);
const onIndexChangeRef = React.useRef((value: number) => value);

useImperativeHandle(ref, () => ({
changeIndex: (toValue: number) => {
animate(toValue);
},
}));

const animate = React.useCallback(
(toValue: number, onDone = () => {}) => {
currentIndex.current = toValue;
onIndexChangeRef.current?.(toValue);
Animated[animationType](translateX.current, {
toValue,

Check failure on line 120 in packages/base/src/Tab/Tab.tsx

View workflow job for this annotation

GitHub Actions / check_types

Type 'number | Value | ValueXY | { x: number; y: number; } | AnimatedInterpolation | { x: number; y: number; }' is not assignable to type '(number | Value | ValueXY | { x: number; y: number; } | AnimatedInterpolation) & (number | Value | ValueXY | { x: number; y: number; })'.
useNativeDriver: true,
easing: Easing.inOut(Easing.ease),
duration: 150,
...animationConfig,
}).start();
onDone?.(toValue);
},
[animationConfig, animationType]
);

return (
<TabContext.Provider
value={{
changeIndex: animate,
__translateX: translateX,
__currentIndex: currentIndex,
__onIndexChangeRef: onIndexChangeRef,

Check failure on line 137 in packages/base/src/Tab/Tab.tsx

View workflow job for this annotation

GitHub Actions / check_types

Type '{ changeIndex: (toValue: number, onDone?: any) => void; __translateX: React.MutableRefObject<Animated.Value>; __currentIndex: React.MutableRefObject<number>; __onIndexChangeRef: React.MutableRefObject<...>; }' is not assignable to type '{ __translateX: MutableRefObject<Value>; __currentIndex: MutableRefObject<number>; changeIndex: (toValue: number) => void; }'.
}}
>
{children}
</TabContext.Provider>
);
}
);

export const useTabsInternal = () => React.useContext(TabContext);

export const TabBase: RneFunctionComponent<TabProps> = ({
Expand Down Expand Up @@ -159,7 +178,10 @@ export const TabBase: RneFunctionComponent<TabProps> = ({

React.useEffect(() => {
if (__onIndexChangeRef) {
__onIndexChangeRef.current = scrollHandler;
__onIndexChangeRef.current = (value) => {

Check warning on line 181 in packages/base/src/Tab/Tab.tsx

View workflow job for this annotation

GitHub Actions / check_lint

'value' is already declared in the upper scope on line 151 column 3
scrollHandler(value);
setActiveIndex(value);
};
}
}, [__onIndexChangeRef, scrollHandler]);

Check failure on line 186 in packages/base/src/Tab/Tab.tsx

View workflow job for this annotation

GitHub Actions / check_types

Block-scoped variable 'scrollHandler' used before its declaration.

Expand Down Expand Up @@ -259,6 +281,8 @@ export const TabBase: RneFunctionComponent<TabProps> = ({
});
};

const [activeIndex, setActiveIndex] = React.useState(currentIndex.current);

return (
<View
{...rest}
Expand Down Expand Up @@ -299,10 +323,11 @@ export const TabBase: RneFunctionComponent<TabProps> = ({
>
{React.cloneElement(child as React.ReactElement<TabItemProps>, {
onPress: () => {
animate(index);
onChange(index);
animate(index, setActiveIndex);

Check failure on line 326 in packages/base/src/Tab/Tab.tsx

View workflow job for this annotation

GitHub Actions / check_types

Expected 1 arguments, but got 2.

onChange?.(index);
},
active: index === currentIndex.current,
active: index === activeIndex,
variant,
_parentProps: {
dense,
Expand Down

0 comments on commit 1a3c441

Please sign in to comment.