Skip to content

Commit 067d365

Browse files
committed
fix(Tab): 增加点击动画过度效果
1 parent 4019f54 commit 067d365

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

packages/core/src/Tabs/TabsItem.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useCallback, useMemo } from 'react';
2-
import { View, StyleSheet, Text, TouchableOpacity } from 'react-native';
1+
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
2+
import { View, StyleSheet, Text, TouchableOpacity, Animated } from 'react-native';
33

44
import Icon, { IconsName } from '../Icon';
55

@@ -42,6 +42,24 @@ export interface TabsItemProps {
4242

4343
function TabsItem(props: TabsItemProps) {
4444
const { activeColor, icon, index, value, onChange, defaultColor } = props;
45+
const opacity = useRef(new Animated.Value(0)).current;
46+
47+
useEffect(() => {
48+
if (value === index) {
49+
Animated.timing(opacity, {
50+
toValue: 1,
51+
duration: 500,
52+
useNativeDriver: true,
53+
}).start();
54+
} else {
55+
Animated.timing(opacity, {
56+
toValue: 0,
57+
duration: 500,
58+
useNativeDriver: true,
59+
}).start();
60+
}
61+
}, [value]);
62+
4563
const style = useCallback(() => {
4664
const { style = {} } = props;
4765
const titleBoxStyle = {
@@ -92,7 +110,15 @@ function TabsItem(props: TabsItemProps) {
92110
if (value === index) {
93111
return (
94112
<View style={styles.bottomView}>
95-
<View style={[styles.bottom, { ...style().borderColor }]} />
113+
<Animated.View
114+
style={[
115+
styles.bottom,
116+
{
117+
...style().borderColor,
118+
opacity,
119+
},
120+
]}
121+
/>
96122
</View>
97123
);
98124
}
@@ -102,10 +128,10 @@ function TabsItem(props: TabsItemProps) {
102128
return (
103129
<View style={styles.TabsItemContainer}>
104130
<TouchableOpacity onPress={() => (index === 0 || index) && onChange?.(index)}>
105-
{IconDom}
106-
<View style={[styles.titleBox, { ...style().titleBoxStyle }]}>
131+
<Animated.View style={[styles.titleBox, { ...style().titleBoxStyle }]}>
132+
{IconDom}
107133
<Text style={[styles.title, { ...style().titleStyle }]}>{props.title}</Text>
108-
</View>
134+
</Animated.View>
109135
{BorderDom}
110136
</TouchableOpacity>
111137
</View>

0 commit comments

Comments
 (0)