react-flexible-star-rating
Version:
A flexible and customizable React star rating component.
3 lines (2 loc) • 4.34 kB
JavaScript
import{jsx as n}from"react/jsx-runtime";import{useState as r,useCallback as t}from"react";var e=function(){return e=Object.assign||function(n){for(var r,t=1,e=arguments.length;t<e;t++)for(var i in r=arguments[t])Object.prototype.hasOwnProperty.call(r,i)&&(n[i]=r[i]);return n},e.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var i="#FFC107",o=function(r){var t=r.color;return n("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:void 0===t?i:t,viewBox:"0 0 16 16",children:n("path",{d:"M2.866 14.85c-.078.444.36.791.746.593l4.39-2.256 4.389 2.256c.386.198.824-.149.746-.592l-.83-4.73 3.522-3.356c.33-.314.16-.888-.282-.95l-4.898-.696L8.465.792a.513.513 0 0 0-.927 0L5.354 5.12l-4.898.696c-.441.062-.612.636-.283.95l3.523 3.356-.83 4.73zm4.905-2.767-3.686 1.894.694-3.957a.56.56 0 0 0-.163-.505L1.71 6.745l4.052-.576a.53.53 0 0 0 .393-.288L8 2.223l1.847 3.658a.53.53 0 0 0 .393.288l4.052.575-2.906 2.77a.56.56 0 0 0-.163.506l.694 3.957-3.686-1.894a.5.5 0 0 0-.461 0z"})})},a=function(r){var t=r.color;return n("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:void 0===t?i:t,viewBox:"0 0 16 16",children:n("path",{d:"M5.354 5.119 7.538.792A.52.52 0 0 1 8 .5c.183 0 .366.097.465.292l2.184 4.327 4.898.696A.54.54 0 0 1 16 6.32a.55.55 0 0 1-.17.445l-3.523 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256a.5.5 0 0 1-.146.05c-.342.06-.668-.254-.6-.642l.83-4.73L.173 6.765a.55.55 0 0 1-.172-.403.6.6 0 0 1 .085-.302.51.51 0 0 1 .37-.245zM8 12.027a.5.5 0 0 1 .232.056l3.686 1.894-.694-3.957a.56.56 0 0 1 .162-.505l2.907-2.77-4.052-.576a.53.53 0 0 1-.393-.288L8.001 2.223 8 2.226z"})})},l=function(r){var t=r.color;return n("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:void 0===t?i:t,viewBox:"0 0 16 16",children:n("path",{d:"M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"})})};function c(i){var c=i.starsLength,u=void 0===c?5:c,s=i.initialRating,f=void 0===s?0:s,d=i.isHalfRatingEnabled,h=void 0===d?f%1!=0:d,v=i.isReadOnly,g=void 0!==v&&v,w=i.isHoverEnabled,m=void 0===w?!g:w,p=i.dimension,b=void 0===p?30:p,L=i.color,R=i.onRatingChange;if(f>u||f<0)throw new Error("initialRating must be within range: 0 <= initialRating <= starsLength.");if(f%.5!=0)throw new Error("initialRating must be a whole number or a multiple of 0.5 when half-rating is enabled.");if(!h&&f%1!=0)throw new Error("initialRating must be a whole number when half-rating is disabled.");if(u<=0)throw new Error("starsLength must be greater than zero to ensure valid ratings.");if(g&&m)throw new Error("isHoverEnabled cannot be true when isReadOnly is enabled. Please disable hover interactions for read-only mode.");var y,x,E=function(n){var r=Math.floor(n),t=n-r>0,e=Array.from({length:u},(function(n,t){return t<r?2:0}));return t&&(e[r]=1),e},M=r((function(){return E(f)})),z=M[0],B=M[1],C=r(z),O=C[0],A=C[1],j=r(f),H=j[0],F=j[1],P=r(null),S=P[0],T=P[1],X=r(!1),k=X[0],W=X[1],q=t((function(r){switch(r){case 1:return n(a,{color:L});case 2:return n(l,{color:L});default:return n(o,{color:L})}}),[L]),D=function(n){return B(n)},G=function(n){return F(n)},I=function(n){R&&R(n)},J=function(n){if(A(z),H===n)return r=Array(u).fill(0),D(r),A(r),G(null),void I(0);var r,t=E(n);D(t),G(n),I(n)},K=function(n,r){var t=function(n,r){var t=h&&function(n){var r=n.currentTarget.getBoundingClientRect().width/2;return n.nativeEvent.offsetX<r}(n);return t?r-.5:r}(n,r);J(t),W(!0)},N=function(){D(O),T(null),W(!1)};return n("div",{style:{display:"flex",maxWidth:"".concat(b,"rem")},children:(y=b?Math.max(.005*b,b/u/10):.5,x=u>1,z.map((function(r,t){return n("div",e({style:e(e({},x&&t<u-1&&{paddingRight:"".concat(y,"rem")}),{cursor:g?"not-allowed":"pointer"})},!g&&e({onClick:function(n){return K(n,t+1)}},m&&{onMouseMove:k?h?function(){return W(!1)}:void 0:function(n){return function(n,r){if(h){var t=n.currentTarget.getBoundingClientRect(),e=t.width/2,i=n.clientX<t.left+e;i&&"L"!==S?(D(E(r-.5)),T("L")):i||"R"===S||(D(E(r)),T("R"))}else D(E(r))}(n,t+1)},onMouseLeave:N}),{children:q(r)}),t)})))})}export{c as StarRating};
//# sourceMappingURL=index.esm.js.map