|
@@ -1,79 +1,39 @@
|
|
|
-import React, { useState, useEffect } from 'react';
|
|
|
+import React, { useState } from 'react';
|
|
|
import Taro from '@tarojs/taro';
|
|
|
import { View, Text } from '@tarojs/components';
|
|
|
-import {
|
|
|
- FaHome,
|
|
|
- FaDoorOpen,
|
|
|
- FaHistory,
|
|
|
- FaUser,
|
|
|
- FaHouseUser,
|
|
|
- FaDoorClosed,
|
|
|
- FaCalendarAlt,
|
|
|
- FaUserCircle
|
|
|
-} from 'react-icons/fa';
|
|
|
+import Icon from '@components/base/Icon'
|
|
|
+import { ICONS } from '@constants/icons';
|
|
|
import './index.scss';
|
|
|
|
|
|
-interface TabItem {
|
|
|
- pagePath: string;
|
|
|
- text: string;
|
|
|
- icon: React.ReactNode;
|
|
|
- selectedIcon: React.ReactNode;
|
|
|
-}
|
|
|
+const tabs = [
|
|
|
+ { pagePath: '/pages/game-plaza/index', text: '游戏', icon: ICONS.PLAZA },
|
|
|
+ { pagePath: '/pages/game-room/index', text: '房间', icon: ICONS.ROOM },
|
|
|
+ { pagePath: '/pages/game-history/index', text: '历史', icon: ICONS.HISTORY },
|
|
|
+ { pagePath: '/pages/profile/index', text: '我的', icon: ICONS.PROFILE }
|
|
|
+];
|
|
|
|
|
|
const CustomTabBar: React.FC = () => {
|
|
|
- const [currentPath, setCurrentPath] = useState<string>('');
|
|
|
- const [tabs] = useState<TabItem[]>([
|
|
|
- {
|
|
|
- pagePath: '/pages/index/index',
|
|
|
- text: '游戏广场',
|
|
|
- icon: <FaHome size={20} color="#999" />,
|
|
|
- selectedIcon: <FaHouseUser size={20} color="#4F7FFE" />
|
|
|
- },
|
|
|
- {
|
|
|
- pagePath: '/pages/room/join/index',
|
|
|
- text: '我的房间',
|
|
|
- icon: <FaDoorOpen size={20} color="#999" />,
|
|
|
- selectedIcon: <FaDoorClosed size={20} color="#4F7FFE" />
|
|
|
- },
|
|
|
- {
|
|
|
- pagePath: '/pages/game-history/index',
|
|
|
- text: '游戏历史',
|
|
|
- icon: <FaHistory size={20} color="#999" />,
|
|
|
- selectedIcon: <FaCalendarAlt size={20} color="#4F7FFE" />
|
|
|
- },
|
|
|
- {
|
|
|
- pagePath: '/pages/profile/index',
|
|
|
- text: '个人中心',
|
|
|
- icon: <FaUser size={20} color="#999" />,
|
|
|
- selectedIcon: <FaUserCircle size={20} color="#4F7FFE" />
|
|
|
- }
|
|
|
- ]);
|
|
|
+ const [current, setCurrent] = useState(0);
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- const page = Taro.getCurrentInstance().router;
|
|
|
- if (page) {
|
|
|
- const path = `/${page.path}`;
|
|
|
- setCurrentPath(path);
|
|
|
- }
|
|
|
- }, []);
|
|
|
-
|
|
|
- const handleTabClick = (pagePath: string) => {
|
|
|
- Taro.switchTab({ url: pagePath });
|
|
|
- setCurrentPath(pagePath);
|
|
|
+ const handleClick = (index: number, url: string) => {
|
|
|
+ setCurrent(index);
|
|
|
+ Taro.switchTab({ url });
|
|
|
};
|
|
|
|
|
|
return (
|
|
|
<View className='custom-tab-bar'>
|
|
|
- {tabs.map(tab => (
|
|
|
+ {tabs.map((tab, index) => (
|
|
|
<View
|
|
|
- key={tab.pagePath}
|
|
|
- className={`custom-tab-bar__item ${currentPath === tab.pagePath ? 'custom-tab-bar__item--active' : ''}`}
|
|
|
- onClick={() => handleTabClick(tab.pagePath)}
|
|
|
+ key={index}
|
|
|
+ className={`tab-item ${current === index ? 'active' : ''}`}
|
|
|
+ onClick={() => handleClick(index, tab.pagePath)}
|
|
|
>
|
|
|
- <View className='custom-tab-bar__icon'>
|
|
|
- {currentPath === tab.pagePath ? tab.selectedIcon : tab.icon}
|
|
|
- </View>
|
|
|
- <Text className='custom-tab-bar__text'>{tab.text}</Text>
|
|
|
+ <Icon
|
|
|
+ type={tab.icon}
|
|
|
+ size={24}
|
|
|
+ color={current === index ? '#1296db' : '#8a8a8a'}
|
|
|
+ />
|
|
|
+ <Text className='tab-text'>{tab.text}</Text>
|
|
|
</View>
|
|
|
))}
|
|
|
</View>
|