antd Tabs组件自定义头部

本文介绍了如何在antd的Tabs组件中使用renderTabBar方法来自定义TabBar。示例代码展示了解决方案,需要注意activeKey必须为字符串,不能使用数字。
该文章已生成可运行项目,

antd的Tabs组件,用renderTabBar自定义TabBar。

直接上代码:

import React, { Fragment, useEffect, useState, useRef } from 'react'
import styles from './style.module.scss'
import classNames from 'classnames/bind'
import { Tabs, Collapse } from 'antd';

const { TabPane } = Tabs;

const Demo = props => {
  const cx = classNames.bind(styles); //引用样式,写自己的样式即可
  const [activeKey, setActiveKey] = useState('0') //用于控制显示哪个TabPane
  const [infos, setInfos] = useState([])

  useEffect(() => {
    setInfos([
      {key: "0", title: "#201"},
      {key: "1", title: "#202"},
      {key: "2", title: "#203"},
      {key: "3", title: "#204"},
      {key: "4", title: "#205"},
      {key: "5", title: "#206"}
    ])
  },[])

  const renderTabBar = () => {
    return(
      // TabBar的样式自己写
      <div className={cx("table-tabbar-root")}>
        {infos ? infos.map((item, index) => {
          //选中tab的样式
          let className = item.key === activeKey ? "active" : ""
          return(
            <div className={cx(className)} key={item.key} onClick={() => setActiveKey(item.key)}>
              {item.title}
            </div>
          )
        })
        :null}
      </div>
    )
  }

  return (
    <div className={cx("root")}>
      <Tabs 
        activeKey={activeKey}
        tabPosition={"top"}
        animated={true}
        renderTabBar={() => renderTabBar()}
      >
        <TabPane tab="Tab 0" key="0">
          Content of Tab 0
        </TabPane>
        <TabPane tab="Tab 1" key="1">
          Content of Tab 1
        </TabPane>
        <TabPane tab="Tab 2" key="2">
          Content of Tab 2
        </TabPane>
        <TabPane tab="Tab 3" key="3">
          Content of Tab 3
        </TabPane>
        <TabPane tab="Tab 4" key="4">
          Content of Tab 4
        </TabPane>
        <TabPane tab="Tab 5" key="5">
          Content of Tab 5
        </TabPane>
      </Tabs>
    </div>
  )
}

export default Demo

注:

1. activeKey需要是字符串,尝试用数字,结果失败。

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值