Tabs 标签页

用于让用户在不同的视图中进行切换。

规则

  • 标签数量,一般 2-4 个;其中,标签中的文案需要精简,一般 2-4 个字。

  • 在 iOS 端的次级页面中,不建议使用左右滑动来切换 Tab,这个和 iOS 的左滑返回存在冲突,eg:详情页中 Tabs。

代码演示

基本用法

Basic Usage.

  1. import { Tabs, WhiteSpace, Badge } from 'antd-mobile';
  2. const TabPane = Tabs.TabPane;
  3. function callback(key) {
  4. console.log('onChange', key);
  5. }
  6. function handleTabClick(key) {
  7. console.log('onTabClick', key);
  8. }
  9. const TabExample = () => (
  10. <div>
  11. <Tabs defaultActiveKey="2" onChange={callback} onTabClick={handleTabClick}>
  12. <TabPane tab={<Badge text={'3'}>First Tab</Badge>} key="1">
  13. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  14. Content of First Tab
  15. </div>
  16. </TabPane>
  17. <TabPane tab={<Badge text={'今日(20)'}>Second Tab</Badge>} key="2">
  18. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  19. Content of Second Tab
  20. </div>
  21. </TabPane>
  22. <TabPane tab={<Badge dot>Third Tab</Badge>} key="3">
  23. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  24. Content of Third Tab
  25. </div>
  26. </TabPane>
  27. </Tabs>
  28. <WhiteSpace />
  29. </div>
  30. );
  31. ReactDOM.render(<TabExample />, mountNode);

无动画

Switch tabs without animation

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. const TabPane = Tabs.TabPane;
  3. function callback(key) {
  4. console.log('onChange', key);
  5. }
  6. function handleTabClick(key) {
  7. console.log('onTabClick', key);
  8. }
  9. const TabExample = () => (
  10. <div>
  11. <WhiteSpace />
  12. <Tabs defaultActiveKey="3" animated={false} onChange={callback} onTabClick={handleTabClick}>
  13. <TabPane tab="First tab" key="1">
  14. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  15. Content of First Tab
  16. </div>
  17. </TabPane>
  18. <TabPane tab="Second Tab" key="2">
  19. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  20. Content of Second Tab
  21. </div>
  22. </TabPane>
  23. <TabPane tab="Third Tab" key="3">
  24. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  25. Content of Third Tab
  26. </div>
  27. </TabPane>
  28. </Tabs>
  29. <WhiteSpace />
  30. </div>
  31. );
  32. ReactDOM.render(<TabExample />, mountNode);

超出界面宽度,多于5个标签

There are at most 5 tab panes in the visible area, click on the both sides of Tabs to scroll.

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. const TabPane = Tabs.TabPane;
  3. function callback(key) {
  4. console.log('onChange', key);
  5. }
  6. function handleTabClick(key) {
  7. console.log('onTabClick', key);
  8. }
  9. const makeTabPane = key => (
  10. <TabPane tab={`Option${key}`} key={key}>
  11. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
  12. {`content of option${key}`}
  13. </div>
  14. </TabPane>
  15. );
  16. const makeMultiTabPane = (count) => {
  17. const result = [];
  18. for (let i = 0; i <= count; i++) {
  19. result.push(makeTabPane(i));
  20. }
  21. return result;
  22. };
  23. const TabExample = () => (
  24. <div>
  25. <Tabs defaultActiveKey="8" onChange={callback} pageSize={5} onTabClick={handleTabClick}>
  26. {makeMultiTabPane(11)}
  27. </Tabs>
  28. <WhiteSpace />
  29. </div>
  30. );
  31. ReactDOM.render(<TabExample />, mountNode);

Tabs标签页 - 图1

API

适用平台:WEB、React-Native

Tabs

属性说明类型默认值
activeKey当前激活 tab 面板的 keyString
defaultActiveKey初始化选中面板的 key,如果没有设置 activeKeyString第一个面板
onChange切换面板的回调(key: string): void
onTabClicktab 被点击的回调(key: string): void
animated是否动画booleantrue
swipeable是否可以滑动 tab 内容进行切换booleantrue
hammerOptions(Web Only)开启swipeable的时候可以对 hammerjspanswipe 两种手势进行参数配置object{}
tabBarPositiontab 位置 top/bottomstringtop
destroyInactiveTabPane是否销毁掉不活动的 tabPane (优化使用)booleanfalse
underlineColor(react-native only)线条颜色string#ddd
activeUnderlineColor(react-native only)选中线条颜色string#108ee9
textColor(react-native only)文字颜色string#000
activeTextColor(react-native only)选中文字颜色string#108ee9
barStyle(react-native only)tabs bar 样式object{}
prefixCls(web only)className 前缀stringam-tabs
className(web only)额外的 classNamestring
pageSize(web only)可视区显示的 tab 数量,可以看做一页number5
speed(web only)多页模式下,TabBar 滑动的速度Number: 1 ~ 108
tabBarhammerOptions(web only)同hammerOptions,对 TabBar 的滑动手势进行配置Obejct{}

Tabs.TabPane

属性说明类型默认值
key对应 activeKeyString
tab选项卡头显示文字React.Element or String