UNPKG

wix-style-react

Version:
281 lines (243 loc) • 10.7 kB
import _extends from "@babel/runtime/helpers/extends"; 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"; 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 { Animator } from 'wix-animations'; import PropTypes from 'prop-types'; import ChevronDown from 'wix-ui-icons-common/ChevronDown'; import ChevronUp from 'wix-ui-icons-common/ChevronUp'; import { WixStyleReactContext } from '../../WixStyleReactProvider/context'; import Text from '../../Text'; import Button from '../../Button'; import TextButton from '../../TextButton'; import { buttonTypes, SHOW_LABEL, dataHooks, transitionSpeedMap, skins } from '../constants'; import { st, classes } from './AccordionItem.st.css'; var AccordionItem = /*#__PURE__*/function (_React$PureComponent) { _inherits(AccordionItem, _React$PureComponent); var _super = _createSuper(AccordionItem); function AccordionItem() { var _this; _classCallCheck(this, AccordionItem); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "state", { hover: false }); _defineProperty(_assertThisInitialized(_this), "_onMouseLeave", function (e) { var _this$props = _this.props, disabled = _this$props.disabled, onMouseLeave = _this$props.onMouseLeave; _this.setState({ hover: false }); !disabled && onMouseLeave && onMouseLeave(e); }); _defineProperty(_assertThisInitialized(_this), "_onMouseEnter", function (e) { var _this$props2 = _this.props, disabled = _this$props2.disabled, onMouseEnter = _this$props2.onMouseEnter; _this.setState({ hover: true }); !disabled && onMouseEnter && onMouseEnter(e); }); _defineProperty(_assertThisInitialized(_this), "_renderOpenButton", function (show) { var _this$props3 = _this.props, expandLabel = _this$props3.expandLabel, buttonType = _this$props3.buttonType, skin = _this$props3.skin, disabled = _this$props3.disabled; var commonProps = { disabled: disabled, skin: skin === skins.neutral ? 'dark' : 'standard', dataHook: dataHooks.toggleButton }; var label; if (buttonType === buttonTypes.node) { label = show && expandLabel; } if (buttonType === buttonTypes.button && expandLabel) { label = show && /*#__PURE__*/React.createElement(Button, _extends({}, commonProps, { size: "small", children: expandLabel })); } if (buttonType === buttonTypes.textButton) { label = /*#__PURE__*/React.createElement(TextButton, _extends({}, commonProps, { suffixIcon: /*#__PURE__*/React.createElement(ChevronDown, { size: "24px" }), children: show && expandLabel })); } return label; }); _defineProperty(_assertThisInitialized(_this), "_renderCloseButton", function (show) { var _this$props4 = _this.props, collapseLabel = _this$props4.collapseLabel, buttonType = _this$props4.buttonType, skin = _this$props4.skin, disabled = _this$props4.disabled; var commonProps = { disabled: disabled, skin: skin === skins.neutral ? 'dark' : 'standard', dataHook: dataHooks.toggleButton }; var label; if (buttonType === buttonTypes.node) { label = show && collapseLabel; } if (buttonType === buttonTypes.button && collapseLabel) { label = show && /*#__PURE__*/React.createElement(Button, _extends({}, commonProps, { size: "small", priority: "secondary", children: collapseLabel })); } if (buttonType === buttonTypes.textButton) { label = /*#__PURE__*/React.createElement(TextButton, _extends({}, commonProps, { suffixIcon: /*#__PURE__*/React.createElement(ChevronUp, { size: "24px" }), children: show && collapseLabel })); } return label; }); _defineProperty(_assertThisInitialized(_this), "_renderButton", function () { var _this$props5 = _this.props, showLabel = _this$props5.showLabel, buttonType = _this$props5.buttonType, open = _this$props5.open; var hover = _this.state.hover; var showLabelValue = showLabel; // Only for buttonType='button', showLabel should be 'always' by default. if (!showLabelValue) { showLabelValue = buttonType === buttonTypes.button ? SHOW_LABEL.always : SHOW_LABEL.hover; } if (showLabelValue === SHOW_LABEL.always) { return open ? _this._renderCloseButton(true) : _this._renderOpenButton(true); } if (showLabelValue === SHOW_LABEL.hover) { return open ? _this._renderCloseButton(hover) : _this._renderOpenButton(hover); } }); return _this; } _createClass(AccordionItem, [{ key: "render", value: function render() { var _this2 = this; var _this$props6 = this.props, icon = _this$props6.icon, title = _this$props6.title, subtitle = _this$props6.subtitle, open = _this$props6.open, children = _this$props6.children, onToggle = _this$props6.onToggle, disabled = _this$props6.disabled, skin = _this$props6.skin, hideShadow = _this$props6.hideShadow, className = _this$props6.className, size = _this$props6.size, horizontalPadding = _this$props6.horizontalPadding, last = _this$props6.last, transitionSpeed = _this$props6.transitionSpeed, onAnimationEnter = _this$props6.onAnimationEnter, onAnimationExit = _this$props6.onAnimationExit; var hover = this.state.hover; return /*#__PURE__*/React.createElement(WixStyleReactContext.Consumer, null, function (_ref) { var reducedSpacingAndImprovedLayout = _ref.reducedSpacingAndImprovedLayout; return /*#__PURE__*/React.createElement("div", { className: st(classes.root, { disabled: disabled, hover: hover, open: open, skin: skin, hideShadow: hideShadow, size: size, horizontalPadding: horizontalPadding, last: last, reducedSpacingAndImprovedLayout: reducedSpacingAndImprovedLayout }, className) }, /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.item }, /*#__PURE__*/React.createElement("div", { onClick: !disabled ? onToggle : null, className: st(classes.header, { size: size }), "data-hook": "header", onMouseEnter: _this2._onMouseEnter, onMouseLeave: _this2._onMouseLeave }, icon && /*#__PURE__*/React.createElement("div", { className: classes.icon, "data-hook": "icon" }, icon), /*#__PURE__*/React.createElement("div", { className: classes.titleWrapper }, title && /*#__PURE__*/React.createElement("div", { "data-hook": "titleContainer" }, typeof title === 'string' ? /*#__PURE__*/React.createElement(Text, { dataHook: "title", ellipsis: true, weight: "normal" }, title) : title), subtitle && /*#__PURE__*/React.createElement("div", { "data-hook": dataHooks.subtitle }, typeof subtitle === 'string' ? /*#__PURE__*/React.createElement(Text, { size: "small" }, subtitle) : subtitle)), /*#__PURE__*/React.createElement("div", { className: classes.toggleButton, "data-hook": "toggle-accordion-wrapper", children: _this2._renderButton() })), /*#__PURE__*/React.createElement(Animator, { show: open, height: true, timing: transitionSpeedMap[transitionSpeed], onAnimationEnter: onAnimationEnter, onAnimationExit: onAnimationExit }, /*#__PURE__*/React.createElement("div", { "data-hook": "children", className: classes.children }, children)))); }); } }]); return AccordionItem; }(React.PureComponent); _defineProperty(AccordionItem, "displayName", 'AccordionItem'); _defineProperty(AccordionItem, "propTypes", { buttonType: PropTypes.oneOf(['textButton', 'button', 'node']), title: PropTypes.node, subtitle: PropTypes.node, expandLabel: PropTypes.node, collapseLabel: PropTypes.node, showLabel: PropTypes.oneOf(['hover', 'always']), children: PropTypes.node, icon: PropTypes.node, open: PropTypes.bool, initiallyOpen: PropTypes.bool, disabled: PropTypes.bool, onToggle: PropTypes.func, onMouseEnter: PropTypes.func, onMouseLeave: PropTypes.func, skin: PropTypes.oneOf(['standard', 'light', 'neutral']), hideShadow: PropTypes.bool, className: PropTypes.string, size: PropTypes.oneOf(['tiny', 'small', 'medium', 'large']), horizontalPadding: PropTypes.oneOf(['tiny', 'small', 'medium', 'large']), transitionSpeed: PropTypes.oneOf(['slow', 'medium', 'fast']), onAnimationEnter: PropTypes.func, onAnimationExit: PropTypes.func }); _defineProperty(AccordionItem, "defaultProps", { buttonType: 'textButton' }); export default AccordionItem;