-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathPagerView.tsx
170 lines (154 loc) · 4.4 KB
/
PagerView.tsx
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
import React from 'react';
import { Platform, Keyboard } from 'react-native';
import { I18nManager } from 'react-native';
import type * as ReactNative from 'react-native';
import {
childrenWithOverriddenStyle,
} from './utils';
import PagerViewNativeComponent, {
Commands as PagerViewNativeCommands,
OnPageScrollEventData,
OnPageScrollStateChangedEventData,
OnPageSelectedEventData,
NativeProps,
} from './PagerViewNativeComponent';
/**
* Container that allows to flip left and right between child views. Each
* child view of the `PagerView` will be treated as a separate page
* and will be stretched to fill the `PagerView`.
*
* It is important all children are `<View>`s and not composite components.
* You can set style properties like `padding` or `backgroundColor` for each
* child. It is also important that each child have a `key` prop.
*
* Example:
*
* ```
* render: function() {
* return (
* <PagerView
* style={styles.PagerView}
* initialPage={0}>
* <View style={styles.pageStyle} key="1">
* <Text>First page</Text>
* </View>
* <View style={styles.pageStyle} key="2">
* <Text>Second page</Text>
* </View>
* </PagerView>
* );
* }
*
* ...
*
* var styles = {
* ...
* PagerView: {
* flex: 1
* },
* pageStyle: {
* alignItems: 'center',
* padding: 20,
* }
* }
* ```
*/
export class PagerView extends React.Component<NativeProps> {
private isScrolling = false;
pagerView: React.ElementRef<typeof PagerViewNativeComponent> | null = null;
private get deducedLayoutDirection() {
if (
!this.props.layoutDirection ||
//@ts-ignore fix it
this.props.layoutDirection === 'locale'
) {
return I18nManager.isRTL ? 'rtl' : 'ltr';
} else {
return this.props.layoutDirection;
}
}
private _onPageScroll = (
e: ReactNative.NativeSyntheticEvent<OnPageScrollEventData>
) => {
if (this.props.onPageScroll) {
this.props.onPageScroll(e);
}
// Not implemented on iOS yet
if (Platform.OS === 'android') {
if (this.props.keyboardDismissMode === 'on-drag') {
Keyboard.dismiss();
}
}
};
private _onPageScrollStateChanged = (
e: ReactNative.NativeSyntheticEvent<OnPageScrollStateChangedEventData>
) => {
if (this.props.onPageScrollStateChanged) {
this.props.onPageScrollStateChanged(e);
}
this.isScrolling = e.nativeEvent.pageScrollState === 'dragging';
};
private _onPageSelected = (
e: ReactNative.NativeSyntheticEvent<OnPageSelectedEventData>
) => {
if (this.props.onPageSelected) {
this.props.onPageSelected(e);
}
};
private _onMoveShouldSetResponderCapture = () => {
return this.isScrolling;
};
/**
* A helper function to scroll to a specific page in the PagerView.
* The transition between pages will be animated.
*/
public setPage = (selectedPage: number) => {
if (this.pagerView) {
PagerViewNativeCommands.setPage(this.pagerView, selectedPage);
}
};
/**
* A helper function to scroll to a specific page in the PagerView.
* The transition between pages will *not* be animated.
*/
public setPageWithoutAnimation = (selectedPage: number) => {
if (this.pagerView) {
PagerViewNativeCommands.setPageWithoutAnimation(
this.pagerView,
selectedPage
);
}
};
/**
* A helper function to enable/disable scroll imperatively
* The recommended way is using the scrollEnabled prop, however, there might be a case where a
* imperative solution is more useful (e.g. for not blocking an animation)
*/
public setScrollEnabled = (scrollEnabled: boolean) => {
if (this.pagerView) {
PagerViewNativeCommands.setScrollEnabledImperatively(
this.pagerView,
scrollEnabled
);
}
};
render() {
return (
<PagerViewNativeComponent
{...this.props}
ref={(ref) => {
this.pagerView = ref;
}}
style={this.props.style}
layoutDirection={this.deducedLayoutDirection}
onPageScroll={this._onPageScroll}
onPageScrollStateChanged={this._onPageScrollStateChanged}
onPageSelected={this._onPageSelected}
onMoveShouldSetResponderCapture={
this._onMoveShouldSetResponderCapture
}
children={childrenWithOverriddenStyle(this.props.children)}
/>
);
}
}