@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
120 lines (119 loc) • 4.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _clsx = _interopRequireDefault(require("clsx"));
var _componentHelper = require("../../shared/component-helper.js");
var _index = require("../../shared/index.js");
var _clamp = require("../../shared/helpers/clamp.js");
var _Text = require("./Text.js");
var _jsxRuntime = require("react/jsx-runtime");
var _StarIcon, _span, _path;
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const MAX_ALLOWED = 20;
function Rating(props) {
const {
value = 0,
max = null,
id = null,
variant = 'stars',
element: Element = 'span',
className = null,
style = null,
srLabel = null,
skeleton = null,
...rest
} = props;
const defaultMax = variant === 'progressive' ? 7 : 5;
const resolvedMax = Number.isFinite(max) && max > 0 ? Math.floor(max) : defaultMax;
if (resolvedMax > MAX_ALLOWED) {
(0, _componentHelper.warn)(`Stat.Rating: max=${resolvedMax} exceeds the supported limit of ${MAX_ALLOWED}. The value will be clamped.`);
}
const clampedMax = Math.min(resolvedMax, MAX_ALLOWED);
const safeValue = Number.isFinite(value) ? value : 0;
const normalizedValue = (0, _clamp.clamp)(safeValue, 0, clampedMax);
const labelValue = Number.isInteger(normalizedValue) ? String(normalizedValue) : String(parseFloat(normalizedValue.toFixed(2)));
const {
Stat: {
rating: ratingTemplate = '%value of %max'
} = {}
} = (0, _index.useTranslation)();
const localizedRating = ratingTemplate.replace('%value', labelValue).replace('%max', String(clampedMax));
const label = srLabel ? `${(0, _componentHelper.convertJsxToString)(srLabel)} ${localizedRating}` : localizedRating;
return (0, _jsxRuntime.jsx)(_Text.TextInternal, {
...rest,
id: id,
element: Element,
className: (0, _clsx.default)(`dnb-stat dnb-stat__rating dnb-stat__rating--${variant}`, className),
style: style,
role: "img",
"aria-label": label,
skeleton: skeleton,
skeletonMethod: "shape",
textClassName: false,
children: variant === 'stars' ? (0, _jsxRuntime.jsx)("span", {
className: "dnb-stat__rating-stars",
"aria-hidden": true,
children: Array.from({
length: clampedMax
}).map((_, index) => {
const fill = (0, _clamp.clamp)(normalizedValue - index, 0, 1);
return (0, _jsxRuntime.jsxs)("span", {
className: "dnb-stat__rating-star",
style: {
'--dnb-stat-rating-fill': `${fill * 100}%`
},
"data-fill": fill.toFixed(2),
children: [_StarIcon || (_StarIcon = (0, _jsxRuntime.jsx)(StarIcon, {
variant: "base"
})), _span || (_span = (0, _jsxRuntime.jsx)("span", {
className: "dnb-stat__rating-fill",
children: (0, _jsxRuntime.jsx)(StarIcon, {
variant: "active"
})
}))]
}, index);
})
}) : (0, _jsxRuntime.jsx)("span", {
className: "dnb-stat__rating-progressive",
"aria-hidden": true,
children: Array.from({
length: clampedMax
}).map((_, index) => {
const fill = (0, _clamp.clamp)(normalizedValue - index, 0, 1);
const stepHeight = clampedMax > 1 ? 0.25 + index / (clampedMax - 1) * 0.75 : 1;
return (0, _jsxRuntime.jsx)("span", {
className: "dnb-stat__rating-progressive-step dnb-stat__rating-progressive-step--base",
style: {
'--dnb-stat-rating-step-fill': `${fill * 100}%`,
'--dnb-stat-rating-step-height': `${stepHeight}rem`
},
"data-fill": fill.toFixed(2)
}, index);
})
})
});
}
const STAR_PATH = 'M5.9996 0L7.8546 3.95006L12 4.58343L9.0006 7.65834L9.708 12L5.9996 9.95006L2.2928 12L3.0002 7.65834L0 4.58343L4.1462 3.95006L5.9996 0Z';
function StarIcon({
variant
}) {
return (0, _jsxRuntime.jsx)("svg", {
className: `dnb-stat__rating-icon dnb-stat__rating-icon--${variant}`,
width: "12",
height: "12",
viewBox: "0 0 12 12",
fill: "none",
xmlns: "http://www.w3.org/2000/svg",
children: _path || (_path = (0, _jsxRuntime.jsx)("path", {
fillRule: "evenodd",
clipRule: "evenodd",
d: STAR_PATH
}))
});
}
Rating._supportsSpacingProps = true;
var _default = exports.default = Rating;
//# sourceMappingURL=Rating.js.map