@elastic/eui
Version:
Elastic UI Component Library
112 lines (111 loc) • 5.33 kB
JavaScript
var _excluded = ["children", "className", "description", "isLoading", "reverse", "textAlign", "title", "titleColor", "titleSize", "titleElement", "descriptionElement"];
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { createElement, useMemo } from 'react';
import PropTypes from "prop-types";
import classNames from 'classnames';
import { EuiText } from '../text';
import { EuiTitle } from '../title/title';
import { useEuiI18n } from '../i18n';
import { useEuiMemoizedStyles } from '../../services';
import { euiStatStyles, euiStatTitleStyles } from './stat.styles';
import { jsx as ___EmotionJSX } from "@emotion/react";
export var COLORS = ['default', 'subdued', 'primary', 'success', 'warning', 'danger', 'accent'];
export var ALIGNMENTS = ['left', 'center', 'right'];
export var EuiStat = function EuiStat(_ref) {
var children = _ref.children,
className = _ref.className,
description = _ref.description,
_ref$isLoading = _ref.isLoading,
isLoading = _ref$isLoading === void 0 ? false : _ref$isLoading,
_ref$reverse = _ref.reverse,
reverse = _ref$reverse === void 0 ? false : _ref$reverse,
_ref$textAlign = _ref.textAlign,
textAlign = _ref$textAlign === void 0 ? 'left' : _ref$textAlign,
title = _ref.title,
_ref$titleColor = _ref.titleColor,
titleColor = _ref$titleColor === void 0 ? 'default' : _ref$titleColor,
_ref$titleSize = _ref.titleSize,
titleSize = _ref$titleSize === void 0 ? 'l' : _ref$titleSize,
_ref$titleElement = _ref.titleElement,
titleElement = _ref$titleElement === void 0 ? 'p' : _ref$titleElement,
_ref$descriptionEleme = _ref.descriptionElement,
descriptionElement = _ref$descriptionEleme === void 0 ? 'p' : _ref$descriptionEleme,
rest = _objectWithoutProperties(_ref, _excluded);
var classes = classNames('euiStat', className);
var styles = useEuiMemoizedStyles(euiStatStyles);
var cssStyles = [styles.euiStat, styles[textAlign]];
var loadingStatsAriaLabel = useEuiI18n('euiStat.loadingText', 'Statistic is loading');
var titleStyles = useEuiMemoizedStyles(euiStatTitleStyles);
var titleDisplay = useMemo(function () {
var isNamedTitleColor = COLORS.includes(titleColor);
var titleCssStyles = [titleStyles.euiStat__title, isNamedTitleColor && titleStyles[titleColor], isLoading && titleStyles.isLoading];
return ___EmotionJSX(EuiTitle, {
size: titleSize,
className: "euiStat__title",
css: titleCssStyles,
"aria-label": isLoading ? loadingStatsAriaLabel : undefined
}, /*#__PURE__*/createElement(titleElement, isNamedTitleColor ? null : {
style: {
color: titleColor
}
}, isLoading ? '--' : title));
}, [title, titleElement, titleColor, titleSize, titleStyles, isLoading, loadingStatsAriaLabel]);
var descriptionDisplay = useMemo(function () {
return ___EmotionJSX(EuiText, {
size: "s",
className: "euiStat__description"
}, /*#__PURE__*/createElement(descriptionElement, null, description));
}, [description, descriptionElement]);
return ___EmotionJSX("div", _extends({
css: cssStyles,
className: classes
}, rest), !reverse && descriptionDisplay, titleDisplay, reverse && descriptionDisplay, children);
};
EuiStat.propTypes = {
className: PropTypes.string,
"aria-label": PropTypes.string,
"data-test-subj": PropTypes.string,
css: PropTypes.any,
/**
* Set the description (label) text
*/
description: PropTypes.node.isRequired,
/**
* Will hide the title with an animation until false
*/
isLoading: PropTypes.bool,
/**
* Flips the order of the description and title
*/
reverse: PropTypes.bool,
textAlign: PropTypes.any,
/**
* The (value) text
*/
title: PropTypes.node.isRequired,
/**
* The color of the title text
*/
titleColor: PropTypes.oneOfType([PropTypes.any.isRequired, PropTypes.string.isRequired]),
/**
* Size of the title. See EuiTitle for options ('s', 'm', 'l'... etc)
*/
titleSize: PropTypes.any,
/**
* HTML Element to be used for title
*/
titleElement: PropTypes.string,
/**
* HTML Element to be used for description
*/
descriptionElement: PropTypes.string
};