UNPKG

4.36 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5import React from 'react';
6import { Platform, UIManager, Keyboard } from 'react-native';
7import ReactNative from 'react-native';
8import { childrenWithOverriddenStyle } from './utils';
9import { getViewManagerConfig, ViewpagerViewManager } from './ViewPagerNative';
10/**
11 * Container that allows to flip left and right between child views. Each
12 * child view of the `ViewPager` will be treated as a separate page
13 * and will be stretched to fill the `ViewPager`.
14 *
15 * It is important all children are `<View>`s and not composite components.
16 * You can set style properties like `padding` or `backgroundColor` for each
17 * child. It is also important that each child have a `key` prop.
18 *
19 * Example:
20 *
21 * ```
22 * render: function() {
23 * return (
24 * <ViewPager
25 * style={styles.viewPager}
26 * initialPage={0}>
27 * <View style={styles.pageStyle} key="1">
28 * <Text>First page</Text>
29 * </View>
30 * <View style={styles.pageStyle} key="2">
31 * <Text>Second page</Text>
32 * </View>
33 * </ViewPager>
34 * );
35 * }
36 *
37 * ...
38 *
39 * var styles = {
40 * ...
41 * viewPager: {
42 * flex: 1
43 * },
44 * pageStyle: {
45 * alignItems: 'center',
46 * padding: 20,
47 * }
48 * }
49 * ```
50 */
51
52export class ViewPager extends React.Component {
53 constructor(...args) {
54 super(...args);
55
56 _defineProperty(this, "isScrolling", false);
57
58 _defineProperty(this, "viewPager", /*#__PURE__*/React.createRef());
59
60 _defineProperty(this, "getInnerViewNode", () => {
61 return this.viewPager.current.getInnerViewNode();
62 });
63
64 _defineProperty(this, "_onPageScroll", e => {
65 if (this.props.onPageScroll) {
66 this.props.onPageScroll(e);
67 } // Not implemented on iOS yet
68
69
70 if (Platform.OS === 'android') {
71 if (this.props.keyboardDismissMode === 'on-drag') {
72 Keyboard.dismiss();
73 }
74 }
75 });
76
77 _defineProperty(this, "_onPageScrollStateChanged", e => {
78 if (this.props.onPageScrollStateChanged) {
79 this.props.onPageScrollStateChanged(e);
80 }
81
82 this.isScrolling = e.nativeEvent.pageScrollState === 'dragging';
83 });
84
85 _defineProperty(this, "_onPageSelected", e => {
86 if (this.props.onPageSelected) {
87 this.props.onPageSelected(e);
88 }
89 });
90
91 _defineProperty(this, "setPage", selectedPage => {
92 UIManager.dispatchViewManagerCommand(ReactNative.findNodeHandle(this), getViewManagerConfig().Commands.setPage, [selectedPage]);
93 });
94
95 _defineProperty(this, "setPageWithoutAnimation", selectedPage => {
96 UIManager.dispatchViewManagerCommand(ReactNative.findNodeHandle(this), getViewManagerConfig().Commands.setPageWithoutAnimation, [selectedPage]);
97 });
98
99 _defineProperty(this, "setScrollEnabled", scrollEnabled => {
100 UIManager.dispatchViewManagerCommand(ReactNative.findNodeHandle(this), getViewManagerConfig().Commands.setScrollEnabled, [scrollEnabled]);
101 });
102
103 _defineProperty(this, "_onMoveShouldSetResponderCapture", () => {
104 return this.isScrolling;
105 });
106 }
107
108 componentDidMount() {
109 // On iOS we do it directly on the native side
110 if (Platform.OS === 'android') {
111 if (this.props.initialPage != null) {
112 this.setPageWithoutAnimation(this.props.initialPage);
113 }
114 }
115 }
116
117 render() {
118 return /*#__PURE__*/React.createElement(ViewpagerViewManager, _extends({}, this.props, {
119 ref: this.viewPager
120 /** TODO: Fix ref type */
121 ,
122 style: this.props.style,
123 onPageScroll: this._onPageScroll,
124 onPageScrollStateChanged: this._onPageScrollStateChanged,
125 onPageSelected: this._onPageSelected,
126 onMoveShouldSetResponderCapture: this._onMoveShouldSetResponderCapture,
127 children: childrenWithOverriddenStyle(this.props.children)
128 }));
129 }
130
131}
132//# sourceMappingURL=ViewPager.js.map
\No newline at end of file