-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTabs.tsx
More file actions
273 lines (247 loc) · 8.25 KB
/
Tabs.tsx
File metadata and controls
273 lines (247 loc) · 8.25 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import * as React from 'react';
import Gesture, { IGestureStatus } from 'rc-gesture';
import { PropsType as BasePropsType, TabBarPropsType } from './PropsType';
import { TabPane } from './TabPane';
import { DefaultTabBar } from './DefaultTabBar';
import { getTransformPropValue, setTransform, setPxStyle } from './util';
import { Tabs as Component, StateType as BaseStateType } from './Tabs.base';
const getPanDirection = (direction: number|undefined) => {
switch (direction) {
case 2:
case 4:
return 'horizontal';
case 8:
case 16:
return 'vertical';
default:
return 'none';
}
};
export interface PropsType extends BasePropsType {
/** prefix class | default: rmc-tabs */
prefixCls?: string;
}
export class StateType extends BaseStateType {
contentPos?= '';
isMoving?= false;
}
export class Tabs extends Component<PropsType, StateType> {
static DefaultTabBar = DefaultTabBar;
static defaultProps = {
...Component.defaultProps,
prefixCls: 'rmc-tabs',
useOnPan: true,
} as PropsType;
layout: HTMLDivElement;
onPan = (() => {
let lastOffset: number | string = 0;
let finalOffset = 0;
let panDirection: string;
const getLastOffset = (isVertical = this.isTabVertical()) => {
let offset = +`${lastOffset}`.replace('%', '');
if (`${lastOffset}`.indexOf('%') >= 0) {
offset /= 100;
offset *= isVertical ? this.layout.clientHeight : this.layout.clientWidth;
}
return offset;
};
return {
onPanStart: (status: IGestureStatus) => {
if (!this.props.swipeable || !this.props.animated) return;
panDirection = getPanDirection(status.direction);
this.setState({
isMoving: true,
});
},
onPanMove: (status: IGestureStatus) => {
const { swipeable, animated, useLeftInsteadTransform } = this.props;
if (!status.moveStatus || !this.layout || !swipeable || !animated) return;
const isVertical = this.isTabVertical();
let offset = getLastOffset();
if (isVertical) {
offset += panDirection === 'horizontal' ? 0 : status.moveStatus.y;
} else {
offset += panDirection === 'vertical' ? 0 : status.moveStatus.x;
}
const canScrollOffset = isVertical ?
-this.layout.scrollHeight + this.layout.clientHeight :
-this.layout.scrollWidth + this.layout.clientWidth;
offset = Math.min(offset, 0);
offset = Math.max(offset, canScrollOffset);
setPxStyle(this.layout, offset, 'px', isVertical, useLeftInsteadTransform);
finalOffset = offset;
},
onPanEnd: () => {
if (!this.props.swipeable || !this.props.animated) return;
lastOffset = finalOffset;
const isVertical = this.isTabVertical();
const offsetIndex = this.getOffsetIndex(finalOffset, isVertical ? this.layout.clientHeight : this.layout.clientWidth);
this.setState({
isMoving: false,
});
if (offsetIndex === this.state.currentTab) {
if (this.props.usePaged) {
setTransform(
this.layout.style,
this.getContentPosByIndex(
offsetIndex,
this.isTabVertical(),
this.props.useLeftInsteadTransform
)
);
}
} else {
this.goToTab(offsetIndex);
}
},
setCurrentOffset: (offset: number | string) => lastOffset = offset,
};
})();
constructor(props: PropsType) {
super(props);
this.state = {
...this.state,
...new StateType,
contentPos: this.getContentPosByIndex(
this.getTabIndex(props),
this.isTabVertical(props.tabDirection),
props.useLeftInsteadTransform
),
};
}
goToTab(index: number, force = false, usePaged = this.props.usePaged) {
const { tabDirection, useLeftInsteadTransform } = this.props;
let newState = {};
if (usePaged) {
newState = {
contentPos: this.getContentPosByIndex(
index,
this.isTabVertical(tabDirection),
useLeftInsteadTransform
),
};
}
return super.goToTab(index, force, newState);
}
tabClickGoToTab(index: number) {
this.goToTab(index, false, true);
}
getContentPosByIndex(index: number, isVertical: boolean, useLeft = false) {
const value = `${-index * 100}%`;
this.onPan.setCurrentOffset(value);
if (useLeft) {
return `${value}`;
} else {
const translate = isVertical ? `0px, ${value}` : `${value}, 0px`;
// fix: content overlay TabBar on iOS 10. ( 0px -> 1px )
return `translate3d(${translate}, 1px)`;
}
}
onSwipe = (status: IGestureStatus) => {
const { tabBarPosition, swipeable, usePaged } = this.props;
if (!swipeable || !usePaged || this.isTabVertical()) return;
// DIRECTION_NONE 1
// DIRECTION_LEFT 2
// DIRECTION_RIGHT 4
// DIRECTION_UP 8
// DIRECTION_DOWN 16
// DIRECTION_HORIZONTAL 6
// DIRECTION_VERTICAL 24
// DIRECTION_ALL 30
switch (tabBarPosition) {
case 'top':
case 'bottom':
switch (status.direction) {
case 2:
if (!this.isTabVertical()) {
this.goToTab(this.prevCurrentTab + 1);
}
case 8:
if (this.isTabVertical()) {
this.goToTab(this.prevCurrentTab + 1);
}
break;
case 4:
if (!this.isTabVertical()) {
this.goToTab(this.prevCurrentTab - 1);
}
case 16:
if (this.isTabVertical()) {
this.goToTab(this.prevCurrentTab - 1);
}
break;
}
break;
}
}
setContentLayout = (div: HTMLDivElement) => {
this.layout = div;
}
renderContent(getSubElements = this.getSubElements()) {
const { prefixCls, tabs, animated, destroyInactiveTab, useLeftInsteadTransform } = this.props;
const { currentTab, isMoving, contentPos } = this.state;
const isTabVertical = this.isTabVertical();
let contentCls = `${prefixCls}-content-wrap`;
if (animated && !isMoving) {
contentCls += ` ${contentCls}-animated`;
}
const contentStyle: React.CSSProperties = animated ? (
useLeftInsteadTransform ? {
position: 'relative',
...this.isTabVertical() ? { top: contentPos, } : { left: contentPos, }
} : getTransformPropValue(contentPos)
) : {
position: 'relative',
...this.isTabVertical() ? { top: `${-currentTab * 100}%`, } : { left: `${-currentTab * 100}%`, }
};
return <div className={contentCls} style={contentStyle} ref={this.setContentLayout}>
{
tabs.map((tab, index) => {
let cls = `${prefixCls}-pane-wrap`;
if (this.state.currentTab === index) {
cls += ` ${cls}-active`;
} else {
cls += ` ${cls}-inactive`;
}
const key = tab.key || `tab_${index}`;
// update tab cache
if (this.shouldRenderTab(index)) {
this.tabCache[index] = this.getSubElement(tab, index, getSubElements);
} else if (destroyInactiveTab) {
this.tabCache[index] = undefined;
}
return <TabPane key={key} className={cls}
active={currentTab === index}
fixX={isTabVertical} fixY={!isTabVertical}
>
{this.tabCache[index]}
</TabPane>;
})
}
</div>;
}
render() {
const { prefixCls, tabBarPosition, tabDirection, useOnPan, noRenderContent } = this.props;
const isTabVertical = this.isTabVertical(tabDirection);
const tabBarProps: TabBarPropsType = {
...this.getTabBarBaseProps(),
};
const onPan = !isTabVertical && useOnPan ? this.onPan : {};
const content = [
<div key="tabBar" className={`${prefixCls}-tab-bar-wrap`}>
{this.renderTabBar(tabBarProps, DefaultTabBar)}
</div>,
!noRenderContent && <Gesture key="$content"
onSwipe={this.onSwipe}
{...onPan}
>
{this.renderContent()}
</Gesture>,
];
return <div className={`${prefixCls} ${prefixCls}-${tabDirection} ${prefixCls}-${tabBarPosition}`}>
{
tabBarPosition === 'top' || tabBarPosition === 'left' ? content : content.reverse()
}
</div>;
}
}