UNPKG

@roo-ui/components

Version:

50 lines (46 loc) 1.53 kB
import React from 'react'; import range from 'lodash/range'; import PropTypes from 'prop-types'; import { Flex, Icon } from '..'; var iconType = function iconType(rating, index, ratingType) { var ratingTypeIcon = ratingType === 'AAA' ? 'star' : 'circle'; if (index < rating - 0.5) return "".concat(ratingTypeIcon); if (index < rating) return "".concat(ratingTypeIcon, "Half"); return "".concat(ratingTypeIcon, "Border"); }; var renderRating = function renderRating(_ref) { var ratingType = _ref.ratingType, rating = _ref.rating, size = _ref.size; var ratingItems = []; range(5).forEach(function (index) { ratingItems.push(React.createElement(Icon, { name: iconType(rating, index, ratingType), key: index, color: "brightSun", size: size })); }); return ratingItems; }; var StarRating = function StarRating(_ref2) { var ratingType = _ref2.ratingType, rating = _ref2.rating, size = _ref2.size; return React.createElement(Flex, { itemType: "http://schema.org/AggregateRating", "aria-label": "".concat(rating, " out of 5 rating"), title: "".concat(rating, " out of 5 rating") }, renderRating({ ratingType: ratingType, rating: rating, size: size })); }; var stringOrNumber = PropTypes.oneOfType([PropTypes.string, PropTypes.number]); StarRating.propTypes = { rating: stringOrNumber.isRequired, ratingType: PropTypes.oneOf(['AAA', 'SELF_RATED']).isRequired, size: stringOrNumber.isRequired }; export default StarRating;