UNPKG

@chepchik/react-rating

Version:

React Rating is a react rating component which supports custom symbols both with inline styles and icon.

416 lines (415 loc) 12.9 kB
import ne, { useState as H, useEffect as Q } from "react"; var J = { exports: {} }, $ = {}; /** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var K; function ae() { if (K) return $; K = 1; var n = Symbol.for("react.transitional.element"), s = Symbol.for("react.fragment"); function b(d, c, l) { var E = null; if (l !== void 0 && (E = "" + l), c.key !== void 0 && (E = "" + c.key), "key" in c) { l = {}; for (var m in c) m !== "key" && (l[m] = c[m]); } else l = c; return c = l.ref, { $$typeof: n, type: d, key: E, ref: c !== void 0 ? c : null, props: l }; } return $.Fragment = s, $.jsx = b, $.jsxs = b, $; } var F = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var ee; function se() { return ee || (ee = 1, process.env.NODE_ENV !== "production" && function() { function n(e) { if (e == null) return null; if (typeof e == "function") return e.$$typeof === U ? null : e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case y: return "Fragment"; case R: return "Profiler"; case M: return "StrictMode"; case q: return "Suspense"; case V: return "SuspenseList"; case z: return "Activity"; } if (typeof e == "object") switch (typeof e.tag == "number" && console.error( "Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue." ), e.$$typeof) { case C: return "Portal"; case x: return (e.displayName || "Context") + ".Provider"; case p: return (e._context.displayName || "Context") + ".Consumer"; case L: var r = e.render; return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e; case W: return r = e.displayName || null, r !== null ? r : n(e.type) || "Memo"; case I: r = e._payload, e = e._init; try { return n(e(r)); } catch { } } return null; } function s(e) { return "" + e; } function b(e) { try { s(e); var r = !1; } catch { r = !0; } if (r) { r = console; var o = r.error, i = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object"; return o.call( r, "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.", i ), s(e); } } function d(e) { if (e === y) return "<>"; if (typeof e == "object" && e !== null && e.$$typeof === I) return "<...>"; try { var r = n(e); return r ? "<" + r + ">" : "<...>"; } catch { return "<...>"; } } function c() { var e = P.A; return e === null ? null : e.getOwner(); } function l() { return Error("react-stack-top-frame"); } function E(e) { if (Y.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function m(e, r) { function o() { a || (a = !0, console.error( "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)", r )); } o.isReactWarning = !0, Object.defineProperty(e, "key", { get: o, configurable: !0 }); } function _() { var e = n(this.type); return t[e] || (t[e] = !0, console.error( "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release." )), e = this.props.ref, e !== void 0 ? e : null; } function v(e, r, o, i, j, T, G, B) { return o = T.ref, e = { $$typeof: w, type: e, key: r, props: T, _owner: j }, (o !== void 0 ? o : null) !== null ? Object.defineProperty(e, "ref", { enumerable: !1, get: _ }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: 0 }), Object.defineProperty(e, "_debugInfo", { configurable: !1, enumerable: !1, writable: !0, value: null }), Object.defineProperty(e, "_debugStack", { configurable: !1, enumerable: !1, writable: !0, value: G }), Object.defineProperty(e, "_debugTask", { configurable: !1, enumerable: !1, writable: !0, value: B }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e; } function N(e, r, o, i, j, T, G, B) { var u = r.children; if (u !== void 0) if (i) if (X(u)) { for (i = 0; i < u.length; i++) g(u[i]); Object.freeze && Object.freeze(u); } else console.error( "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead." ); else g(u); if (Y.call(r, "key")) { u = n(e); var S = Object.keys(r).filter(function(oe) { return oe !== "key"; }); i = 0 < S.length ? "{key: someKey, " + S.join(": ..., ") + ": ...}" : "{key: someKey}", A[u + i] || (S = 0 < S.length ? "{" + S.join(": ..., ") + ": ...}" : "{}", console.error( `A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, i, u, S, u ), A[u + i] = !0); } if (u = null, o !== void 0 && (b(o), u = "" + o), E(r) && (b(r.key), u = "" + r.key), "key" in r) { o = {}; for (var Z in r) Z !== "key" && (o[Z] = r[Z]); } else o = r; return u && m( o, typeof e == "function" ? e.displayName || e.name || "Unknown" : e ), v( e, u, T, j, c(), o, G, B ); } function g(e) { typeof e == "object" && e !== null && e.$$typeof === w && e._store && (e._store.validated = 1); } var k = ne, w = Symbol.for("react.transitional.element"), C = Symbol.for("react.portal"), y = Symbol.for("react.fragment"), M = Symbol.for("react.strict_mode"), R = Symbol.for("react.profiler"), p = Symbol.for("react.consumer"), x = Symbol.for("react.context"), L = Symbol.for("react.forward_ref"), q = Symbol.for("react.suspense"), V = Symbol.for("react.suspense_list"), W = Symbol.for("react.memo"), I = Symbol.for("react.lazy"), z = Symbol.for("react.activity"), U = Symbol.for("react.client.reference"), P = k.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, Y = Object.prototype.hasOwnProperty, X = Array.isArray, D = console.createTask ? console.createTask : function() { return null; }; k = { "react-stack-bottom-frame": function(e) { return e(); } }; var a, t = {}, f = k["react-stack-bottom-frame"].bind( k, l )(), h = D(d(l)), A = {}; F.Fragment = y, F.jsx = function(e, r, o, i, j) { var T = 1e4 > P.recentlyCreatedOwnerStacks++; return N( e, r, o, !1, i, j, T ? Error("react-stack-top-frame") : f, T ? D(d(e)) : h ); }, F.jsxs = function(e, r, o, i, j) { var T = 1e4 > P.recentlyCreatedOwnerStacks++; return N( e, r, o, !0, i, j, T ? Error("react-stack-top-frame") : f, T ? D(d(e)) : h ); }; }()), F; } var re; function ce() { return re || (re = 1, process.env.NODE_ENV === "production" ? J.exports = ae() : J.exports = se()), J.exports; } var O = ce(); const te = (n) => ne.isValidElement(n) ? n : typeof n == "object" && n !== null ? /* @__PURE__ */ O.jsx("span", { style: n }) : typeof n == "string" ? /* @__PURE__ */ O.jsx("span", { className: n }) : null, le = ({ index: n, inactiveIcon: s, activeIcon: b, percent: d, direction: c, readonly: l = !1, onClick: E, onMouseMove: m, onTouchEnd: _ }) => { const v = te(s), g = d < 100 ? {} : { visibility: "hidden" }, k = te(b), w = { display: "inline-block", position: "absolute", overflow: "hidden", top: 0, [c === "rtl" ? "right" : "left"]: 0, width: `${d}%` }, C = { cursor: l ? "inherit" : "pointer", display: "inline-block", position: "relative" }, y = (p) => { m && m(n, p); }, M = (p) => { E && (p.preventDefault(), E(n, p)); }, R = (p) => { _ && _(n, p); }; return /* @__PURE__ */ O.jsxs( "span", { style: C, onClick: M, onMouseMove: y, onTouchMove: y, onTouchEnd: R, children: [ /* @__PURE__ */ O.jsx("span", { style: g, children: v }), /* @__PURE__ */ O.jsx("span", { style: w, children: k }) ] } ); }, ue = ({ totalSymbols: n = 5, value: s, placeholderValue: b, readonly: d = !1, // Рейтинг редактируемый quiet: c = !1, // Включает обработку событий при касании // будет ли отображаться при наведении fractions: l = 1, // Поддержка дробных значений (например, 0.5). direction: E = "ltr", // Направление текста emptySymbol: m, // Настройка внешнего вида звездочек fullSymbol: _, // Настройка внешнего вида звездочек placeholderSymbol: v = void 0, // Отображаемое значение при наведении или текущее значение, если наведения нет. onClick: N, onHover: g = () => { }, className: k, id: w, style: C, tabIndex: y, "aria-label": M }) => { const [R, p] = H(s), [x, L] = H(!1); Q(() => { s !== R && p(s); }, [s]), Q(() => { !x && R !== s ? g(void 0) : x && s === R && g(R); }, [x, R, s, g]); const q = (a) => { const t = "touches" in a ? "changedTouches" in a ? a.changedTouches[0].clientX : ( // @ts-ignore a.touches[0].clientX ) : a.clientX, f = a.target.getBoundingClientRect(), h = E === "rtl" ? f.right - t : t - f.left; return h < 0 ? 0 : h / f.width; }, V = (a, t) => { const f = q(t), h = Math.ceil(f % 1 * l) / l, A = 10 ** 3, e = a + (Math.floor(f) + Math.floor(h * A) / A); return e > 0 ? e > n ? n : e : 1 / l; }, W = (a, t) => { if (d) return; const f = V(a, t); p(f), L(!0); }, I = (a, t) => { const f = V(a, t); N(f, t); }, z = (a, t) => { c || (I(a, t), t.preventDefault()), U(); }, U = () => { p(s), L(!1); }, P = b !== 0 && s === 0 && !x, Y = P ? b : c ? s : R, X = Math.floor(Y), D = Array.from({ length: n }).map((a, t) => { const f = t - X < 0 ? 100 : t - X === 0 ? (Y - t) * 100 : 0, h = Array.isArray(m) ? m : [m], A = Array.isArray(_) ? _ : [_], e = v ? Array.isArray(v) ? v : [v] : []; return /* @__PURE__ */ O.jsx( le, { index: t, readonly: d, inactiveIcon: h[t % h.length], activeIcon: P ? e[t % e.length] : A[t % A.length], percent: f, direction: E, ...!d && { onClick: I, onMouseMove: W, onTouchMove: W, onTouchEnd: z } }, t ); }); return /* @__PURE__ */ O.jsx( "span", { id: w, style: { ...C, display: "inline-block", direction: E }, className: k, tabIndex: y, "aria-label": M, ...!d && { onMouseLeave: U }, children: D } ); }; export { ue as Rating };