-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathSwipeableTabBarMixin.js
More file actions
executable file
·184 lines (177 loc) · 5.66 KB
/
Copy pathSwipeableTabBarMixin.js
File metadata and controls
executable file
·184 lines (177 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from 'react';
import classnames from 'classnames';
import Hammer from 'rc-hammerjs';
import ReactDOM from 'react-dom';
import {
isVertical,
getStyle,
setPxStyle,
} from './utils';
export default {
getInitialState() {
const { hasPrevPage, hasNextPage } = this.checkPaginationByKey(this.props.activeKey);
return {
hasPrevPage,
hasNextPage,
};
},
getDefaultProps() {
return {
hammerOptions: {},
pageSize: 5, // per page show how many tabs
speed: 7, // swipe speed, 1 to 10, more bigger more faster
};
},
checkPaginationByKey(activeKey) {
const { panels, pageSize } = this.props;
const index = this.getIndexByKey(activeKey);
const centerTabCount = Math.floor(pageSize / 2);
// the basic rule is to make activeTab be shown in the center of TabBar viewport
return {
hasPrevPage: index - centerTabCount > 0,
hasNextPage: index + centerTabCount < panels.length,
};
},
/**
* used for props.activeKey setting, not for swipe callback
*/
getDeltaByKey(activeKey) {
const { pageSize } = this.props;
const index = this.getIndexByKey(activeKey);
const centerTabCount = Math.floor(pageSize / 2);
const { tabWidth } = this.cache;
const delta = (index - centerTabCount) * tabWidth * -1;
return delta;
},
getIndexByKey(activeKey) {
const { panels } = this.props;
const length = panels.length;
for (let i = 0; i < length; i++) {
if (panels[i].key === activeKey) {
return i;
}
}
return -1;
},
checkPaginationByDelta(delta) {
const { totalAvaliableDelta } = this.cache;
return {
hasPrevPage: delta < 0,
hasNextPage: -delta < totalAvaliableDelta,
};
},
setSwipePositionByKey(activeKey) {
const { hasPrevPage, hasNextPage } = this.checkPaginationByKey(activeKey);
const { totalAvaliableDelta } = this.cache;
this.setState({
hasPrevPage,
hasNextPage,
});
let delta;
if (!hasPrevPage) {
// the first page
delta = 0;
} else if (!hasNextPage) {
// the last page
delta = -totalAvaliableDelta;
} else if (hasNextPage) {
// the middle page
delta = this.getDeltaByKey(activeKey);
}
this.cache.totalDelta = delta;
this.setSwipePosition();
},
setSwipePosition() {
const { totalDelta, vertical } = this.cache;
setPxStyle(this.swipeNode, totalDelta, vertical);
},
updatePropsSwipeComponent() {
const { swipe, nav } = this.refs;
const { tabBarPosition, pageSize, panels, activeKey } = this.props;
this.swipeNode = ReactDOM.findDOMNode(swipe); // dom which scroll (9999px)
this.realNode = ReactDOM.findDOMNode(nav); // dom which visiable in screen (viewport)
const _isVertical = isVertical(tabBarPosition);
const _viewSize = getStyle(this.realNode, _isVertical ? 'height' : 'width');
const _tabWidth = _viewSize / pageSize;
return {
vertical: _isVertical,
totalAvaliableDelta: _tabWidth * panels.length - _viewSize,
tabWidth: _tabWidth,
}
},
componentDidMount() {
const { activeKey } = this.props;
this.cache = this.updatePropsSwipeComponent();
this.setSwipePositionByKey(activeKey);
},
componentWillReceiveProps(nextProps) {
if (nextProps.activeKey && nextProps.activeKey !== this.props.activeKey) {
this.setSwipePositionByKey(nextProps.activeKey);
}
const updateCache = this.updatePropsSwipeComponent();
this.cache.totalAvaliableDelta = updateCache.totalAvaliableDelta;
this.cache.tabWidth = updateCache.totalAvaliableDelta;
},
onPan(e) {
const { vertical, totalAvaliableDelta, totalDelta } = this.cache;
const { speed } = this.props;
// calculate touch distance
let nowDelta = vertical ? e.deltaY : e.deltaX;
nowDelta = nowDelta * (speed / 10);
// calculate distance dom need transform
let _nextDelta = nowDelta + totalDelta;
if (_nextDelta >= 0) {
_nextDelta = 0;
} else if (_nextDelta <= -totalAvaliableDelta) {
_nextDelta = -totalAvaliableDelta;
}
this.cache.totalDelta = _nextDelta;
this.setSwipePosition();
// calculate pagination display
const { hasPrevPage, hasNextPage } = this.checkPaginationByDelta(this.cache.totalDelta);
if (hasPrevPage !== this.state.hasPrevPage || hasNextPage !== this.state.hasNextPage) {
this.setState({
hasPrevPage,
hasNextPage,
});
}
},
getSwipeBarNode(tabs) {
const { prefixCls, hammerOptions, tabBarPosition } = this.props;
const { hasPrevPage, hasNextPage } = this.state;
const navClassName = `${prefixCls}-nav`;
const navClasses = classnames({
[navClassName]: true,
});
const events = {
onPan: this.onPan,
};
return (
<div
className={classnames({
[`${prefixCls}-nav-container`]: 1,
[`${prefixCls}-nav-swipe-container`]: 1,
// page classname can be used to render special style when there has a prev/next page
[`${prefixCls}-prevpage`]: hasPrevPage,
[`${prefixCls}-nextpage`]: hasNextPage,
})}
key="container"
ref={this.saveRef('container')}
>
<div className={`${prefixCls}-nav-wrap`} ref={this.saveRef('navWrap')}>
<Hammer
{...events}
direction={isVertical(tabBarPosition) ? 'DIRECTION_ALL' : 'DIRECTION_HORIZONTAL'}
options={hammerOptions}
>
<div className={`${prefixCls}-nav-swipe`} ref={this.saveRef('swipe')}>
<div className={navClasses} ref={this.saveRef('nav')}>
{tabs}
</div>
</div>
</Hammer>
</div>
</div>
);
},
};