UNPKG

wix-style-react

Version:
158 lines (139 loc) 6.14 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; 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; } } import React from 'react'; import PropTypes from 'prop-types'; import Text from '../Text'; import { dataHooks } from './constants'; import Tab from './Tab'; import { st, classes, vars } from './CardFolderTabs.st.css'; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable"; var tabButton = function tabButton(_ref) { var className = _ref.className, id = _ref.id, name = _ref.name, disabled = _ref.disabled, selected = _ref.selected, maxTabWidth = _ref.maxTabWidth, onTabChange = _ref.onTabChange, focusableOnFocus = _ref.focusableOnFocus, focusableOnBlur = _ref.focusableOnBlur, fluid = _ref.fluid, ellipsis = _ref.ellipsis, size = _ref.size, beforeSelected = _ref.beforeSelected, afterSelected = _ref.afterSelected; return /*#__PURE__*/React.createElement("button", { "data-hook": dataHooks.tabButton(id), className: st(classes.button, { selected: selected, disabled: disabled, fluid: fluid, beforeSelected: beforeSelected, afterSelected: afterSelected }, className), style: fluid ? {} : _defineProperty({}, vars['maxWidth'], maxTabWidth), onClick: function onClick() { return onTabChange(id); }, disabled: disabled, key: id, onFocus: focusableOnFocus, onBlur: focusableOnBlur }, /*#__PURE__*/React.createElement(Text, { dataHook: dataHooks.tabText(id), ellipsis: ellipsis, className: classes.buttonText, size: size }, name)); }; var TabButton = withFocusable(tabButton); var CardFolderTabs = /*#__PURE__*/function (_React$PureComponent) { _inherits(CardFolderTabs, _React$PureComponent); var _super = _createSuper(CardFolderTabs); function CardFolderTabs() { _classCallCheck(this, CardFolderTabs); return _super.apply(this, arguments); } _createClass(CardFolderTabs, [{ key: "render", value: function render() { var _this$props = this.props, dataHook = _this$props.dataHook, className = _this$props.className, children = _this$props.children, activeId = _this$props.activeId, maxTabWidth = _this$props.maxTabWidth, onTabChange = _this$props.onTabChange, fluid = _this$props.fluid, ellipsis = _this$props.ellipsis, size = _this$props.size; var cardFolderTabs = Array.isArray(children) ? children : [children]; var selectedTabIndex = cardFolderTabs.findIndex(function (tab) { return activeId === tab.props.id; }); return /*#__PURE__*/React.createElement("div", { className: st(classes.root, { fluid: fluid }, className), "data-hook": dataHook }, /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.header, className: classes.headerWrapper }, cardFolderTabs.map(function (_ref3, index) { var props = _ref3.props; return /*#__PURE__*/React.createElement(TabButton, _extends({ key: props.id }, props, { selected: activeId === props.id, beforeSelected: index === selectedTabIndex - 1, afterSelected: index === selectedTabIndex + 1, maxTabWidth: maxTabWidth, fluid: fluid, ellipsis: ellipsis, size: size, onTabChange: typeof onTabChange === 'function' ? onTabChange : undefined })); })), /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.content, className: classes.tabContentWrapper }, cardFolderTabs.filter(function (tab) { return activeId === tab.props.id; }))); } }]); return CardFolderTabs; }(React.PureComponent); CardFolderTabs.displayName = 'CardFolderTabs'; CardFolderTabs.propTypes = { /** Applies a data-hook HTML attribute that can be used in the tests. */ dataHook: PropTypes.string, /** Specifies a CSS class name to be appended to the component’s root element. */ className: PropTypes.string, /** Specifies an id of a currently visible tab. */ activeId: PropTypes.string.isRequired, /** Defines a callback function which is called on tab change. An id of a newly selected tab is passed as a parameter. */ onTabChange: PropTypes.func, /** Defines a maximum width of a tab button. */ maxTabWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), /** Stretches tabs to fill a 100% of a parent container width */ fluid: PropTypes.bool, /** Specifies whether text that exceeds tab width is truncated with ellipsis. If false, text will wrap to another line. */ ellipsis: PropTypes.bool, /** Controls tab label font size */ size: PropTypes.oneOf(['medium', 'small']) }; CardFolderTabs.defaultProps = { maxTabWidth: '138px', ellipsis: true, size: 'medium', fluid: false }; CardFolderTabs.Tab = Tab; export default CardFolderTabs;