Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add swipeableTabBar for antd-mobile #54

Merged
merged 1 commit into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions assets/index/bottom.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
width: 99999px;
}

&-bottom &-nav-swipe {
width: 99999px;
position: relative;
left: 0;
}

&-bottom &-nav-wrap {
width: 100%;
}
Expand Down
6 changes: 6 additions & 0 deletions assets/index/left.less
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
height: 99999px;
}

&-left &-nav-swipe {
height: 99999px;
position: relative;
top: 0;
}

&-left &-tab-prev, &-left &-tab-next {
margin-top: -2px;
height: 32px;
Expand Down
6 changes: 6 additions & 0 deletions assets/index/right.less
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
height: 99999px;
}

&-right &-nav-swipe {
height: 99999px;
position: relative;
top: 0;
}

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

&-top &-nav-swipe {
width: 99999px;
position: relative;
left: 0;
}

&-top &-nav-wrap {
width: 100%;
}
Expand Down
1 change: 1 addition & 0 deletions examples/swipeTabBar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
placeholder
98 changes: 98 additions & 0 deletions examples/swipeTabBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* 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 SwipeableTabBar from '../src/SwipeableTabBar';

class PanelContent extends React.Component {
constructor(props) {
super(props);
console.log(this.props.id, 'constructor');
}

componentWillReceiveProps() {
console.log(this.props.id, 'componentWillReceiveProps');
}

render() {
const count = [1, 1, 1, 1];// new Array(4) skip forEach ....
const els = count.map((c, i) => {
return <p key={i}>{this.props.id}</p>;
});
return <div>{els}</div>;
}
}
PanelContent.propTypes = {
id: React.PropTypes.number,
};

const Component = React.createClass({
getInitialState() {
return {
activeKey: '',
start: 0,
};
},

onChange(activeKey) {
console.log(`onChange ${activeKey}`);
this.setState({
activeKey,
});
},

onTabClick(key) {
console.log(`onTabClick ${key}`);
if (key === this.state.activeKey) {
this.setState({
activeKey: '',
});
}
},


render() {
const start = this.state.start;
return (
<div style={{ margin: '20 0', width: '100%' }}>
<h1>Swipe Tabs, please visit this demo at mobile, or chrome simulator</h1>
<Tabs
renderTabBar={() => <SwipeableTabBar onTabClick={this.onTabClick}/>}
renderTabContent={() => <TabContent animatedWithMargin />}
activeKey={this.state.activeKey}
onChange={this.onChange}
>
<TabPane tab={`tab ${start}`} key="0">
<PanelContent id={start}/>
</TabPane>
<TabPane tab={`tab ${start + 1}`} key="1">
<PanelContent id={start + 1}/>
</TabPane>
<TabPane tab={`tab ${start + 2}`} key="2">
<PanelContent id={start + 2}/>
</TabPane>
<TabPane tab={`tab ${start + 3}`} key="3">
<PanelContent id={start + 3}/>
</TabPane>
<TabPane tab={`tab ${start + 4}`} key="4">
<PanelContent id={start + 4}/>
</TabPane>
<TabPane tab={`tab ${start + 5}`} key="5">
<PanelContent id={start + 5}/>
</TabPane>
<TabPane tab={`tab ${start + 6}`} key="6">
<PanelContent id={start + 6}/>
</TabPane>
<TabPane tab={`tab ${start + 7}`} key="7">
<PanelContent id={start + 7}/>
</TabPane>
</Tabs>
</div>
);
},
});

ReactDOM.render(<Component />, document.getElementById('__react-content'));
15 changes: 15 additions & 0 deletions src/SwipeableTabBar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import SwipeableTabBarMixin from './SwipeableTabBarMixin';
import TabBarMixin from './TabBarMixin';


const SwipeableTabBar = React.createClass({
mixins: [TabBarMixin, SwipeableTabBarMixin],
render() {
const tabs = this.getTabs();
const swipebarNode = this.getSwipeBarNode(tabs);
return this.getRootNode(swipebarNode);
},
});

export default SwipeableTabBar;
75 changes: 75 additions & 0 deletions src/SwipeableTabBarMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from 'react';
import classnames from 'classnames';
import Hammer from 'react-hammerjs';
import ReactDOM from 'react-dom';
import {
isVertical,
} from './utils';

export default {
getDefaultProps() {
return {
hammerOptions: {},
};
},
componentDidMount() {
this.swipeNode = ReactDOM.findDOMNode(this.refs.swipe);
this.realNode = ReactDOM.findDOMNode(this.refs.nav);
// todo: do we need to handle possible props.tabBarPosition change in componentWillReceiveProps
this._isVertical = isVertical(this.props.tabBarPosition);
this._relativeDirection = this._isVertical ? 'top' : 'left';
const screen = document.documentElement;
const screenSize = this._isVertical ? screen.clientHeight : screen.clientWidth;
const totalSize = this._isVertical ? this.realNode.offsetHeight : this.realNode.offsetWidth;
this._totalAvaliableDelta = totalSize - screenSize;
},
onPan(e) {
const nowDelta = this._isVertical ? e.deltaY : e.deltaX;
const preDelta = +getComputedStyle(this.swipeNode)
.getPropertyValue(this._relativeDirection)
.replace('px', '');
const nextTotalDelta = nowDelta + preDelta;
if (nextTotalDelta < 0 && -nextTotalDelta < this._totalAvaliableDelta) {
this.swipeNode.style[this._relativeDirection] = `${nextTotalDelta}px`;
}
},
getSwipeBarNode(tabs) {
const { prefixCls, hammerOptions, tabBarPosition } = this.props;
const navClassName = `${prefixCls}-nav`;
const navClasses = classnames({
[navClassName]: true,
});
let direction = {};
if (isVertical(tabBarPosition)) {
direction = {
vertical: true,
};
}
const events = {
onPan: this.onPan,
};
return (
<div
className={classnames({
[`${prefixCls}-nav-container`]: 1,
})}
key="container"
ref="container"
>
<div className={`${prefixCls}-nav-wrap`} ref="navWrap">
<Hammer
{...events}
{...direction}
options={hammerOptions}
>
<div className={`${prefixCls}-nav-swipe`} ref="swipe">
<div className={navClasses} ref="nav">
{tabs}
</div>
</div>
</Hammer>
</div>
</div>
);
},
};