TabBar 标签列

import { TaTabBar, TaFixed, TaGroup } from '@/index'
import { baseList, badgeList, imageList } from './data'
import TaobaoSvg from '../../../assets/icons/taobao.svg?jsx'
import QqSvg from '../../../assets/icons/qq.svg?jsx'
import WechatSvg from '../../../assets/icons/wechat.svg?jsx'
import WeiboSvg from '../../../assets/icons/weibo.svg?jsx'

const customIconList = [
  {
    value: 'wechat',
    label: '微信',
    icon: WechatSvg
  },
  {
    value: 'qq',
    label: 'QQ',
    icon: QqSvg
  },
  {
    value: 'weibo',
    label: '微博',
    icon: WeiboSvg
  },
  {
    value: 'taobao',
    label: '淘宝',
    icon: TaobaoSvg
  }
]

export default function ExpTabBar() {
  return (
    <>
      <TaGroup title="基础用法">
        <TaTabBar options={baseList} />
      </TaGroup>
      <TaGroup title="徽标">
        <TaTabBar options={badgeList} />
      </TaGroup>
      <TaGroup title="自定义图标">
        <TaTabBar options={customIconList} />
      </TaGroup>
      <TaGroup title="自定义颜色">
        <TaTabBar
          color="#8B8DB8"
          activeColor="#ffffff"
          style={{ backgroundColor: '#6667ab' }}
          options={baseList}
        />
      </TaGroup>
      <TaGroup title="自定义图片(icon=URL)">
        <TaTabBar className="exp-tabBar-custom" options={imageList} />
      </TaGroup>
      <TaGroup title="配合 Fixed 实现置底">
        <TaFixed>
          <TaTabBar options={baseList} />
        </TaFixed>
      </TaGroup>
    </>
  )
}

Import

import { TaTabBar } from 'tantalum-ui-mobile-react'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

import type {
  TabBarOnChange,
  TabBarOptions,
  TabBarOption,
  BadgeOption,
  TabBarRef
} from 'tantalum-ui-mobile-react'

Props

属性类型默认值必填说明
valuestring | number当前激活项的 value 值
optionsTabBarOptions[]tab 数据集
colorstring自定义默认态字体和图标颜色
activeColorstring自定义激活态的字体和图标颜色

TabBarOptions

type TabBarOption = {
  label: string
  value: number | string
  icon?: IconData
  activeIcon?: IconData
  badge?: BadgeOption
  subLabel?: string
}

type TabBarOptions = (number | string | TabBarOption)[]
key类型默认值必填说明
valuestring | number唯一值
labelstring标签名
iconstring | Component设置图标,使用 Icon 组件,也支持图像 URL
activeIconstring | Component设置激活态图标,也支持图像 URL,没有设置则沿用 icon 属性
badgeBadgeOption徽标,使用 Badge 组件
const options = [
  {
    value: 1,
    label: '首页',
    icon: 'HomeOutlined',
    badge: '热'
  },
  {
    value: 2,
    label: '搜索',
    icon: 'SearchOutlined',
    badge: {
      dot: true,
      content: 1
    }
  }
]

也可以直接设置为 string[]number[],如:

const options = ['aaa', 'bbb', 'ccc']

将被转为:

const options = [
  {
    value: 'aaa',
    label: 'aaa'
  },
  {
    value: 'bbb',
    label: 'bbb'
  }
  {
    value: 'ccc',
    label: 'ccc'
  }
]

注:注意数组项要保持唯一性。

BadgeOption

type BadgeOption =
  | number
  | string
  | Partial<{
      color: string
      content: string | number
      offset: number[]
      animated: boolean
      dot: boolean
      maxCount: number
      showZero: boolean
    }>

参数主要是基于 Badge 组件的 props,如果传入是 number 或者 string 则设置直接设置 content 属性。

Events

事件描述回调函数参数TypeScript 函数
onChange点击切换 tab 时触发(value:string | number, index: number)TabBarOnChange

onChange 的回调参数

参数类型描述
valuestring | number当前激活项的 value 值
indexnumber当前激活项的索引值

Methods

interface TabBarRef {
  switchTo: (value: string | number) => void
  switchToIndex: (index: number) => void
}
方法名说明
switchTo切换到指定 value 的 Tab
switchToIndex切换到指定索引的 Tab