tab-bar-layout.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import React, { ReactNode } from 'react'
  2. import { View } from '@tarojs/components'
  3. import { TabBar, TabBarItem } from '@/components/ui/tab-bar'
  4. import { useRouter } from '@tarojs/taro'
  5. export interface TabBarLayoutProps {
  6. children: ReactNode
  7. activeKey: string
  8. }
  9. const tabBarItems: TabBarItem[] = [
  10. {
  11. key: 'home',
  12. title: '首页',
  13. icon: '🏠',
  14. selectedIcon: '🏠',
  15. },
  16. {
  17. key: 'explore',
  18. title: '发现',
  19. icon: '🔍',
  20. selectedIcon: '🔍',
  21. },
  22. {
  23. key: 'profile',
  24. title: '我的',
  25. icon: '👤',
  26. selectedIcon: '👤',
  27. },
  28. ]
  29. export const TabBarLayout: React.FC<TabBarLayoutProps> = ({ children, activeKey }) => {
  30. const handleTabChange = (key: string) => {
  31. // 使用 Taro 的导航 API 进行页面跳转
  32. import('@tarojs/taro').then(({ default: Taro }) => {
  33. switch (key) {
  34. case 'home':
  35. Taro.switchTab({ url: '/pages/index/index' })
  36. break
  37. case 'explore':
  38. Taro.switchTab({ url: '/pages/explore/index' })
  39. break
  40. case 'profile':
  41. Taro.switchTab({ url: '/pages/profile/index' })
  42. break
  43. default:
  44. break
  45. }
  46. })
  47. }
  48. return (
  49. <View className="min-h-screen bg-gray-50">
  50. <View className="flex-1">
  51. {children}
  52. </View>
  53. <TabBar
  54. items={tabBarItems}
  55. activeKey={activeKey}
  56. onChange={handleTabChange}
  57. fixed={true}
  58. safeArea={true}
  59. />
  60. </View>
  61. )
  62. }
  63. export default TabBarLayout