import React, { useMemo, useRef, useEffect } from 'react'; import { View, ScrollView, Pressable, ViewStyle, TextStyle, Animated, useWindowDimensions, } from 'react-native'; import { withTheme } from '../theme'; import Node from '../node'; import { createStylesWithTheme } from './style'; const prefixCls = 'tabs'; export interface TabTitleProps { key?: string | number; tab?: React.ReactNode; } export interface TabsProps { activeKey?: string | number; titleWidth?: number; titleSquare?: boolean; titleFlexible?: boolean; titleCursorWidth?: number; titleCursorStyle?: ViewStyle; titleStyle?: TextStyle; titleActiveStyle?: TextStyle; tabBarStyle?: ViewStyle; hideCursor?: boolean; theme: AnnaNative.Theme; renderTab?: (item?: TabTitleProps, index?: number, isActive?: boolean) => React.ReactElement; renderTitleCursor?: () => React.ReactElement; onTabClick?: (i: any) => void; } const getTabContents = ( children: React.ReactNode, activeKey?: string | number, animated?: boolean, ): [TabTitleProps[], any[]] => { const tabContents: any[] = []; const tabs: TabTitleProps[] = []; React.Children.forEach(children, (node: any, index: number) => { const newNode = node; if (React.isValidElement(node)) { tabs.push({ key: newNode.key, tab: newNode.props.tab }) && tabContents.push( , ); } }); return [tabs, tabContents]; }; const getTabIndex = (tabs: TabTitleProps[], activeKey?: string | number) => { let tIndex = 0; tabs.forEach((i: any, index: number) => { if (i.key === activeKey) { tIndex = index; } }); return tIndex; }; const Tabs: React.FC = props => { const { activeKey, titleFlexible, titleCursorWidth, titleCursorStyle, titleStyle, titleActiveStyle, tabBarStyle, hideCursor, renderTab, renderTitleCursor, onTabClick, theme, children, } = props; const styles = createStylesWithTheme(theme); const { colors } = theme; const { primary } = colors; const windowWidth = useWindowDimensions().width; const activeX = useRef(new Animated.Value(0)).current; const activeWidth = useRef(new Animated.Value(0)).current; const tabTitleLayouts = useRef([]).current; const header = useRef(null); const headerLayout = useRef(null); const [tabs, tabContents] = useMemo(() => getTabContents(children, activeKey), [children]); const selected = useMemo(() => getTabIndex(tabs, activeKey), [activeKey, tabs]); useEffect(() => { emitTabTitleActiveAnimated(); }, [selected]); const emitTabTitleActiveAnimated = () => { const x = (tabTitleLayouts?.[selected]?.x || 0) + 16; const width = (tabTitleLayouts?.[selected]?.width || 0) - 32 > 0 ? (tabTitleLayouts?.[selected]?.width || 0) - 32 : 1; Animated.parallel([ Animated.timing(activeX, { toValue: x + width / 2 - 1 / 2, // 这里的 1 是 'tabs-plain-active' 的宽度,这里RN的处理方式是一直以一个节点最初的宽度来作为标准,不管这个节点通过scaleX放大或缩小其宽度。 duration: 300, useNativeDriver: true, }), Animated.timing(activeWidth, { toValue: width, duration: 300, useNativeDriver: true, }), ]).start(); }; const handleTabClick = (_e: any, item: any, index: number) => { onTabClick?.(item); const currentTabTitle = tabTitleLayouts?.[index]; const currentTabTitleCenterPoint = currentTabTitle?.x + currentTabTitle?.width / 2; const harfWindowWidth = windowWidth / 2; header.current.scrollTo({ x: currentTabTitleCenterPoint - harfWindowWidth }); }; const handleTabTitleLayout = (e: any, index: number) => { tabTitleLayouts[index] = e.nativeEvent.layout; if (index === selected) { emitTabTitleActiveAnimated(); } }; const handleHeaderLayout = (e: any) => { headerLayout.current = e.nativeEvent.layout; }; const renderTabs = () => { return ( { header.current = ref; }} style={[styles[`${prefixCls}-plain`], tabBarStyle]} contentContainerStyle={[titleFlexible ? { flex: 1 } : null]} // 注意,当 titleFlexible 为 true 时这里一定要设置 flex: 1 使 ScrollView 的 content 撑满 horizontal={true} showsHorizontalScrollIndicator={false} onLayout={handleHeaderLayout} > {tabs.map((item: TabTitleProps, index) => ( handleTabClick(e, item, index)} onLayout={e => handleTabTitleLayout(e, index)} > {renderTab ? ( renderTab(item, index, selected === index) ) : ( {item.tab} )} ))} {!hideCursor ? ( renderTitleCursor ? ( renderTitleCursor() ) : ( ) ) : null} ); }; return ( {renderTabs()} {tabContents} ); }; export interface TabContentProps { key?: string | number; tab?: React.ReactNode; active?: boolean; style?: ViewStyle; animated?: boolean; theme: AnnaNative.Theme; } const TabContent: React.FC = props => { const { active, style, children, theme } = props; const styles = createStylesWithTheme(theme); if (!active) { return ( {children} ); } return ( {children} ); }; const TabsWithTheme = withTheme(Tabs); TabsWithTheme.TabContent = withTheme(TabContent); export default TabsWithTheme;