UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

282 lines (243 loc) 11.2 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(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 = this && this.__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; }; import * as React from 'react'; import classNames from 'classnames'; import noop from '../_utils/noop'; import Gesture from '../gesture'; import { getPxStyle, getTransformPropValue, setPxStyle } from '../_utils/styleUtil'; var DefaultTabBar = /*#__PURE__*/function (_React$PureComponent) { _inherits(DefaultTabBar, _React$PureComponent); var _super = _createSuper(DefaultTabBar); function DefaultTabBar(props) { var _this; _classCallCheck(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); 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: 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: 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(_assertThisInitialized(_this)); _this.isTabBarVertical = _this.isTabBarVertical.bind(_assertThisInitialized(_this)); _this.getTabSize = _this.getTabSize.bind(_assertThisInitialized(_this)); _this.onClick = _this.onClick.bind(_assertThisInitialized(_this)); return _this; } _createClass(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 = classNames(prefixCls, "".concat(prefixCls, "-").concat(tabBarPosition), _defineProperty({}, " ".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({}, 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, _extends({}, 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); export { DefaultTabBar as default }; DefaultTabBar.defaultProps = { prefixCls: 'Yep-tabs-default-bar', onTabClick: noop, activeTab: 0, page: 5, animated: true, tabBarBackgroundColor: '#FFF' };