Tabs 标签页
用于让用户在不同的视图中进行切换。规则
标签数量,一般 2-4 个;其中,标签中的文案需要精简,一般 2-4 个字。
在 iOS 端的次级页面中,不建议使用左右滑动来切换 Tab,这个和 iOS 的左滑返回存在冲突,eg:详情页中 Tabs。
代码演示
Basic Usage.
Switch tabs without animation
import { Tabs, WhiteSpace, Badge } from 'antd-mobile';
const TabPane = Tabs.TabPane;
function callback(key) {
console.log('onChange', key);
}
function handleTabClick(key) {
console.log('onTabClick', key);
}
const TabExample = () => (
<div>
<Tabs defaultActiveKey="2" onChange={callback} onTabClick={handleTabClick}>
<TabPane tab={<Badge text={'3'}>First Tab</Badge>} key="1">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of First Tab
</div>
</TabPane>
<TabPane tab={<Badge text={'今日(20)'}>Second Tab</Badge>} key="2">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of Second Tab
</div>
</TabPane>
<TabPane tab={<Badge dot>Third Tab</Badge>} key="3">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of Third Tab
</div>
</TabPane>
</Tabs>
<WhiteSpace />
</div>
);
ReactDOM.render(<TabExample />, mountNode);
There are at most 5 tab panes in the visible area, click on the both sides of
import { Tabs, WhiteSpace } from 'antd-mobile';
const TabPane = Tabs.TabPane;
function callback(key) {
console.log('onChange', key);
}
function handleTabClick(key) {
console.log('onTabClick', key);
}
const TabExample = () => (
<div>
<WhiteSpace />
<Tabs defaultActiveKey="3" animated={false} onChange={callback} onTabClick={handleTabClick}>
<TabPane tab="First tab" key="1">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of First Tab
</div>
</TabPane>
<TabPane tab="Second Tab" key="2">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of Second Tab
</div>
</TabPane>
<TabPane tab="Third Tab" key="3">
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
Content of Third Tab
</div>
</TabPane>
</Tabs>
<WhiteSpace />
</div>
);
ReactDOM.render(<TabExample />, mountNode);
Tabs
to scroll.
import { Tabs, WhiteSpace } from 'antd-mobile';
const TabPane = Tabs.TabPane;
function callback(key) {
console.log('onChange', key);
}
function handleTabClick(key) {
console.log('onTabClick', key);
}
const makeTabPane = key => (
<TabPane tab={`Option${key}`} key={key}>
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '5rem', backgroundColor: '#fff' }}>
{`content of option${key}`}
</div>
</TabPane>
);
const makeMultiTabPane = (count) => {
const result = [];
for (let i = 0; i <= count; i++) {
result.push(makeTabPane(i));
}
return result;
};
const TabExample = () => (
<div>
<Tabs defaultActiveKey="8" onChange={callback} pageSize={5} onTabClick={handleTabClick}>
{makeMultiTabPane(11)}
</Tabs>
<WhiteSpace />
</div>
);
ReactDOM.render(<TabExample />, mountNode);
API
适用平台:WEB、React-NativeTabs
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
activeKey | 当前激活 tab 面板的 key | String | 无 |
defaultActiveKey | 初始化选中面板的 key,如果没有设置 activeKey | String | 第一个面板 |
onChange | 切换面板的回调 | (key: string): void | 无 |
onTabClick | tab 被点击的回调 | (key: string): void | 无 |
animated | 是否动画 | boolean | true |
swipeable | 是否可以滑动 tab 内容进行切换 | boolean | true |
hammerOptions(Web Only ) | 开启swipeable 的时候可以对 hammerjs 的 pan 和 swipe 两种手势进行参数配置 | object | {} |
tabBarPosition | tab 位置 top/bottom | string | top |
destroyInactiveTabPane | 是否销毁掉不活动的 tabPane (优化使用) | boolean | false |
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 前缀 | string | am-tabs |
className(web only ) | 额外的 className | string | 无 |
pageSize(web only ) | 可视区显示的 tab 数量,可以看做一页 | number | 5 |
speed(web only ) | 多页模式下,TabBar 滑动的速度 | Number: 1 ~ 10 | 8 |
tabBarhammerOptions(web only ) | 同hammerOptions,对 TabBar 的滑动手势进行配置 | Obejct | {} |
Tabs.TabPane
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
key | 对应 activeKey | String | 无 |
tab | 选项卡头显示文字 | React.Element or String | 无 |