UNPKG

wix-style-react

Version:
190 lines (162 loc) • 7.13 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 PropTypes from 'prop-types'; import Tooltip from '../Tooltip'; import Heading from '../Heading'; import AdaptiveHeading from '../utils/AdaptiveHeading'; import { st, classes, vars } from './BarChart.st.css'; import dataHooks from './dataHooks'; var BarChart = /*#__PURE__*/function (_React$PureComponent) { _inherits(BarChart, _React$PureComponent); var _super = _createSuper(BarChart); function BarChart() { var _this; _classCallCheck(this, BarChart); 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), "MIN_BAR_WIDTH", 50); _defineProperty(_assertThisInitialized(_this), "state", { width: 0 }); _defineProperty(_assertThisInitialized(_this), "_renderValue", function (_ref) { var descriptionInfo = _ref.descriptionInfo, value = _ref.value, label = _ref.label, labelShort = _ref.labelShort, showText = _ref.showText; var text = String(label || value); var onDescriptionInfoShown = _this.props.onDescriptionInfoShown; var headingProps = { text: text, textInShort: labelShort, dataHook: dataHooks.value, appearance: 'H3', light: true }; return descriptionInfo ? /*#__PURE__*/React.createElement(Tooltip, { textAlign: "start", dataHook: dataHooks.tooltip, content: descriptionInfo, onShow: onDescriptionInfoShown, zIndex: 5999 }, /*#__PURE__*/React.createElement("div", { className: classes.value }, showText && /*#__PURE__*/React.createElement(AdaptiveHeading, _extends({}, headingProps, { emptyLast: true })))) : /*#__PURE__*/React.createElement("div", { className: classes.value }, showText && /*#__PURE__*/React.createElement(AdaptiveHeading, headingProps)); }); _defineProperty(_assertThisInitialized(_this), "_renderItem", function (_ref2, key) { var value = _ref2.value, label = _ref2.label, labelShort = _ref2.labelShort, description = _ref2.description, descriptionInfo = _ref2.descriptionInfo; var width = _this.state.width; var total = _this.props.total; var calculatedTotal = _this._getCalculatedTotal(); var coefficient = total ? calculatedTotal / total : 1; var showText = width === 0 || value * width / (calculatedTotal * coefficient) > _this.MIN_BAR_WIDTH; return /*#__PURE__*/React.createElement("div", { className: st(classes.item), key: key, "data-hook": dataHooks.bar, style: _defineProperty({}, vars.barValue, value / Math.pow(10, calculatedTotal.toString().length - 1)) }, _this._renderValue({ descriptionInfo: descriptionInfo, value: value, label: label, labelShort: labelShort, showText: showText }), /*#__PURE__*/React.createElement("div", { className: classes.description }, /*#__PURE__*/React.createElement(Heading, { ellipsis: true, dataHook: dataHooks.description, appearance: "H5" }, showText && description))); }); return _this; } _createClass(BarChart, [{ key: "componentDidMount", value: function componentDidMount() { this.setState({ width: this.node.offsetWidth }); } }, { key: "_getCalculatedTotal", value: function _getCalculatedTotal() { return this.props.items.reduce(function (a, b) { return a + b.value; }, 0); } }, { key: "render", value: function render() { var _this2 = this; var _this$props = this.props, dataHook = _this$props.dataHook, items = _this$props.items, total = _this$props.total; var calculatedTotal = this._getCalculatedTotal(); var width = total ? calculatedTotal / total * 100 : 100; return /*#__PURE__*/React.createElement("div", { "data-hook": dataHook, ref: function ref(elem) { return _this2.node = elem; }, className: classes.wrapper }, /*#__PURE__*/React.createElement("div", { className: classes.root, style: { width: "".concat(width, "%") } }, items.slice().sort(function (a, b) { return b.value - a.value; }).map(this._renderItem))); } }]); return BarChart; }(React.PureComponent); _defineProperty(BarChart, "displayName", 'BarChart'); _defineProperty(BarChart, "defaultProps", { items: [] }); _defineProperty(BarChart, "propTypes", { /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** * Array of items * * `value` - This prop is used for sorting bars. Displayed as big text on a bar, when there is no caption prop. * * `label` - Displayed as big text on a bar. * * `labelShort` - Is shown instead of a `label` when there is not enough space. * * `description` - short label under the bar. * * `descriptionInfo` - long description. */ items: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.number.isRequired, label: PropTypes.node, labelShort: PropTypes.node, description: PropTypes.node, descriptionInfo: PropTypes.node })), /** Used to calculate space for bars inside a widget. Should be specified if the actual total is different from the sum of values of all items */ total: PropTypes.number, /** Callback called every time when descriptionInfo tooltip is shown*/ onDescriptionInfoShown: PropTypes.func }); export default BarChart;