Tabs 标签页

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

规则

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

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

代码演示

基本用法

Basic Usage.

  1. import { Tabs, WhiteSpace, Badge } from 'antd-mobile';
  2. const tabs = [
  3. { title: <Badge text={'3'}>First Tab</Badge> },
  4. { title: <Badge text={'今日(20)'}>Second Tab</Badge> },
  5. { title: <Badge dot>Third Tab</Badge> },
  6. ];
  7. const tabs2 = [
  8. { title: 'First Tab', sub: '1' },
  9. { title: 'Second Tab', sub: '2' },
  10. { title: 'Third Tab', sub: '3' },
  11. ];
  12. const TabExample = () => (
  13. <div>
  14. <Tabs tabs={tabs}
  15. initialPage={1}
  16. onChange={(tab, index) => { console.log('onChange', index, tab); }}
  17. onTabClick={(tab, index) => { console.log('onTabClick', index, tab); }}
  18. >
  19. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  20. Content of first tab
  21. </div>
  22. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  23. Content of second tab
  24. </div>
  25. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  26. Content of third tab
  27. </div>
  28. </Tabs>
  29. <WhiteSpace />
  30. <Tabs tabs={tabs2}
  31. initialPage={1}
  32. tabBarPosition="bottom"
  33. renderTab={tab => <span>{tab.title}</span>}
  34. >
  35. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  36. Content of first tab
  37. </div>
  38. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  39. Content of second tab
  40. </div>
  41. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  42. Content of third tab
  43. </div>
  44. </Tabs>
  45. <WhiteSpace />
  46. </div>
  47. );
  48. ReactDOM.render(<TabExample />, mountNode);

TabBar sticky

use react-sticky

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. import { StickyContainer, Sticky } from 'react-sticky';
  3. function renderTabBar(props) {
  4. return (<Sticky>
  5. {({ style }) => <div style={{ ...style, zIndex: 1 }}><Tabs.DefaultTabBar {...props} /></div>}
  6. </Sticky>);
  7. }
  8. const tabs = [
  9. { title: 'First Tab', key: 't1' },
  10. { title: 'Second Tab', key: 't2' },
  11. { title: 'Third Tab', key: 't3' },
  12. ];
  13. const TabExample = () => (
  14. <div>
  15. <WhiteSpace />
  16. <StickyContainer>
  17. <Tabs tabs={tabs}
  18. initialPage={'t2'}
  19. renderTabBar={renderTabBar}
  20. >
  21. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  22. Content of first tab
  23. </div>
  24. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  25. Content of second tab
  26. </div>
  27. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  28. Content of third tab
  29. </div>
  30. </Tabs>
  31. </StickyContainer>
  32. <WhiteSpace />
  33. </div>
  34. );
  35. ReactDOM.render(<TabExample />, mountNode);

无动画

Switch tabs without animation

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. const tabs = [
  3. { title: 'First Tab' },
  4. { title: 'Second Tab' },
  5. { title: 'Third Tab' },
  6. ];
  7. const TabExample = () => (
  8. <div>
  9. <WhiteSpace />
  10. <Tabs tabs={tabs} initialPage={2} animated={false} useOnPan={false}>
  11. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  12. Content of first tab
  13. </div>
  14. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  15. Content of second tab
  16. </div>
  17. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  18. Content of third tab
  19. </div>
  20. </Tabs>
  21. <WhiteSpace />
  22. </div>
  23. );
  24. ReactDOM.render(<TabExample />, mountNode);

固定高度

固定外部容器高度

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. const tabs = [
  3. { title: 'First Tab', key: 't1' },
  4. { title: 'Second Tab', key: 't2' },
  5. { title: 'Third Tab', key: 't3' },
  6. ];
  7. const TabExample = () => (
  8. <div>
  9. <WhiteSpace />
  10. <div style={{ height: 200 }}>
  11. <Tabs tabs={tabs}
  12. initialPage={'t2'}
  13. >
  14. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  15. Content of first tab
  16. </div>
  17. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  18. Content of second tab
  19. </div>
  20. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  21. Content of third tab
  22. </div>
  23. </Tabs>
  24. </div>
  25. </div>
  26. );
  27. ReactDOM.render(<TabExample />, mountNode);

