UNPKG

@spaced-out/ui-design-system

Version:
108 lines (107 loc) 4.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rating = exports.RATING_SIZE = void 0; var React = _interopRequireWildcard(require("react")); var _invariant = _interopRequireDefault(require("invariant")); var _lodash = require("lodash"); var _classify = require("../../utils/classify"); var _qa = require("../../utils/qa"); var _rating = require("../../utils/rating"); var _Button = require("../Button"); var _Chip = require("../Chip"); var _ConditionalWrapper = require("../ConditionalWrapper"); var _Icon = require("../Icon"); var _Text = require("../Text"); var _RatingModule = _interopRequireDefault(require("./Rating.module.css")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const RATING_SIZE = exports.RATING_SIZE = Object.freeze({ small: 'small', medium: 'medium' }); const Rating = exports.Rating = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { size = 'medium', start = 0, end = 5, quiet, rating = 0, labels = _rating.RATINGS, onChange, readOnly, hideLabel, iconName = 'star', iconColor = _Text.TEXT_COLORS.favorite, chipSemantic = _Chip.CHIP_SEMANTIC.warning, classNames, testId } = _ref; const [hoverIcon, setHoverIcon] = React.useState(-1); const chipText = (0, _rating.getRatingLabel)(rating - start, labels); const totalIcons = end - start; (0, _invariant.default)(totalIcons, JSON.stringify(_rating.RATING_ERRORS.INVALID_RANGE)); (0, _invariant.default)((0, _lodash.isUndefined)(labels) || (0, _lodash.isArray)(labels) && labels.length, JSON.stringify(_rating.RATING_ERRORS.INVALID_RATING_LABELS)); return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", { "data-testid": (0, _qa.generateTestId)({ base: testId, slot: 'wrapper' }), ref: ref, className: (0, _classify.classify)(_RatingModule.default.ratingContainer, classNames?.wrapper), onBlur: () => !quiet && setHoverIcon(-1), children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", { className: _RatingModule.default.stars, children: Array.from({ length: totalIcons }).map((_, index) => { const isHovering = hoverIcon > -1; const iconCount = index + 1; const iconType = (isHovering ? iconCount <= hoverIcon : iconCount <= rating - start) ? 'solid' : 'regular'; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ConditionalWrapper.ConditionalWrapper, { condition: !readOnly, wrapper: children => /*#__PURE__*/(0, _jsxRuntime.jsx)(_Button.UnstyledButton, { onKeyDown: e => { if (e.key === 'Enter') { onChange?.(start + iconCount, e); } }, onFocus: () => !quiet && setHoverIcon(iconCount), className: (0, _classify.classify)(_RatingModule.default.button, classNames?.button), onClick: e => onChange?.(start + iconCount, e), onMouseEnter: () => !quiet && setHoverIcon(iconCount), onMouseLeave: () => !quiet && setHoverIcon(-1), testId: (0, _qa.generateTestId)({ base: testId, slot: 'star-button', index: iconCount.toString() }), children: children }), children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, { size: size, name: iconName, type: iconType, color: iconColor, className: (0, _classify.classify)(_RatingModule.default.icon, classNames?.icon), testId: (0, _qa.generateTestId)({ base: testId, slot: 'star-icon', index: iconCount.toString() }) }) }, iconCount); }) }), !hideLabel && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Chip.Chip, { size: size, semantic: chipSemantic, testId: (0, _qa.generateTestId)({ base: testId, slot: 'rating-chip' }), children: chipText })] }); });