UNPKG

6.96 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; /**
8 * This source code is quoted from rc-tabs.
9 * homepage: https://github.com/react-component/tabs
10 */
11
12
13var _TabContent = require('./TabContent');
14
15var _TabContent2 = _interopRequireDefault(_TabContent);
16
17var _react = require('react');
18
19var _react2 = _interopRequireDefault(_react);
20
21var _reactHammerjs = require('react-hammerjs');
22
23var _reactHammerjs2 = _interopRequireDefault(_reactHammerjs);
24
25var _reactDom = require('react-dom');
26
27var _reactDom2 = _interopRequireDefault(_reactDom);
28
29var _utils = require('./utils');
30
31var _propTypes = require('prop-types');
32
33var _propTypes2 = _interopRequireDefault(_propTypes);
34
35var _createReactClass = require('create-react-class');
36
37var _createReactClass2 = _interopRequireDefault(_createReactClass);
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
40
41var RESISTANCE_COEF = 0.6;
42
43function computeIndex(_ref) {
44 var maxIndex = _ref.maxIndex,
45 startIndex = _ref.startIndex,
46 delta = _ref.delta,
47 viewSize = _ref.viewSize;
48
49 var index = startIndex + -delta / viewSize;
50 if (index < 0) {
51 index = Math.exp(index * RESISTANCE_COEF) - 1;
52 } else if (index > maxIndex) {
53 index = maxIndex + 1 - Math.exp((maxIndex - index) * RESISTANCE_COEF);
54 }
55 return index;
56}
57
58function getIndexByDelta(e) {
59 var delta = (0, _utils.isVertical)(this.props.tabBarPosition) ? e.deltaY : e.deltaX;
60 var currentIndex = computeIndex({
61 maxIndex: this.maxIndex,
62 viewSize: this.viewSize,
63 startIndex: this.startIndex,
64 delta: delta
65 });
66 var showIndex = delta < 0 ? Math.floor(currentIndex + 1) : Math.floor(currentIndex);
67 if (showIndex < 0) {
68 showIndex = 0;
69 } else if (showIndex > this.maxIndex) {
70 showIndex = this.maxIndex;
71 }
72 if (this.children[showIndex].props.disabled) {
73 return undefined;
74 }
75 return currentIndex;
76}
77
78var SwipeableTabContent = (0, _createReactClass2["default"])({
79 propTypes: {
80 tabBarPosition: _propTypes2["default"].string,
81 onChange: _propTypes2["default"].func,
82 children: _propTypes2["default"].any,
83 hammerOptions: _propTypes2["default"].any,
84 animated: _propTypes2["default"].bool,
85 activeKey: _propTypes2["default"].string
86 },
87
88 getDefaultProps: function getDefaultProps() {
89 return {
90 animated: true
91 };
92 },
93 componentDidMount: function componentDidMount() {
94 this.rootNode = _reactDom2["default"].findDOMNode(this);
95 },
96 onPanStart: function onPanStart() {
97 var _props = this.props,
98 tabBarPosition = _props.tabBarPosition,
99 children = _props.children,
100 activeKey = _props.activeKey,
101 animated = _props.animated;
102
103 var startIndex = this.startIndex = (0, _utils.getActiveIndex)(children, activeKey);
104 if (startIndex === -1) {
105 return;
106 }
107 if (animated) {
108 (0, _utils.setTransition)(this.rootNode.style, 'none');
109 }
110 this.startDrag = true;
111 this.children = (0, _utils.toArray)(children);
112 this.maxIndex = this.children.length - 1;
113 this.viewSize = (0, _utils.isVertical)(tabBarPosition) ? this.rootNode.offsetHeight : this.rootNode.offsetWidth;
114 },
115 onPan: function onPan(e) {
116 if (!this.startDrag) {
117 return;
118 }
119 var tabBarPosition = this.props.tabBarPosition;
120
121 var currentIndex = getIndexByDelta.call(this, e);
122 if (currentIndex !== undefined) {
123 (0, _utils.setTransform)(this.rootNode.style, (0, _utils.getTransformByIndex)(currentIndex, tabBarPosition));
124 }
125 },
126 onPanEnd: function onPanEnd(e) {
127 if (!this.startDrag) {
128 return;
129 }
130 this.end(e);
131 },
132 onSwipe: function onSwipe(e) {
133 this.end(e, true);
134 },
135 end: function end(e, swipe) {
136 var _props2 = this.props,
137 tabBarPosition = _props2.tabBarPosition,
138 animated = _props2.animated;
139
140 this.startDrag = false;
141 if (animated) {
142 (0, _utils.setTransition)(this.rootNode.style, '');
143 }
144 var currentIndex = getIndexByDelta.call(this, e);
145 var finalIndex = this.startIndex;
146 if (currentIndex !== undefined) {
147 if (currentIndex < 0) {
148 finalIndex = 0;
149 } else if (currentIndex > this.maxIndex) {
150 finalIndex = this.maxIndex;
151 } else if (swipe) {
152 var delta = (0, _utils.isVertical)(tabBarPosition) ? e.deltaY : e.deltaX;
153 finalIndex = delta < 0 ? Math.ceil(currentIndex) : Math.floor(currentIndex);
154 } else {
155 var floorIndex = Math.floor(currentIndex);
156 if (currentIndex - floorIndex > 0.6) {
157 finalIndex = floorIndex + 1;
158 } else {
159 finalIndex = floorIndex;
160 }
161 }
162 }
163 if (this.children[finalIndex].props.disabled) {
164 return;
165 }
166 if (this.startIndex === finalIndex) {
167 if (animated) {
168 (0, _utils.setTransform)(this.rootNode.style, (0, _utils.getTransformByIndex)(finalIndex, this.props.tabBarPosition));
169 }
170 } else {
171 this.props.onChange((0, _utils.getActiveKey)(this.props.children, finalIndex));
172 }
173 },
174 render: function render() {
175 var _props3 = this.props,
176 tabBarPosition = _props3.tabBarPosition,
177 hammerOptions = _props3.hammerOptions,
178 animated = _props3.animated;
179
180 var direction = {};
181 if ((0, _utils.isVertical)(tabBarPosition)) {
182 direction = {
183 vertical: true
184 };
185 }
186 var events = {
187 onSwipe: this.onSwipe,
188 onPanStart: this.onPanStart
189 };
190 if (animated !== false) {
191 events = _extends({}, events, {
192 onPan: this.onPan,
193 onPanEnd: this.onPanEnd
194 });
195 }
196 return _react2["default"].createElement(
197 _reactHammerjs2["default"],
198 _extends({}, events, direction, {
199 options: hammerOptions
200 }),
201 _react2["default"].createElement(_TabContent2["default"], this.props)
202 );
203 }
204});
205
206exports["default"] = SwipeableTabContent;
207module.exports = exports['default'];
\No newline at end of file