标签页
选项卡切换组件。
何时使用
提供平级的区域将大块内容进行收纳和展现,保持界面整洁。
Ant Design 依次提供了三级选项卡,分别用于不同的场景。
- 卡片式的页签,提供可关闭的样式,常用于容器顶部。
- 既可用于容器顶部,也可用于容器内部,是最通用的 Tabs。
- Radio.Button 可作为更次级的页签来使用。
代码演示
-
基本 ·
basic -
禁用 ·
disabled -
居中 ·
centered -
图标 ·
icon -
指示条 ·
custom-indicator -
滑动 ·
slide -
附加内容 ·
extra -
大小 ·
size -
位置 ·
placement -
自定义折叠菜单搜索 ·
popupRender-Search -
卡片式页签 ·
card -
新增和关闭页签 ·
editable-card -
自定义新增页签触发器 ·
custom-add-trigger -
自定义页签头 ·
custom-tab-bar -
可拖拽标签 ·
custom-tab-bar-node -
自定义语义结构的样式和类 ·
style-class
API
通用属性参考:通用属性
Tabs
| 参数 | 说明 | 类型 | 默认值 | 版本 | 全局配置 |
|---|---|---|---|---|---|
| activeKey | 当前激活 tab 面板的 key | string | - | × | |
| addIcon | 自定义添加按钮,设置 type="editable-card" 时有效 | ReactNode | <PlusOutlined /> | 4.4.0 | 5.14.0 |
| animated | 是否使用动画切换 Tabs | boolean| { inkBar: boolean, tabPane: boolean } | { inkBar: true, tabPane: false } | × | |
| centered | 标签居中展示 | boolean | false | 4.4.0 | × |
| classNames | 用于自定义组件内部各语义化结构的 class,支持对象或函数 | Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string> | - | 6.0.0 | |
| defaultActiveKey | 初始化选中面板的 key,如果没有设置 activeKey | string | 第一个面板的 key | × | |
| hideAdd | 是否隐藏加号图标,在 type="editable-card" 时有效 | boolean | false | × | |
| indicator | 自定义指示条的长度和对齐方式 | { size?: number | (origin: number) => number; align: start | center | end; } | - | 5.13.0 | 5.13.0 |
| items | 配置选项卡内容 | TabItemType | [] | 4.23.0 | × |
| more | 自定义折叠菜单属性 | MoreProps | { icon: <EllipsisOutlined /> , trigger: 'hover' } | more.icon: 5.17.0 | |
| removeIcon | 自定义删除按钮,设置 type="editable-card" 时有效 | ReactNode | <CloseOutlined /> | 5.15.0 | 5.15.0 |
更多菜单的 className, 请使用 classNames.popup 替换 | string | - | 4.21.0 | × | |
| renderTabBar | 替换 TabBar,用于二次封装标签头 | (props: DefaultTabBarProps, DefaultTabBar: React.ComponentClass) => React.ReactElement | - | × | |
| size | 大小,提供 large medium 和 small 三种大小 | large | medium | small | medium | × | |
| styles | 用于自定义组件内部各语义化结构的行内 style,支持对象或函数 | Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties> | - | 6.0.0 | |
| tabBarExtraContent | tab bar 上额外的元素 | ReactNode | {left?: ReactNode, right?: ReactNode} | - | object: 4.6.0 | × |
| tabBarGutter | tabs 之间的间隙 | number | - | × | |
| tabBarStyle | tab bar 的样式对象 | CSSProperties | - | × | |
| tabPlacement | 页签位置,可选值有 top end bottom start | top | end | bottom | start | top | × | |
页签位置,可选值有 top right bottom left,请使用 tabPlacement 替换 | top | right | bottom | left | top | × | ||
被隐藏时是否销毁 DOM 结构,使用 destroyOnHidden 代替 | boolean | false | × | ||
| destroyOnHidden | 被隐藏时是否销毁 DOM 结构 | boolean | false | 5.25.0 | × |
| type | 页签的基本样式,可选 line、card editable-card 类型 | line | card | editable-card | line | × | |
| onChange | 切换面板的回调 | (activeKey: string) => void | - | × | |
| onEdit | 新增和删除页签的回调,在 type="editable-card" 时有效 | (action === 'add' ? event : targetKey, action) => void | - | × | |
| onTabClick | tab 被点击的回调 | (key: string, event: MouseEvent) => void | - | × | |
| onTabScroll | tab 滚动时触发 | ({ direction: left | right | top | bottom }) => void | - | 4.3.0 | × |
更多属性查看 @rc-component/tabs
TabItemType
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| closeIcon | 自定义关闭图标,在 type="editable-card" 时有效。5.7.0:设置为 null 或 false 时隐藏关闭按钮 | ReactNode | - | |
被隐藏时是否销毁 DOM 结构,使用 destroyOnHidden 代替 | boolean | false | 5.11.0 | |
| destroyOnHidden | 被隐藏时是否销毁 DOM 结构 | boolean | false | 5.25.0 |
| disabled | 禁用某一项 | boolean | false | |
| forceRender | 被隐藏时是否渲染 DOM 结构 | boolean | false | |
| key | 对应 activeKey | string | - | |
| label | 选项卡头部文字元素 | ReactNode | - | |
| icon | 选项卡头部图标元素 | ReactNode | - | 5.12.0 |
| children | 选项卡内容元素 | ReactNode | - | |
| closable | 是否显示选项卡的关闭按钮,在 type="editable-card" 时有效 | boolean | true |
MoreProps
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
| icon | 自定义折叠图标 | ReactNode | - | |
| popupRender | 自定义折叠菜单渲染 | (menu: ReactElement, info: { restTabs: Tab[], onClose: () => void }) => ReactElement | - | 6.6.0 |
| DropdownProps | 支持 Dropdown 的其他属性 |
Semantic DOM
| 语义节点 | 原生说明 | 版本 |
|---|---|---|
root | 根元素,包含标签页容器的基础样式、布局和方向控制 | — |
header | 头部元素,包含标签页头部导航的布局、背景、边框等样式 | — |
item | Item 元素,包含相对定位、内边距、颜色、文本省略、圆角、过渡动画等标签项的样式和交互效果 | — |
remove | 删除按钮元素,包含可编辑标签页关闭按钮的尺寸、颜色、悬浮态和交互反馈等样式 | 6.4.0 |
indicator | 指示器元素,包含指示条的颜色、位置、尺寸、过渡动画等活跃状态指示样式 | — |
body | 内容区域元素,包含标签页面板容器的布局、动画和尺寸控制 | — |
content | 内容元素,包含单个标签页面板的布局、内边距等内容展示样式 | — |
popup.root | 弹出菜单元素,包含下拉菜单的绝对定位、层级、显示控制、最大高度、滚动等样式 | — |
语义说明来源: vendor/components/tabs/demo/_semantic.tsx
主题变量(Design Token)
类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。
| 令牌 | 类型 | 原生说明 |
|---|---|---|
zIndexPopup | number | 下拉菜单 z-index |
cardBg | string | 卡片标签页背景色 |
cardHeight | number | 卡片标签页高度 |
cardHeightSM | number | 小尺寸卡片标签页高度 |
cardHeightLG | number | 大尺寸卡片标签页高度 |
cardPadding | string | 卡片标签页内间距 |
cardPaddingSM | string | 小号卡片标签页内间距 |
cardPaddingLG | string | 大号卡片标签页内间距 |
titleFontSize | number | 标签页标题文本大小 |
titleFontSizeLG | number | 大号标签页标题文本大小 |
titleFontSizeSM | number | 小号标签页标题文本大小 |
inkBarColor | string | 指示条颜色 |
horizontalMargin | string | 横向标签页外间距 |
horizontalItemGutter | number | 横向标签页标签间距 |
horizontalItemMargin | string | 横向标签页标签外间距 |
horizontalItemMarginRTL | string | 横向标签页标签外间距(RTL) |
horizontalItemPadding | string | 横向标签页标签内间距 |
horizontalItemPaddingLG | string | 大号横向标签页标签内间距 |
horizontalItemPaddingSM | string | 小号横向标签页标签内间距 |
verticalItemPadding | string | 纵向标签页标签内间距 |
verticalItemMargin | string | 纵向标签页标签外间距 |
itemColor | string | 标签文本颜色 |
itemActiveColor | string | 标签激活态文本颜色 |
itemHoverColor | string | 标签悬浮态文本颜色 |
itemSelectedColor | string | 标签选中态文本颜色 |
cardGutter | number | 卡片标签间距 |
声明来源: vendor/components/tabs/style/index.ts
import { unit } from '@ant-design/cssinjs';
import type { CSSObject } from '@ant-design/cssinjs';
import { genFocusOutline, genFocusStyle, resetComponent, textEllipsis } from '../../style';
import { slideDownIn, slideDownOut, slideUpIn, slideUpOut } from '../../style/motion';
import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';
import { genStyleHooks, mergeToken } from '../../theme/internal';
import genMotionStyle from './motion';
export interface ComponentToken {
/**
* @desc 下拉菜单 z-index
* @descEN z-index of dropdown menu
*/
zIndexPopup: number;
/**
* @desc 卡片标签页背景色
* @descEN Background color of card tab
*/
cardBg: string;
/**
* @desc 卡片标签页高度
* @descEN Height of card tab
*/
cardHeight: number;
/**
* @desc 小尺寸卡片标签页高度
* @descEN Height of small card tab
*/
cardHeightSM: number;
/**
* @desc 大尺寸卡片标签页高度
* @descEN Height of large card tab
*/
cardHeightLG: number;
/**
* @desc 卡片标签页内间距
* @descEN Padding of card tab
*/
cardPadding: string;
/**
* @desc 小号卡片标签页内间距
* @descEN Padding of small card tab
*/
cardPaddingSM: string;
/**
* @desc 大号卡片标签页内间距
* @descEN Padding of large card tab
*/
cardPaddingLG: string;
/**
* @desc 标签页标题文本大小
* @descEN Font size of title
*/
titleFontSize: number;
/**
* @desc 大号标签页标题文本大小
* @descEN Font size of large title
*/
titleFontSizeLG: number;
/**
* @desc 小号标签页标题文本大小
* @descEN Font size of small title
*/
titleFontSizeSM: number;
/**
* @desc 指示条颜色
* @descEN Color of indicator
*/
inkBarColor: string;
/**
* @desc 横向标签页外间距
* @descEN Horizontal margin of horizontal tab
*/
horizontalMargin: string;
/**
* @desc 横向标签页标签间距
* @descEN Horizontal gutter of horizontal tab
*/
horizontalItemGutter: number;
/**
* @desc 横向标签页标签外间距
* @descEN Horizontal margin of horizontal tab item
*/
horizontalItemMargin: string;
/**
* @desc 横向标签页标签外间距(RTL)
* @descEN Horizontal margin of horizontal tab item (RTL)
*/
horizontalItemMarginRTL: string;
/**
* @desc 横向标签页标签内间距
* @descEN Horizontal padding of horizontal tab item
*/
horizontalItemPadding: string;
/**
* @desc 大号横向标签页标签内间距
* @descEN Horizontal padding of large horizontal tab item
*/
horizontalItemPaddingLG: string;
/**
* @desc 小号横向标签页标签内间距
* @descEN Horizontal padding of small horizontal tab item
*/
horizontalItemPaddingSM: string;
/**
* @desc 纵向标签页标签内间距
* @descEN Vertical padding of vertical tab item
*/
verticalItemPadding: string;
/**
* @desc 纵向标签页标签外间距
* @descEN Vertical margin of vertical tab item
*/
verticalItemMargin: string;
/**
* @desc 标签文本颜色
* @descEN Text color of tab
*/
itemColor: string;
/**
* @desc 标签激活态文本颜色
* @descEN Text color of active tab
*/
itemActiveColor: string;
/**
* @desc 标签悬浮态文本颜色
* @descEN Text color of hover tab
*/
itemHoverColor: string;
/**
* @desc 标签选中态文本颜色
* @descEN Text color of selected tab
*/
itemSelectedColor: string;
/**
* @desc 卡片标签间距
* @descEN Gutter of card tab
*/
cardGutter: number;
}
export interface TabsToken extends FullToken<'Tabs'> {
tabsCardPadding: string;
dropdownEdgeChildVerticalPadding: number;
tabsNavWrapPseudoWidth: number;
tabsDropdownHeight: number | string;
tabsDropdownWidth: number | string;
tabsHorizontalItemMargin: string;
tabsHorizontalItemMarginRTL: string;
}
原生通用属性
Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| style | 自定义样式 | CSSProperties | - |
| className | 自定义类名 | string | - |
| rootClassName | 添加在组件最外层的 className | string | - |
| autoFocus | 自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效 | boolean | false |
安装并复现示例
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23
使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。
原生类型参考
源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。
vendor/components/tabs/TabPane.tsvendor/components/tabs/index.tsxvendor/components/tabs/style/index.ts
来源与许可
https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd
保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。