垂直样式

use react-sticky

  1. import { Tabs, WhiteSpace } from 'antd-mobile';
  2. const tabs = [
  3. { title: '1 Tab', key: 't1' },
  4. { title: '2 Tab', key: 't2' },
  5. { title: '3 Tab', key: 't3' },
  6. ];
  7. const TabExample = () => (
  8. <div style={{ height: 200 }}>
  9. <WhiteSpace />
  10. <Tabs tabs={tabs}
  11. initialPage={'t2'}
  12. tabBarPosition="left"
  13. tabDirection="vertical"
  14. >
  15. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  16. Content of first tab
  17. </div>
  18. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  19. Content of second tab
  20. </div>
  21. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '250px', backgroundColor: '#fff' }}>
  22. Content of third tab
  23. </div>
  24. </Tabs>
  25. <WhiteSpace />
  26. </div>
  27. );
  28. 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. class Demo extends React.Component {
  3. renderContent = tab =>
  4. (<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '150px', backgroundColor: '#fff' }}>
  5. <p>Content of {tab.title}</p>
  6. </div>);
  7. render() {
  8. const tabs = [
  9. { title: '1st Tab' },
  10. { title: '2nd Tab' },
  11. { title: '3rd Tab' },
  12. { title: '4th Tab' },
  13. { title: '5th Tab' },
  14. { title: '6th Tab' },
  15. { title: '7th Tab' },
  16. { title: '8th Tab' },
  17. { title: '9th Tab' },
  18. ];
  19. return (
  20. <div>
  21. <WhiteSpace />
  22. <Tabs tabs={tabs} renderTabBar={props => <Tabs.DefaultTabBar {...props} page={3} />}>
  23. {this.renderContent}
  24. </Tabs>
  25. <WhiteSpace />
  26. </div>
  27. );
  28. }
  29. }
  30. ReactDOM.render(<Demo />, mountNode);

Tabs标签页 - 图1

API

Tabs

属性说明类型默认值必选
prefixCls样式前缀stringrmc-tabsfalse
tabstab数据Models.TabData[]true
tabBarPositionTabBar位置'top' | 'bottom' | 'left' | 'right'topfalse
renderTabBar替换TabBar((props: TabBarPropsType) => React.ReactNode) | falsefalse
initialPage初始化Tab, index or keynumber | stringfalse
page当前Tab, index or keynumber | stringfalse
swipeable是否可以滑动内容切换booleantruefalse
useOnPan使用跟手滚动booleantruefalse
prerenderingSiblingsNumber预加载两侧Tab数量number1false
animated是否开启切换动画booleantruefalse
onChangetab变化时触发(tab: Models.TabData, index: number) => voidfalse
onTabClicktab 被点击的回调(tab: Models.TabData, index: number) => voidfalse
destroyInactiveTab销毁超出范围Tabbooleanfalsefalse
distanceToChangeTab滑动切换阈值(宽度比例)number0.3false
usePaged是否启用分页模式booleantruefalse
tabDirectionTab方向'horizontal' | 'vertical'horizontalfalse
tabBarUnderlineStyletabBar下划线样式React.CSSProperties | anyfalse
tabBarBackgroundColortabBar背景色stringfalse
tabBarActiveTextColortabBar激活Tab文字颜色stringfalse
tabBarInactiveTextColortabBar非激活Tab文字颜色stringfalse
tabBarTextStyletabBar文字样式React.CSSProperties | anyfalse
renderTab替换TabBar的Tab(tab: Models.TabData) => React.ReactNodefalse

Tabs.DefaultTabBar

属性说明类型默认值必选
goToTab跳转Tab(index: number) => booleantrue
tabstab数据Models.TabData[]true
activeTab当前激活Tab索引numbertrue
animated是否使用动画booleantrue
prefixCls样式前缀stringam-tabs-default-barfalse
renderTab替换TabBar的Tab(tab: Models.TabData) => React.ReactNodefalse
pageTab分页尺寸number5false
onTabClicktab 被点击的回调(tab: Models.TabData, index: number) => voidfalse