230 lines
9.2 KiB
JavaScript
Executable File
230 lines
9.2 KiB
JavaScript
Executable File
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
|
|
function _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); }
|
|
|
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
|
|
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
|
|
function _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; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
|
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
|
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
|
|
|
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
|
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
|
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
|
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
|
|
import React from 'react';
|
|
import Hammer from 'rc-hammerjs';
|
|
import ReactDOM from 'react-dom';
|
|
import TabContent from './TabContent';
|
|
import { isVertical, getActiveIndex, getTransformByIndex, setTransform, getActiveKey, toArray, setTransition } from './utils';
|
|
var RESISTANCE_COEF = 0.6;
|
|
|
|
function computeIndex(_ref) {
|
|
var maxIndex = _ref.maxIndex,
|
|
startIndex = _ref.startIndex,
|
|
delta = _ref.delta,
|
|
viewSize = _ref.viewSize;
|
|
var index = startIndex + -delta / viewSize;
|
|
|
|
if (index < 0) {
|
|
index = Math.exp(index * RESISTANCE_COEF) - 1;
|
|
} else if (index > maxIndex) {
|
|
index = maxIndex + 1 - Math.exp((maxIndex - index) * RESISTANCE_COEF);
|
|
}
|
|
|
|
return index;
|
|
}
|
|
|
|
function getIndexByDelta(e) {
|
|
var delta = isVertical(this.props.tabBarPosition) ? e.deltaY : e.deltaX;
|
|
var otherDelta = isVertical(this.props.tabBarPosition) ? e.deltaX : e.deltaY;
|
|
|
|
if (Math.abs(delta) < Math.abs(otherDelta)) {
|
|
return undefined;
|
|
}
|
|
|
|
var currentIndex = computeIndex({
|
|
maxIndex: this.maxIndex,
|
|
viewSize: this.viewSize,
|
|
startIndex: this.startIndex,
|
|
delta: delta
|
|
});
|
|
var showIndex = delta < 0 ? Math.floor(currentIndex + 1) : Math.floor(currentIndex);
|
|
|
|
if (showIndex < 0) {
|
|
showIndex = 0;
|
|
} else if (showIndex > this.maxIndex) {
|
|
showIndex = this.maxIndex;
|
|
}
|
|
|
|
if (this.children[showIndex].props.disabled) {
|
|
return undefined;
|
|
}
|
|
|
|
return currentIndex;
|
|
}
|
|
|
|
var SwipeableTabContent = /*#__PURE__*/function (_React$Component) {
|
|
_inherits(SwipeableTabContent, _React$Component);
|
|
|
|
function SwipeableTabContent() {
|
|
var _getPrototypeOf2;
|
|
|
|
var _this;
|
|
|
|
_classCallCheck(this, SwipeableTabContent);
|
|
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
_this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(SwipeableTabContent)).call.apply(_getPrototypeOf2, [this].concat(args)));
|
|
|
|
_this.onPanStart = function () {
|
|
var _this$props = _this.props,
|
|
tabBarPosition = _this$props.tabBarPosition,
|
|
children = _this$props.children,
|
|
activeKey = _this$props.activeKey,
|
|
animated = _this$props.animated;
|
|
_this.startIndex = getActiveIndex(children, activeKey);
|
|
|
|
var _assertThisInitialize = _assertThisInitialized(_this),
|
|
startIndex = _assertThisInitialize.startIndex;
|
|
|
|
if (startIndex === -1) {
|
|
return;
|
|
}
|
|
|
|
if (animated) {
|
|
setTransition(_this.rootNode.style, 'none');
|
|
}
|
|
|
|
_this.startDrag = true;
|
|
_this.children = toArray(children);
|
|
_this.maxIndex = _this.children.length - 1;
|
|
_this.viewSize = isVertical(tabBarPosition) ? _this.rootNode.offsetHeight : _this.rootNode.offsetWidth;
|
|
};
|
|
|
|
_this.onPan = function (e) {
|
|
if (!_this.startDrag) {
|
|
return;
|
|
}
|
|
|
|
var tabBarPosition = _this.props.tabBarPosition;
|
|
var currentIndex = getIndexByDelta.call(_assertThisInitialized(_this), e);
|
|
|
|
if (currentIndex !== undefined) {
|
|
setTransform(_this.rootNode.style, getTransformByIndex(currentIndex, tabBarPosition));
|
|
}
|
|
};
|
|
|
|
_this.onPanEnd = function (e) {
|
|
if (!_this.startDrag) {
|
|
return;
|
|
}
|
|
|
|
_this.end(e);
|
|
};
|
|
|
|
_this.onSwipe = function (e) {
|
|
_this.end(e, true);
|
|
};
|
|
|
|
_this.end = function (e, swipe) {
|
|
var _this$props2 = _this.props,
|
|
tabBarPosition = _this$props2.tabBarPosition,
|
|
animated = _this$props2.animated;
|
|
_this.startDrag = false;
|
|
|
|
if (animated) {
|
|
setTransition(_this.rootNode.style, '');
|
|
}
|
|
|
|
var currentIndex = getIndexByDelta.call(_assertThisInitialized(_this), e);
|
|
var finalIndex = _this.startIndex;
|
|
|
|
if (currentIndex !== undefined) {
|
|
if (currentIndex < 0) {
|
|
finalIndex = 0;
|
|
} else if (currentIndex > _this.maxIndex) {
|
|
finalIndex = _this.maxIndex;
|
|
} else if (swipe) {
|
|
var delta = isVertical(tabBarPosition) ? e.deltaY : e.deltaX;
|
|
finalIndex = delta < 0 ? Math.ceil(currentIndex) : Math.floor(currentIndex);
|
|
} else {
|
|
var floorIndex = Math.floor(currentIndex);
|
|
|
|
if (currentIndex - floorIndex > 0.6) {
|
|
finalIndex = floorIndex + 1;
|
|
} else {
|
|
finalIndex = floorIndex;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (_this.children[finalIndex].props.disabled) {
|
|
return;
|
|
}
|
|
|
|
if (_this.startIndex === finalIndex) {
|
|
if (animated) {
|
|
setTransform(_this.rootNode.style, getTransformByIndex(finalIndex, _this.props.tabBarPosition));
|
|
}
|
|
} else {
|
|
_this.props.onChange(getActiveKey(_this.props.children, finalIndex));
|
|
}
|
|
};
|
|
|
|
return _this;
|
|
}
|
|
|
|
_createClass(SwipeableTabContent, [{
|
|
key: "componentDidMount",
|
|
value: function componentDidMount() {
|
|
this.rootNode = ReactDOM.findDOMNode(this);
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var _this$props3 = this.props,
|
|
tabBarPosition = _this$props3.tabBarPosition,
|
|
hammerOptions = _this$props3.hammerOptions,
|
|
animated = _this$props3.animated;
|
|
var events = {
|
|
onSwipe: this.onSwipe,
|
|
onPanStart: this.onPanStart
|
|
};
|
|
|
|
if (animated !== false) {
|
|
events = _objectSpread({}, events, {
|
|
onPan: this.onPan,
|
|
onPanEnd: this.onPanEnd
|
|
});
|
|
}
|
|
|
|
return React.createElement(Hammer, _extends({}, events, {
|
|
direction: isVertical(tabBarPosition) ? 'DIRECTION_ALL' : 'DIRECTION_HORIZONTAL',
|
|
options: hammerOptions
|
|
}), React.createElement(TabContent, this.props));
|
|
}
|
|
}]);
|
|
|
|
return SwipeableTabContent;
|
|
}(React.Component);
|
|
|
|
export { SwipeableTabContent as default };
|
|
SwipeableTabContent.defaultProps = {
|
|
animated: true
|
|
}; |