UNPKG

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