-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
264 lines (241 loc) · 7.5 KB
/
index.js
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
import React from "react";
import PropTypes from "prop-types";
import {
UIManager,
View,
dismissKeyboard,
requireNativeComponent,
findNodeHandle,
ColorPropType,
processColor
} from "react-native";
var ReactPropTypes = PropTypes;
var VIEWPAGER_REF = "viewPager";
type Event = Object;
export type ViewPagerScrollState = $Enum<{
idle: string,
dragging: string,
settling: string
}>;
/**
* Container that allows to flip left and right between child views. Each
* child view of the `TabbedViewPager` will be treated as a separate page
* and will be stretched to fill the `TabbedViewPager`.
*
* It is important all children are `<View>`s and not composite components.
* You can set style properties like `padding` or `backgroundColor` for each
* child.
*
* Example:
*
* ```
* render: function() {
* return (
* <TabbedViewPager
* style={styles.viewPager}
* initialPage={0}>
* <View style={styles.pageStyle}>
* <Text>First page</Text>
* </View>
* <View style={styles.pageStyle}>
* <Text>Second page</Text>
* </View>
* </TabbedViewPager>
* );
* }
*
* ...
*
* var styles = {
* ...
* pageStyle: {
* alignItems: 'center',
* padding: 20,
* }
* }
* ```
*/
class TabbedViewPager extends React.Component {
props: {
initialPage?: number,
onPageScroll?: Function,
onPageScrollStateChanged?: Function,
onPageSelected?: Function,
pageMargin?: number,
keyboardDismissMode?: "none" | "on-drag",
scrollEnabled?: boolean
};
static propTypes = {
...View.propTypes,
/**
* Index of initial page that should be selected. Use `setPage` method to
* update the page, and `onPageSelected` to monitor page changes
*/
initialPage: ReactPropTypes.number,
/**
* Executed when transitioning between pages (ether because of animation for
* the requested page change or when user is swiping/dragging between pages)
* The `event.nativeEvent` object for this callback will carry following data:
* - position - index of first page from the left that is currently visible
* - offset - value from range [0,1) describing stage between page transitions.
* Value x means that (1 - x) fraction of the page at "position" index is
* visible, and x fraction of the next page is visible.
*/
onPageScroll: ReactPropTypes.func,
/**
* Function called when the page scrolling state has changed.
* The page scrolling state can be in 3 states:
* - idle, meaning there is no interaction with the page scroller happening at the time
* - dragging, meaning there is currently an interaction with the page scroller
* - settling, meaning that there was an interaction with the page scroller, and the
* page scroller is now finishing it's closing or opening animation
*/
onPageScrollStateChanged: ReactPropTypes.func,
/**
* This callback will be called once ViewPager finish navigating to selected page
* (when user swipes between pages). The `event.nativeEvent` object passed to this
* callback will have following fields:
* - position - index of page that has been selected
*/
onPageSelected: ReactPropTypes.func,
/**
* Blank space to show between pages. This is only visible while scrolling, pages are still
* edge-to-edge.
*/
pageMargin: ReactPropTypes.number,
/**
* Determines whether the keyboard gets dismissed in response to a drag.
* - 'none' (the default), drags do not dismiss the keyboard.
* - 'on-drag', the keyboard is dismissed when a drag begins.
*/
keyboardDismissMode: ReactPropTypes.oneOf([
"none", // default
"on-drag"
]),
/**
* When false, the content does not scroll.
* The default value is true.
*/
scrollEnabled: ReactPropTypes.bool,
/**
* Tab properties
*/
tabGravity: ReactPropTypes.oneOf(["fill", "center"]),
tabMode: ReactPropTypes.oneOf(["fixed", "scrollable"]),
tabBackground: ColorPropType,
tabIndicatorColor: ColorPropType,
tabTextColor: ColorPropType,
tabSelectedTextColor: ColorPropType,
tabIndicatorHeight: ReactPropTypes.number,
tabElevation: ReactPropTypes.number,
tabNames: ReactPropTypes.array.isRequired
};
componentDidMount() {
if (this.props.initialPage != null) {
this.setPageWithoutAnimation(this.props.initialPage);
}
}
getInnerViewNode = (): ReactComponent => {
return this.refs[VIEWPAGER_REF].getInnerViewNode();
};
_childrenWithOverridenStyle = (): Array => {
// Override styles so that each page will fill the parent. Native component
// will handle positioning of elements, so it's not important to offset
// them correctly.
return React.Children.map(this.props.children, function(child) {
if (!child) {
return null;
}
var newProps = {
...child.props,
style: [
child.props.style,
{
position: "absolute",
left: 0,
top: 0,
right: 0,
bottom: 0,
width: undefined,
height: undefined
}
],
collapsable: false
};
if (
child.type &&
child.type.displayName &&
child.type.displayName !== "RCTView" &&
child.type.displayName !== "View"
) {
console.warn(
"Each ViewPager child must be a <View>. Was " + child.type.displayName
);
}
return React.createElement(child.type, newProps);
});
};
_onPageScroll = (e: Event) => {
if (this.props.onPageScroll) {
this.props.onPageScroll(e);
}
if (this.props.keyboardDismissMode === "on-drag") {
dismissKeyboard();
}
};
_onPageScrollStateChanged = (e: Event) => {
if (this.props.onPageScrollStateChanged) {
this.props.onPageScrollStateChanged(e.nativeEvent.pageScrollState);
}
};
_onPageSelected = (e: Event) => {
if (this.props.onPageSelected) {
this.props.onPageSelected(e);
}
};
/**
* A helper function to scroll to a specific page in the ViewPager.
* The transition between pages will be animated.
*/
setPage = (selectedPage: number) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.TabbedViewPager.Commands.setPage,
[selectedPage]
);
};
/**
* A helper function to scroll to a specific page in the ViewPager.
* The transition between pages will *not* be animated.
*/
setPageWithoutAnimation = (selectedPage: number) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
UIManager.TabbedViewPager.Commands.setPageWithoutAnimation,
[selectedPage]
);
};
render() {
return (
<NativeTabbedViewPager
{...this.props}
ref={VIEWPAGER_REF}
style={this.props.style}
onPageScroll={this._onPageScroll}
onPageScrollStateChanged={this._onPageScrollStateChanged}
onPageSelected={this._onPageSelected}
tabTextColor={processColor(this.props.tabTextColor)}
tabBackground={processColor(this.props.tabBackground)}
tabSelectedTextColor={processColor(this.props.tabSelectedTextColor)}
tabIndicatorColor={processColor(this.props.tabIndicatorColor)}
children={this._childrenWithOverridenStyle()}
/>
);
}
}
var NativeTabbedViewPager = requireNativeComponent(
"TabbedViewPager",
TabbedViewPager
);
var TabbedViewPagerAndroid = TabbedViewPager;
module.exports = TabbedViewPagerAndroid;