stars-rating-react-hooks
Advanced tools
Comparing version 0.2.2 to 0.2.3
import React from 'react'; | ||
import { Config, Renderable } from './types'; | ||
export declare function useStars(config: Config): { | ||
export declare function useStars(config: Config, isDisabled?: boolean): { | ||
getStarProps: (index: number, elementProps?: any) => any; | ||
@@ -5,0 +5,0 @@ stars: Renderable[]; |
@@ -44,3 +44,7 @@ 'use strict'; | ||
function useStars(config) { | ||
function useStars(config, isDisabled) { | ||
if (isDisabled === void 0) { | ||
isDisabled = false; | ||
} | ||
var totalStars = config.totalStars, | ||
@@ -67,2 +71,3 @@ _config$initialSelect = config.initialSelectedValue, | ||
var _onClick = function onClick() { | ||
if (isDisabled) return; | ||
setSelectedValue(selectingValue); | ||
@@ -72,2 +77,3 @@ }; | ||
var onMouseEnter = function onMouseEnter() { | ||
if (isDisabled) return; | ||
setisSelecting(true); | ||
@@ -77,2 +83,3 @@ }; | ||
var onMouseLeave = function onMouseLeave() { | ||
if (isDisabled) return; | ||
setSelecting(selectedValue); | ||
@@ -83,2 +90,3 @@ setisSelecting(false); | ||
var _onMouseMove = function onMouseMove(e, index) { | ||
if (isDisabled) return; | ||
var rect = e.target.getBoundingClientRect(); | ||
@@ -85,0 +93,0 @@ var xmiddleRelativePosistion = e.pageX - e.target.offsetLeft; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e;function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var a=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return t.forEach((function(e){return e&&e.apply(void 0,n)}))}};function o(e){var t=e.totalStars,o=e.initialSelectedValue,u=void 0===o?0:o,i=e.renderFull,l=e.renderHalf,s=void 0===l?null:l,c=e.renderEmpty,f=n.useState(u),d=f[0],p=f[1],g=n.useState(u),v=g[0],S=g[1],m=n.useState(!1),y=m[0],h=m[1],M=function(){h(!0)},_=function(){S(d),h(!1)};return{getStarProps:function(e,t){var n=null!==s?e+.5:e+1;return r({},t,{onClick:function(e){return a(void p(v),t&&t.onClick(e,v))},onMouseMove:function(e){return a(function(e,t){var n=e.target.getBoundingClientRect();S(null!==s&&n.width/2<=e.pageX-e.target.offsetLeft?t+.5:t)}(e,n),t&&t.onMouseMove&&t.onMouseMove(e,n))}})},stars:Array.from(Array(t),(function(e,t){var n=null!==s?t+.5:t+1,r=n<=v,a=!Number.isInteger(v)&&n===v;return r?a?s:i:c})),getStarWrapperProps:function(e){return r({},e,{onMouseLeave:a(e&&e.onMouseLeave,_),onMouseEnter:a(e&&e.onMouseEnter,M)})},isSelecting:y,selectedValue:d,selectingValue:v}}!function(e,t){void 0===t&&(t={});var n=t.insertAt;if("undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],a=document.createElement("style");a.type="text/css","top"===n&&r.firstChild?r.insertBefore(a,r.firstChild):r.appendChild(a),a.styleSheet?a.styleSheet.cssText=e:a.appendChild(document.createTextNode(e))}}(".StarRating-module_stars__3roiw {\n cursor: pointer;\n display: inline-block;\n}\n.StarRating-module_star__MMPKi {\n display: inline-block;\n}\n"),exports.StarsRating=function(e){var n=o(e.config),r=n.stars,a=n.getStarProps,u=n.getStarWrapperProps,i=n.isSelecting,l=n.selectingValue,s=n.selectedValue,c=t.useRef(!1);return t.useEffect((function(){e&&e.onSelecting&&null!=e&&e.onSelecting&&e.onSelecting(i,l)}),[i,l]),t.useEffect((function(){!0===c.current&&null!=e&&e.onStarsRated&&e.onStarsRated(s),c.current=!0}),[s]),t.createElement(t.Fragment,null,t.createElement("div",null,t.createElement("span",Object.assign({tabIndex:0},u(),{className:"StarRating-module_stars__3roiw"}),null==r?void 0:r.map((function(e,n){return t.createElement("span",Object.assign({key:n},a(n),{className:"StarRating-module_star__MMPKi"}),e)})))))},exports.default=o,exports.useStars=o; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),n=(e=t)&&"object"==typeof e&&"default"in e?e.default:e;function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var a=function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];return function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return t.forEach((function(e){return e&&e.apply(void 0,n)}))}};function o(e,t){void 0===t&&(t=!1);var o=e.totalStars,u=e.initialSelectedValue,i=void 0===u?0:u,l=e.renderFull,s=e.renderHalf,c=void 0===s?null:s,f=e.renderEmpty,d=n.useState(i),p=d[0],g=d[1],v=n.useState(i),S=v[0],m=v[1],y=n.useState(!1),h=y[0],M=y[1],_=function(){t||M(!0)},E=function(){t||(m(p),M(!1))};return{getStarProps:function(e,n){var o=null!==c?e+.5:e+1;return r({},n,{onClick:function(e){return a(void(t||g(S)),n&&n.onClick(e,S))},onMouseMove:function(e){return a(function(e,n){if(!t){var r=e.target.getBoundingClientRect();m(null!==c&&r.width/2<=e.pageX-e.target.offsetLeft?n+.5:n)}}(e,o),n&&n.onMouseMove&&n.onMouseMove(e,o))}})},stars:Array.from(Array(o),(function(e,t){var n=null!==c?t+.5:t+1,r=n<=S,a=!Number.isInteger(S)&&n===S;return r?a?c:l:f})),getStarWrapperProps:function(e){return r({},e,{onMouseLeave:a(e&&e.onMouseLeave,E),onMouseEnter:a(e&&e.onMouseEnter,_)})},isSelecting:h,selectedValue:p,selectingValue:S}}!function(e,t){void 0===t&&(t={});var n=t.insertAt;if("undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],a=document.createElement("style");a.type="text/css","top"===n&&r.firstChild?r.insertBefore(a,r.firstChild):r.appendChild(a),a.styleSheet?a.styleSheet.cssText=e:a.appendChild(document.createTextNode(e))}}(".StarRating-module_stars__3roiw {\n cursor: pointer;\n display: inline-block;\n}\n.StarRating-module_star__MMPKi {\n display: inline-block;\n}\n"),exports.StarsRating=function(e){var n=o(e.config),r=n.stars,a=n.getStarProps,u=n.getStarWrapperProps,i=n.isSelecting,l=n.selectingValue,s=n.selectedValue,c=t.useRef(!1);return t.useEffect((function(){e&&e.onSelecting&&null!=e&&e.onSelecting&&e.onSelecting(i,l)}),[i,l]),t.useEffect((function(){!0===c.current&&null!=e&&e.onStarsRated&&e.onStarsRated(s),c.current=!0}),[s]),t.createElement(t.Fragment,null,t.createElement("div",null,t.createElement("span",Object.assign({tabIndex:0},u(),{className:"StarRating-module_stars__3roiw"}),null==r?void 0:r.map((function(e,n){return t.createElement("span",Object.assign({key:n},a(n),{className:"StarRating-module_star__MMPKi"}),e)})))))},exports.default=o,exports.useStars=o; | ||
//# sourceMappingURL=stars-rating-react-hooks.cjs.production.min.js.map |
@@ -37,3 +37,7 @@ import React__default, { useRef, useEffect, createElement, Fragment } from 'react'; | ||
function useStars(config) { | ||
function useStars(config, isDisabled) { | ||
if (isDisabled === void 0) { | ||
isDisabled = false; | ||
} | ||
var totalStars = config.totalStars, | ||
@@ -60,2 +64,3 @@ _config$initialSelect = config.initialSelectedValue, | ||
var _onClick = function onClick() { | ||
if (isDisabled) return; | ||
setSelectedValue(selectingValue); | ||
@@ -65,2 +70,3 @@ }; | ||
var onMouseEnter = function onMouseEnter() { | ||
if (isDisabled) return; | ||
setisSelecting(true); | ||
@@ -70,2 +76,3 @@ }; | ||
var onMouseLeave = function onMouseLeave() { | ||
if (isDisabled) return; | ||
setSelecting(selectedValue); | ||
@@ -76,2 +83,3 @@ setisSelecting(false); | ||
var _onMouseMove = function onMouseMove(e, index) { | ||
if (isDisabled) return; | ||
var rect = e.target.getBoundingClientRect(); | ||
@@ -78,0 +86,0 @@ var xmiddleRelativePosistion = e.pageX - e.target.offsetLeft; |
{ | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -7,3 +7,3 @@ import React, { HtmlHTMLAttributes } from 'react'; | ||
export function useStars(config: Config) { | ||
export function useStars(config: Config, isDisabled: boolean = false) { | ||
const { | ||
@@ -33,2 +33,3 @@ totalStars, | ||
const onClick = () => { | ||
if (isDisabled) return; | ||
setSelectedValue(selectingValue); | ||
@@ -38,2 +39,3 @@ }; | ||
const onMouseEnter = () => { | ||
if (isDisabled) return; | ||
setisSelecting(true); | ||
@@ -43,2 +45,3 @@ }; | ||
const onMouseLeave = () => { | ||
if (isDisabled) return; | ||
setSelecting(selectedValue); | ||
@@ -49,2 +52,3 @@ setisSelecting(false); | ||
const onMouseMove = (e: any, index: number) => { | ||
if (isDisabled) return; | ||
const rect = e.target.getBoundingClientRect(); | ||
@@ -51,0 +55,0 @@ const xmiddleRelativePosistion = e.pageX - e.target.offsetLeft; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77750
818