UNPKG

wix-style-react

Version:
93 lines (82 loc) 3.19 kB
import React from 'react'; import PropTypes from 'prop-types'; import { WixStyleReactContext } from '../WixStyleReactProvider/context'; import StatisticsItem from './StatisticsItem'; import { st, classes } from './StatisticsWidget.st.css'; class StatisticsWidget extends React.PureComponent { static displayName = 'StatisticsWidget'; static propTypes = { /** Applied as data-hook HTML attribute that can be used to create driver in testing */ dataHook: PropTypes.string, /** Displayed value size (default: large) */ size: PropTypes.oneOf(['tiny', 'large']), /** Alignment of inner items (default: center) */ alignItems: PropTypes.oneOf(['start', 'center', 'end']), /** Show loader instead of values for all statistic items (default: undefined) */ isLoading: PropTypes.bool, /** * Array of statistic items * * `value` - Value of the statistic. Displayed as big text in the first row. * * `valueInShort` - Short version of value. Will be applied when there is no space for long value. If not specified, part of the value will be hidden with ellipsis * * `description` - Description of the statistic. Displayed in the second row. * * `descriptionInfo` - More info about the description. Displayed as an info icon with this text inside a tooltip * * `percentage` - Change in percents. Positive number - arrow up, negative - arrow down. * * `invertedPercentage` - When set to true renders positive percentage in red and negative in green. * * `onClick` - Callback to be executed on click (also on Enter/Space key press) * * `isLoading` - Shows a loader instead of value. * * `children` - Node to render on bottom of section. */ items: PropTypes.arrayOf( PropTypes.shape({ value: PropTypes.string, valueInShort: PropTypes.string, description: PropTypes.string, descriptionInfo: PropTypes.string, percentage: PropTypes.number, invertedPercentage: PropTypes.bool, onClick: PropTypes.func, children: PropTypes.node, }), ), }; _renderStat = (stat, key) => { const { size, alignItems, isLoading } = this.props; return ( <StatisticsItem {...stat} isLoading={isLoading} key={key} size={size} alignItems={alignItems} /> ); }; render() { const { dataHook, size } = this.props; let { items } = this.props; items = items || []; if (items.length > 5) { // eslint-disable-next-line console.warn( `${items.length} items were passed in items array. StatisticsWidget will display only the first 5.`, ); } const firstFive = items.slice(0, 5); return ( <WixStyleReactContext.Consumer> {({ reducedSpacingAndImprovedLayout }) => ( <div className={st(classes.root, { size, reducedSpacingAndImprovedLayout, })} data-hook={dataHook} > {firstFive.map(this._renderStat)} </div> )} </WixStyleReactContext.Consumer> ); } } export default StatisticsWidget;