UNPKG

wix-style-react

Version:
225 lines (193 loc) • 9.83 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; 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"; var _excluded = ["value", "valueInShort", "description", "descriptionInfo", "percentage", "invertedPercentage", "onClick", "children", "focusableOnFocus", "focusableOnBlur", "className", "size", "alignItems", "isLoading"]; 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 from 'react'; import { withFocusable } from "wix-ui-core/dist/es/src/hocs/Focusable/FocusableHOC"; import InfoCircleSmall from 'wix-ui-icons-common/InfoCircleSmall'; import { WixStyleReactContext } from '../../WixStyleReactProvider/context'; import Heading from '../../Heading'; import Text from '../../Text'; import Tooltip from '../../Tooltip'; import Loader from '../../Loader'; import Box from '../../Box'; import TrendIndicator from '../../TrendIndicator'; import AdaptiveHeading from '../../utils/AdaptiveHeading'; import DataHooks from '../dataHooks'; import { st, classes } from './StatisticsItem.st.css'; import { SIZES } from '../constants'; var StatisticsItem = /*#__PURE__*/function (_React$PureComponent) { _inherits(StatisticsItem, _React$PureComponent); var _super = _createSuper(StatisticsItem); function StatisticsItem() { var _this; _classCallCheck(this, StatisticsItem); 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), "_getFocusableProps", function () { var _this$props = _this.props, onClick = _this$props.onClick, focusableOnFocus = _this$props.focusableOnFocus, focusableOnBlur = _this$props.focusableOnBlur; return onClick ? { onFocus: focusableOnFocus, onBlur: focusableOnBlur, tabIndex: 0 } : {}; }); _defineProperty(_assertThisInitialized(_this), "_getSpaceOrEnterHandler", function (handler) { return function (event) { var key = event.key; var isEnter = key === 'Enter'; var isSpace = key === ' '; if (isEnter || isSpace) { handler(event); event.preventDefault(); } }; }); _defineProperty(_assertThisInitialized(_this), "_renderValue", function (_ref) { var value = _ref.value, valueInShort = _ref.valueInShort, size = _ref.size, reducedSpacingAndImprovedLayout = _ref.reducedSpacingAndImprovedLayout, isLoading = _ref.isLoading; if (isLoading) { var margin = size === SIZES.tiny ? '3px' : '6px'; return /*#__PURE__*/React.createElement(Box, { align: "center", marginTop: margin, marginBottom: margin }, /*#__PURE__*/React.createElement(Loader, { dataHook: DataHooks.loader, size: "tiny" })); } var appearance = size === SIZES.tiny ? 'tiny' : reducedSpacingAndImprovedLayout ? 'H2' : 'H1'; return /*#__PURE__*/React.createElement(AdaptiveHeading, { text: value || '-', appearance: appearance, textInShort: valueInShort, dataHook: DataHooks.value }); }); _defineProperty(_assertThisInitialized(_this), "_renderDescription", function (_ref2) { var description = _ref2.description, descriptionInfo = _ref2.descriptionInfo, size = _ref2.size, alignItems = _ref2.alignItems, reducedSpacingAndImprovedLayout = _ref2.reducedSpacingAndImprovedLayout; if (!description) { return null; } return /*#__PURE__*/React.createElement("div", { className: st(classes.description, { alignItems: alignItems }) }, size === SIZES.tiny || reducedSpacingAndImprovedLayout ? /*#__PURE__*/React.createElement(Text, { ellipsis: true, size: "small", dataHook: DataHooks.description, secondary: true }, description) : /*#__PURE__*/React.createElement(Heading, { ellipsis: true, dataHook: DataHooks.description, appearance: "H5" }, description), descriptionInfo && /*#__PURE__*/React.createElement(Tooltip, { textAlign: "start", className: classes.tooltip, dataHook: DataHooks.tooltip, content: descriptionInfo }, /*#__PURE__*/React.createElement(InfoCircleSmall, { className: classes.info, "data-hook": DataHooks.info }))); }); _defineProperty(_assertThisInitialized(_this), "_renderPercents", function (_ref3) { var percentage = _ref3.percentage, invertedPercentage = _ref3.invertedPercentage; if (percentage == null) { return null; } return /*#__PURE__*/React.createElement(TrendIndicator, { className: classes.percentage, dataHook: DataHooks.percentage, value: percentage, inverted: invertedPercentage }); }); return _this; } _createClass(StatisticsItem, [{ key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, value = _this$props2.value, valueInShort = _this$props2.valueInShort, description = _this$props2.description, descriptionInfo = _this$props2.descriptionInfo, percentage = _this$props2.percentage, invertedPercentage = _this$props2.invertedPercentage, onClick = _this$props2.onClick, children = _this$props2.children, focusableOnFocus = _this$props2.focusableOnFocus, focusableOnBlur = _this$props2.focusableOnBlur, className = _this$props2.className, size = _this$props2.size, alignItems = _this$props2.alignItems, isLoading = _this$props2.isLoading, rest = _objectWithoutProperties(_this$props2, _excluded); var attrs = _objectSpread(_objectSpread({}, this._getFocusableProps()), {}, { 'data-hook': DataHooks.stat, onKeyDown: onClick ? this._getSpaceOrEnterHandler(onClick) : undefined, onClick: onClick }, rest); return /*#__PURE__*/React.createElement(WixStyleReactContext.Consumer, null, function (_ref4) { var reducedSpacingAndImprovedLayout = _ref4.reducedSpacingAndImprovedLayout; var defaultSize = reducedSpacingAndImprovedLayout ? SIZES.tiny : SIZES.large; return /*#__PURE__*/React.createElement("div", _extends({}, attrs, { className: st(classes.item, { clickable: !!onClick, size: size || defaultSize, alignItems: alignItems, reducedSpacingAndImprovedLayout: reducedSpacingAndImprovedLayout }, className) }), _this2._renderValue({ value: value, valueInShort: valueInShort, size: size || defaultSize, reducedSpacingAndImprovedLayout: reducedSpacingAndImprovedLayout, isLoading: isLoading }), _this2._renderDescription({ description: description, descriptionInfo: descriptionInfo, size: size || defaultSize, alignItems: alignItems, reducedSpacingAndImprovedLayout: reducedSpacingAndImprovedLayout }), _this2._renderPercents({ percentage: percentage, invertedPercentage: invertedPercentage }), children); }); } }]); return StatisticsItem; }(React.PureComponent); _defineProperty(StatisticsItem, "displayName", 'StatisticsItem'); _defineProperty(StatisticsItem, "defaultProps", { alignItems: 'center' }); export default withFocusable(StatisticsItem);