Skip to content

Commit

Permalink
Merge pull request #56 from react-component/feat/swipeableTabBar
Browse files Browse the repository at this point in the history
feat: add swipeableTabBar for antd-mobile
  • Loading branch information
paranoidjk committed Mar 2, 2017
2 parents 7677efe + 759a282 commit a9e5d20
Show file tree
Hide file tree
Showing 13 changed files with 707 additions and 3 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ React.render(
document.getElementById('t2'));
```

## API
## API

### Tabs

Expand Down Expand Up @@ -237,6 +237,41 @@ tab bar with ink indicator, in addition to tab bar props, extra props:

scrollable tab bar with ink indicator, same with tab bar/ink bar props.

### lib/SwipeableInkTabBar (Use for Mobile)

swipeable tab bar with ink indicator, same with tab bar/ink bar props, and below is the additional props.

<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 100px;">name</th>
<th style="width: 50px;">type</th>
<th>default</th>
<th>description</th>
</tr>
</thead>
<tbody>
<tr>
<td>pageSize</td>
<td>number</td>
<th>5</th>
<td>show how many tabs at one page</td>
</tr>
<tr>
<td>speed</td>
<td>number</td>
<th>5</th>
<td>swipe speed, 1 to 10, more bigger more faster</td>
</tr>
<tr>
<td>hammerOptions</td>
<td>Object</td>
<td></td>
<td>options for react-hammerjs</td>
</tr>
</tbody>
</table>

### lib/TabContent

<table class="table table-bordered table-striped">
Expand Down
14 changes: 14 additions & 0 deletions assets/index/bottom.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@
width: 99999px;
}

&-bottom &-nav-swipe {
position: relative;
left: 0;
.@{tabs-prefix-cls}-nav {
display: flex;
flex: 1;
width: 100%;
.@{tabs-prefix-cls}-tab {
margin-right: 0;
padding: 8px 0;
justify-content: center;
}
}
}
&-bottom &-nav-wrap {
width: 100%;
}
Expand Down
16 changes: 15 additions & 1 deletion assets/index/left.less
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
height: 99999px;
}

&-left &-nav-swipe {
position: relative;
top: 0;
.@{tabs-prefix-cls}-nav {
display: flex;
flex: 1;
flex-direction: column;
height: 100%;
.@{tabs-prefix-cls}-tab {
justify-content: center;
}
}
}

&-left &-tab-prev, &-left &-tab-next {
margin-top: -2px;
height: 32px;
Expand Down Expand Up @@ -73,4 +87,4 @@
&-left &-tab {
padding: 16px 24px;
}
}
}
15 changes: 14 additions & 1 deletion assets/index/right.less
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
height: 99999px;
}

&-right &-nav-swipe {
position: relative;
.@{tabs-prefix-cls}-nav {
display: flex;
flex: 1;
flex-direction: column;
height: 100%;
.@{tabs-prefix-cls}-tab {
justify-content: center;
}
}
}

&-right &-tab-prev, &-right &-tab-next {
margin-top: -2px;
height: 32px;
Expand Down Expand Up @@ -73,4 +86,4 @@
&-right &-tab {
padding: 16px 24px;
}
}
}
15 changes: 15 additions & 0 deletions assets/index/top.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
width: 99999px;
}

&-top &-nav-swipe {
position: relative;
left: 0;
.@{tabs-prefix-cls}-nav {
display: flex;
flex: 1;
width: 100%;
.@{tabs-prefix-cls}-tab {
margin-right: 0;
padding: 8px 0;
justify-content: center;
}
}
}

&-top &-nav-wrap {
width: 100%;
}
Expand Down
1 change: 1 addition & 0 deletions examples/swipeInkTabBar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
placeholder
86 changes: 86 additions & 0 deletions examples/swipeInkTabBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint react/no-multi-comp:0, no-console:0 */

import 'rc-tabs/assets/index.less';
import React from 'react';
import ReactDOM from 'react-dom';
import Tabs, { TabPane } from 'rc-tabs';
import TabContent from '../src/SwipeableTabContent';
import SwipeableInkTabBar from '../src/SwipeableInkTabBar';

const contentStyle = {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: '100px',
backgroundColor: '#fff',
};


const makeTabPane = key => (
<TabPane tab={`选项${key}`} key={key}>
<div style={contentStyle}>
{`选项${key}内容`}
</div>
</TabPane>
);

const makeMultiTabPane = (count) => {
const result = [];
for (let i = 0; i < count; i++) {
result.push(makeTabPane(i));
}
return result;
};

const Component = () => (
<div>
<h1>pageSize = 5, speed = 5</h1>
<div>
<Tabs
renderTabBar={() =>
<SwipeableInkTabBar
pageSize={5}
speed={5}
/>
}
renderTabContent={() => <TabContent />}
defaultActiveKey="8"
>
{makeMultiTabPane(11)}
</Tabs>
</div>
<h1>pageSize = 3, speed = 10</h1>
<div>
<Tabs
renderTabBar={() =>
<SwipeableInkTabBar
pageSize={3}
speed={10}
/>
}
renderTabContent={() => <TabContent/>}
defaultActiveKey="2"
>
{makeMultiTabPane(7)}
</Tabs>
</div>
<h1>pageSize = 3, speed = 10, tabBarPosition='bottom'</h1>
<div>
<Tabs
tabBarPosition="bottom"
renderTabBar={() =>
<SwipeableInkTabBar
pageSize={3}
speed={10}
/>
}
renderTabContent={() => <TabContent/>}
defaultActiveKey="2"
>
{makeMultiTabPane(7)}
</Tabs>
</div>
</div>
);

ReactDOM.render(<Component />, document.getElementById('__react-content'));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"start": "rc-tools run server",
"pub": "rc-tools run pub --babel-runtime",
"lint": "rc-tools run lint",
"watch": "rc-tools run watch",
"karma": "rc-tools run karma",
"saucelabs": "rc-tools run saucelabs",
"test": "jest",
Expand Down
65 changes: 65 additions & 0 deletions src/SwipeableInkTabBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import InkTabBarMixin from './InkTabBarMixin';
import SwipeableTabBarMixin from './SwipeableTabBarMixin';
import TabBarMixin from './TabBarMixin';

const SwipeableInkTabBar = React.createClass({
mixins: [TabBarMixin, InkTabBarMixin, SwipeableTabBarMixin],

getSwipeableTabs() {
const props = this.props;
const children = props.panels;
const activeKey = props.activeKey;
const rst = [];
const prefixCls = props.prefixCls;

const tabStyle = {
display: 'flex',
flex: `0 0 ${1 / props.pageSize * 100}%`,
};

React.Children.forEach(children, (child) => {
if (!child) {
return;
}
const key = child.key;
let cls = activeKey === key ? `${prefixCls}-tab-active` : '';
cls += ` ${prefixCls}-tab`;
let events = {};
if (child.props.disabled) {
cls += ` ${prefixCls}-tab-disabled`;
} else {
events = {
onClick: this.onTabClick.bind(this, key),
};
}
const ref = {};
if (activeKey === key) {
ref.ref = 'activeTab';
}
rst.push(<div
role="tab"
style={tabStyle}
aria-disabled={child.props.disabled ? 'true' : 'false'}
aria-selected={activeKey === key ? 'true' : 'false'}
{...events}
className={cls}
key={key}
{...ref}
>
{child.props.tab}
</div>);
});

return rst;
},

render() {
const inkBarNode = this.getInkBarNode();
const tabs = this.getSwipeableTabs();
const scrollbarNode = this.getSwipeBarNode([inkBarNode, tabs]);
return this.getRootNode(scrollbarNode);
},
});

export default SwipeableInkTabBar;

0 comments on commit a9e5d20

Please sign in to comment.