UNPKG

react-flexible-star-rating

Version:
3 lines (2 loc) 4.47 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react/jsx-runtime"),r=require("react"),n=function(){return n=Object.assign||function(t){for(var r,n=1,e=arguments.length;n<e;n++)for(var i in r=arguments[n])Object.prototype.hasOwnProperty.call(r,i)&&(t[i]=r[i]);return t},n.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var e="#FFC107",i=function(r){var n=r.color,i=void 0===n?e:n;return t.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:i,viewBox:"0 0 16 16",children:t.jsx("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"})})},o=function(r){var n=r.color,i=void 0===n?e:n;return t.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:i,viewBox:"0 0 16 16",children:t.jsx("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"})})},a=function(r){var n=r.color,i=void 0===n?e:n;return t.jsx("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",fill:i,viewBox:"0 0 16 16",children:t.jsx("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"})})};exports.StarRating=function(e){var l=e.starsLength,u=void 0===l?5:l,s=e.initialRating,c=void 0===s?0:s,d=e.isHalfRatingEnabled,f=void 0===d?c%1!=0:d,h=e.isReadOnly,v=void 0!==h&&h,g=e.isHoverEnabled,w=void 0===g?!v:g,p=e.dimension,x=void 0===p?30:p,m=e.color,b=e.onRatingChange;if(c>u||c<0)throw new Error("initialRating must be within range: 0 <= initialRating <= starsLength.");if(c%.5!=0)throw new Error("initialRating must be a whole number or a multiple of 0.5 when half-rating is enabled.");if(!f&&c%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(v&&w)throw new Error("isHoverEnabled cannot be true when isReadOnly is enabled. Please disable hover interactions for read-only mode.");var L,j,R=function(t){var r=Math.floor(t),n=t-r>0,e=Array.from({length:u},(function(t,n){return n<r?2:0}));return n&&(e[r]=1),e},y=r.useState((function(){return R(c)})),E=y[0],M=y[1],S=r.useState(E),z=S[0],C=S[1],O=r.useState(c),B=O[0],A=O[1],H=r.useState(null),P=H[0],k=H[1],q=r.useState(!1),F=q[0],T=q[1],X=r.useCallback((function(r){switch(r){case 1:return t.jsx(o,{color:m});case 2:return t.jsx(a,{color:m});default:return t.jsx(i,{color:m})}}),[m]),_=function(t){return M(t)},W=function(t){return A(t)},D=function(t){b&&b(t)},G=function(t){if(C(E),B===t)return r=Array(u).fill(0),_(r),C(r),W(null),void D(0);var r,n=R(t);_(n),W(t),D(t)},I=function(t,r){var n=function(t,r){var n=f&&function(t){var r=t.currentTarget.getBoundingClientRect().width/2;return t.nativeEvent.offsetX<r}(t);return n?r-.5:r}(t,r);G(n),T(!0)},J=function(){_(z),k(null),T(!1)};return t.jsx("div",{style:{display:"flex",maxWidth:"".concat(x,"rem")},children:(L=x?Math.max(.005*x,x/u/10):.5,j=u>1,E.map((function(r,e){return t.jsx("div",n({style:n(n({},j&&e<u-1&&{paddingRight:"".concat(L,"rem")}),{cursor:v?"not-allowed":"pointer"})},!v&&n({onClick:function(t){return I(t,e+1)}},w&&{onMouseMove:F?f?function(){return T(!1)}:void 0:function(t){return function(t,r){if(f){var n=t.currentTarget.getBoundingClientRect(),e=n.width/2,i=t.clientX<n.left+e;i&&"L"!==P?(_(R(r-.5)),k("L")):i||"R"===P||(_(R(r)),k("R"))}else _(R(r))}(t,e+1)},onMouseLeave:J}),{children:X(r)}),e)})))})}; //# sourceMappingURL=index.js.map