UNPKG

wix-style-react

Version:
331 lines (285 loc) • 12.1 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 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 _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, { Component } from 'react'; import PropTypes from 'prop-types'; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import { ThemeProviderConsumerBackwardCompatible } from '../ThemeProvider/ThemeProviderConsumerBackwardCompatible'; import Image from '../Image'; import AddItemLarge from 'wix-ui-icons-common/system/AddItemLarge'; import AddItemMedium from 'wix-ui-icons-common/system/AddItemMedium'; import AddItemSmall from 'wix-ui-icons-common/system/AddItemSmall'; import Add from 'wix-ui-icons-common/Add'; import Tooltip from '../Tooltip'; import Text from '../Text'; import AddMedia from 'wix-ui-icons-common/system/AddMedia'; import { dataHooks } from './constants'; import { TooltipCommonProps } from '../common/PropTypes/TooltipCommon'; import { st, classes } from './AddItem.st.css'; import { isString } from '../utils/StringUtils'; var AddItemButtonIcons = { tiny: function tiny(_ref) { var className = _ref.className; return /*#__PURE__*/React.createElement(Add, { className: className, width: "26", height: "26" }); }, small: AddItemSmall, medium: AddItemMedium, large: AddItemLarge, image: function image(_ref2) { var className = _ref2.className; return /*#__PURE__*/React.createElement(AddMedia, { className: className, width: "31", height: "31" }); } }; var illustrationDimensions = { tiny: { height: 24, width: 24 }, small: { height: 60, width: 60 }, medium: { height: 120, width: 120 }, large: { height: 120, width: 120 } }; var tooltipPlacementByAlignment = { left: 'top-start', right: 'top-end' }; var AddItem = /*#__PURE__*/function (_Component) { _inherits(AddItem, _Component); var _super = _createSuper(AddItem); function AddItem(props) { var _this; _classCallCheck(this, AddItem); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "_renderDefaultIcon", function () { var _this$props = _this.props, size = _this$props.size, theme = _this$props.theme; var isImageIcon = theme === 'image'; return /*#__PURE__*/React.createElement(ThemeProviderConsumerBackwardCompatible, { defaultIcons: { AddItemButton: AddItemButtonIcons } }, function (_ref3) { var icons = _ref3.icons; var Icon = icons.AddItemButton[isImageIcon ? 'image' : size]; return /*#__PURE__*/React.createElement(Icon, { className: classes.icon }); }); }); _defineProperty(_assertThisInitialized(_this), "_renderIllustration", function () { var _this$props2 = _this.props, icon = _this$props2.icon, size = _this$props2.size; return /*#__PURE__*/React.createElement(Image, _extends({ className: classes.illustration, fit: "contain", src: icon }, illustrationDimensions[size])); }); _defineProperty(_assertThisInitialized(_this), "_renderIcon", function () { var icon = _this.props.icon; if (!icon) { return _this._renderDefaultIcon(); } return isString(icon) ? _this._renderIllustration() : icon; }); _defineProperty(_assertThisInitialized(_this), "_renderText", function () { var _this$props3 = _this.props, children = _this$props3.children, theme = _this$props3.theme, size = _this$props3.size; if (!children || theme === 'image') { return null; } var textSize = size === 'tiny' ? 'small' : 'medium'; return /*#__PURE__*/React.createElement("div", { className: st(classes.textWrapper, { size: size }) }, typeof children === 'function' ? children() : /*#__PURE__*/React.createElement(Text, { className: classes.textContent, weight: "thin", skin: "standard", size: textSize, dataHook: dataHooks.itemText, ellipsis: true }, children)); }); _defineProperty(_assertThisInitialized(_this), "_renderSubtitle", function () { var _this$props4 = _this.props, size = _this$props4.size, subtitle = _this$props4.subtitle; return subtitle && /*#__PURE__*/React.createElement(Text, { className: st(classes.subtitle, { size: size }), size: "small", dataHook: dataHooks.itemSubtitle }, subtitle); }); _defineProperty(_assertThisInitialized(_this), "_renderContent", function () { var _this$props5 = _this.props, theme = _this$props5.theme, alignItems = _this$props5.alignItems, size = _this$props5.size, disabled = _this$props5.disabled, showIcon = _this$props5.showIcon; return /*#__PURE__*/React.createElement("div", { className: st(classes.contentContainer, { theme: theme, size: size, alignItems: alignItems, disabled: disabled }) }, /*#__PURE__*/React.createElement("div", { className: st(classes.content, { size: size }) }, showIcon && _this._renderIcon(), _this._renderText()), _this._renderSubtitle()); }); _defineProperty(_assertThisInitialized(_this), "_getTooltipProps", function () { var _this$props6 = _this.props, _this$props6$tooltipP = _this$props6.tooltipProps, tooltipProps = _this$props6$tooltipP === void 0 ? {} : _this$props6$tooltipP, theme = _this$props6.theme, tooltipContent = _this$props6.tooltipContent, alignItems = _this$props6.alignItems; var content = tooltipProps.content || tooltipContent; var isImageTheme = theme === 'image'; return _objectSpread({ disabled: !content, content: content, flip: !isImageTheme, moveBy: { y: isImageTheme ? -12 : 0 }, placement: tooltipPlacementByAlignment[alignItems] || 'top' }, tooltipProps); }); _defineProperty(_assertThisInitialized(_this), "focus", function () { _this.button.current && _this.button.current.focus(); }); _defineProperty(_assertThisInitialized(_this), "blur", function () { _this.button.current && _this.button.current.blur(); }); _this.button = /*#__PURE__*/React.createRef(); return _this; } _createClass(AddItem, [{ key: "render", value: function render() { var _this$props7 = this.props, dataHook = _this$props7.dataHook, onClick = _this$props7.onClick, disabled = _this$props7.disabled, theme = _this$props7.theme, focusableOnFocus = _this$props7.focusableOnFocus, focusableOnBlur = _this$props7.focusableOnBlur, removePadding = _this$props7.removePadding, borderRadius = _this$props7.borderRadius, className = _this$props7.className, ariaLabel = _this$props7.ariaLabel, ariaLabelledBy = _this$props7.ariaLabelledBy, size = _this$props7.size, alignItems = _this$props7.alignItems; return /*#__PURE__*/React.createElement(Tooltip, _extends({ className: classes.tooltip, dataHook: dataHook }, this._getTooltipProps()), /*#__PURE__*/React.createElement("button", { className: st(classes.root, { theme: theme, size: size, removePadding: removePadding, borderRadius: borderRadius, disabled: disabled, alignItems: alignItems }, className), ref: this.button, style: borderRadius && { borderRadius: borderRadius }, disabled: disabled, "data-hook": dataHooks.addItem, type: "button", onClick: onClick, onFocus: focusableOnFocus, onBlur: focusableOnBlur, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy }, this._renderContent())); } }]); return AddItem; }(Component); _defineProperty(AddItem, "displayName", 'AddItem'); _defineProperty(AddItem, "propTypes", { /** any renderable node or a render function. In case of a render function, text styles will not be applied. */ children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), /** apply disabled styles */ disabled: PropTypes.bool, /** the theme of component */ theme: PropTypes.oneOf(['dashes', 'plain', 'filled', 'image']), /** switching content alignment */ alignItems: PropTypes.oneOf(['center', 'right', 'left']), /** size to control icon and spacing */ size: PropTypes.oneOf(['large', 'medium', 'small', 'tiny']), /** click event handler */ onClick: PropTypes.func, /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** A css class to be applied to the component's root element */ className: PropTypes.string, /** When provided, hover will display a tooltip */ tooltipContent: PropTypes.node, /** Tooltip props */ tooltipProps: PropTypes.shape(TooltipCommonProps), /** Displays the plus icon */ showIcon: PropTypes.bool, /** Removes padding */ removePadding: PropTypes.bool, /** sets the border-radius css property on the button element */ borderRadius: PropTypes.string, /** Defines a string value that labels the add item element */ ariaLabel: PropTypes.string, /** Identifies the element that labels the add item element */ ariaLabelledby: PropTypes.string, /** Subtitle of the component */ subtitle: PropTypes.node, /** The illustraion icon src or node */ icon: PropTypes.node }); _defineProperty(AddItem, "defaultProps", { theme: 'dashes', size: 'tiny', alignItems: 'center', showIcon: true, removePadding: false }); export default withFocusable(AddItem);