@jdcfe/yep-react
Version:
一套移动端的React组件库
306 lines (252 loc) • 13.2 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _noop = _interopRequireDefault(require("../_utils/noop"));
var _gesture = _interopRequireDefault(require("../gesture"));
var _styleUtil = require("../_utils/styleUtil");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var DefaultTabBar = /*#__PURE__*/function (_React$PureComponent) {
(0, _inherits2.default)(DefaultTabBar, _React$PureComponent);
var _super = _createSuper(DefaultTabBar);
function DefaultTabBar(props) {
var _this;
(0, _classCallCheck2.default)(this, DefaultTabBar);
_this = _super.call(this, props);
_this.onPan = function () {
var lastOffset = 0;
var finalOffset = 0;
var getLastOffset = function getLastOffset() {
var isVertical = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.isTabBarVertical();
var offset = +"".concat(lastOffset).replace('%', '');
if ("".concat(lastOffset).indexOf('%') >= 0) {
offset /= 100;
offset *= isVertical ? _this.layout.clientHeight : _this.layout.clientWidth;
}
return offset;
};
return {
onPanStart: function onPanStart() {
_this.setState({
isMoving: true
});
},
onPanMove: function onPanMove(status) {
if (!status.moveStatus || !_this.layout) return;
var isVertical = _this.isTabBarVertical();
var offset = getLastOffset() + (isVertical ? status.moveStatus.y : status.moveStatus.x);
var canScrollOffset = isVertical ? -_this.layout.scrollHeight + _this.layout.clientHeight : -_this.layout.scrollWidth + _this.layout.clientWidth;
offset = Math.min(offset, 0);
offset = Math.max(offset, canScrollOffset);
(0, _styleUtil.setPxStyle)(_this.layout, offset, 'px', isVertical);
finalOffset = offset;
_this.setState({
showPrev: -offset > 0,
showNext: offset > canScrollOffset
});
},
onPanEnd: function onPanEnd() {
var isVertical = _this.isTabBarVertical();
lastOffset = finalOffset;
_this.setState({
isMoving: false,
transform: (0, _styleUtil.getPxStyle)(finalOffset, 'px', isVertical)
});
},
setCurrentOffset: function setCurrentOffset(offset) {
return lastOffset = offset;
}
};
}();
_this.getTransformByIndex = function (props) {
var activeTab = props.activeTab,
tabs = props.tabs,
_props$page = props.page,
page = _props$page === void 0 ? 0 : _props$page;
var isVertical = _this.isTabBarVertical();
var size = _this.getTabSize(page, tabs.length);
var center = page / 2;
var pos = Math.min(activeTab, tabs.length - center - 0.5);
var skipSize = Math.min(-(pos - center + 0.5) * size, 0);
_this.onPan.setCurrentOffset("".concat(skipSize, "%"));
return {
transform: (0, _styleUtil.getPxStyle)(skipSize, '%', isVertical),
showPrev: activeTab > center - 0.5 && tabs.length > page,
showNext: activeTab < tabs.length - center - 0.5 && tabs.length > page
};
};
_this.onClick = function (index) {
var _this$props = _this.props,
goToTab = _this$props.goToTab,
onTabClick = _this$props.onTabClick,
tabs = _this$props.tabs;
onTabClick && onTabClick(tabs[index], index);
goToTab && goToTab(index);
};
_this.renderTab = function (t, i, size, isTabBarVertical) {
var _this$props2 = _this.props,
prefixCls = _this$props2.prefixCls,
renderTab = _this$props2.renderTab,
activeTab = _this$props2.activeTab,
tabBarTextStyle = _this$props2.tabBarTextStyle,
tabBarActiveTextColor = _this$props2.tabBarActiveTextColor,
tabBarInactiveTextColor = _this$props2.tabBarInactiveTextColor;
var textStyle = Object.assign({}, tabBarTextStyle);
var cls = "".concat(prefixCls, "-tab");
if (activeTab === i) {
cls += " ".concat(cls, "-active");
if (tabBarActiveTextColor) {
textStyle.color = tabBarActiveTextColor;
}
} else if (tabBarInactiveTextColor) {
textStyle.color = tabBarInactiveTextColor;
}
return /*#__PURE__*/React.createElement("div", {
key: "t_".concat(i),
style: Object.assign(Object.assign({}, textStyle), isTabBarVertical ? {
height: "".concat(size, "%")
} : {
width: "".concat(size, "%")
}),
className: cls,
onClick: function onClick() {
return _this.onClick(i);
}
}, renderTab ? renderTab(t) : t.title);
};
_this.state = Object.assign({
transform: '',
isMoving: false,
showPrev: false,
showNext: false
}, _this.getTransformByIndex(props));
_this.renderTab = _this.renderTab.bind((0, _assertThisInitialized2.default)(_this));
_this.isTabBarVertical = _this.isTabBarVertical.bind((0, _assertThisInitialized2.default)(_this));
_this.getTabSize = _this.getTabSize.bind((0, _assertThisInitialized2.default)(_this));
_this.onClick = _this.onClick.bind((0, _assertThisInitialized2.default)(_this));
return _this;
}
(0, _createClass2.default)(DefaultTabBar, [{
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (this.props.activeTab !== nextProps.activeTab || this.props.tabs !== nextProps.tabs || this.props.tabs.length !== nextProps.tabs.length) {
this.setState(Object.assign({}, this.getTransformByIndex(nextProps)));
}
}
}, {
key: "isTabBarVertical",
value: function isTabBarVertical() {
var position = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.props.tabBarPosition;
return position === 'left' || position === 'right';
}
}, {
key: "getTabSize",
value: function getTabSize(page, tabLength) {
return 100 / Math.min(page, tabLength);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props3 = this.props,
prefixCls = _this$props3.prefixCls,
animated = _this$props3.animated,
_this$props3$tabs = _this$props3.tabs,
tabs = _this$props3$tabs === void 0 ? [] : _this$props3$tabs,
_this$props3$page = _this$props3.page,
page = _this$props3$page === void 0 ? 0 : _this$props3$page,
_this$props3$activeTa = _this$props3.activeTab,
activeTab = _this$props3$activeTa === void 0 ? 0 : _this$props3$activeTa,
tabBarBackgroundColor = _this$props3.tabBarBackgroundColor,
tabBarUnderlineStyle = _this$props3.tabBarUnderlineStyle,
tabBarPosition = _this$props3.tabBarPosition,
renderUnderline = _this$props3.renderUnderline,
style = _this$props3.style;
var _this$state = this.state,
isMoving = _this$state.isMoving,
transform = _this$state.transform,
showNext = _this$state.showNext,
showPrev = _this$state.showPrev;
var cls = (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(tabBarPosition), (0, _defineProperty2.default)({}, " ".concat(prefixCls, "-animated"), animated && !isMoving));
var isTabBarVertical = this.isTabBarVertical();
var needScroll = tabs.length > page;
var size = this.getTabSize(page, tabs.length);
var Tabs = tabs.map(function (t, i) {
return _this2.renderTab(t, i, size, isTabBarVertical);
});
var styles = Object.assign(Object.assign({}, style), {
backgroundColor: tabBarBackgroundColor || ''
});
var transformStyle = needScroll ? Object.assign({}, (0, _styleUtil.getTransformPropValue)(transform)) : {};
var _a = this.onPan,
setCurrentOffset = _a.setCurrentOffset,
onPan = __rest(_a, ["setCurrentOffset"]);
var underlineProps = {
style: Object.assign(Object.assign(Object.assign({}, isTabBarVertical ? {
height: "".concat(size, "%")
} : {
width: "".concat(size, "%")
}), isTabBarVertical ? {
top: "".concat(size * activeTab, "%")
} : {
left: "".concat(size * activeTab, "%")
}), tabBarUnderlineStyle),
className: "".concat(prefixCls, "-underline")
};
return /*#__PURE__*/React.createElement("div", {
className: cls,
style: styles
}, showPrev && /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-prevpage")
}), /*#__PURE__*/React.createElement(_gesture.default, (0, _extends2.default)({}, onPan, {
direction: isTabBarVertical ? 'vertical' : 'horizontal'
}), /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-content"),
ref: function ref(div) {
return _this2.layout = div;
},
style: transformStyle
}, Tabs, renderUnderline ? renderUnderline(underlineProps) : /*#__PURE__*/React.createElement("div", underlineProps, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-underline-bar")
})))), showNext && /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-nextpage")
}));
}
}]);
return DefaultTabBar;
}(React.PureComponent);
exports.default = DefaultTabBar;
DefaultTabBar.defaultProps = {
prefixCls: 'Yep-tabs-default-bar',
onTabClick: _noop.default,
activeTab: 0,
page: 5,
animated: true,
tabBarBackgroundColor: '#FFF'
};
;