UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime

2 lines (1 loc) 5.5 kB
import*as e from"react";import{useEventListener as n}from"primereact/hooks";import{ObjectUtils as r,DomHandler as t,classNames as a}from"primereact/utils";function o(){return o=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var t in r)Object.prototype.hasOwnProperty.call(r,t)&&(e[t]=r[t])}return e},o.apply(this,arguments)}function i(e,n){(null==n||n>e.length)&&(n=e.length);for(var r=0,t=new Array(n);r<n;r++)t[r]=e[r];return t}function l(e){if(Array.isArray(e))return i(e)}function u(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function c(e,n){if(e){if("string"==typeof e)return i(e,n);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(e,n):void 0}}function s(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(e){if(Array.isArray(e))return e}function m(e,n){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var t,a,o=[],i=!0,l=!1;try{for(r=r.call(e);!(i=(t=r.next()).done)&&(o.push(t.value),!n||o.length!==n);i=!0);}catch(e){l=!0,a=e}finally{try{i||null==r.return||r.return()}finally{if(l)throw a}}return o}}function d(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function p(e,n){return f(e)||m(e,n)||c(e,n)||d()}var v=e.memo(e.forwardRef((function(i,f){var m=e.useRef(null),d=e.useRef(0),h=e.useRef(!1),y=e.useRef(!1),g=e.useRef(0),b=e.useRef(0),w=e.useRef(0),x=e.useRef(0),E=i.range?i.value||[0,100]:i.value||0,S="horizontal"===i.orientation,A="vertical"===i.orientation,R=p(n({type:"mousemove",listener:function(e){return W(e)}}),2),I=R[0],C=R[1],j=p(n({type:"mouseup",listener:function(e){return Y(e)}}),2),N=j[0],O=j[1],D=p(n({type:"touchmove",listener:function(e){return W(e)}}),2),P=D[0],M=D[1],T=p(n({type:"touchend",listener:function(e){return Y(e)}}),2),F=T[0],k=T[1],z=function(e,n){B(e,(i.range?E[d.current]:E)+(i.step||1)*n),e.preventDefault()},K=function(e,n){i.disabled||(y.current=!0,U(),h.current=!0,d.current=n)},W=function(e){y.current&&(X(e),e.preventDefault())},Y=function(e){y.current&&(y.current=!1,i.onSlideEnd&&i.onSlideEnd({originalEvent:e,value:i.value}),C(),O(),M(),k())},_=function(e,n){I(),N(),K(0,n)},H=function(e,n){P(),F(),K(0,n)},L=function(e,n){if(!i.disabled){d.current=n;var r=e.key;"ArrowRight"===r||"ArrowUp"===r?z(e,1):"ArrowLeft"!==r&&"ArrowDown"!==r||z(e,-1)}},U=function(){var e=m.current.getBoundingClientRect();g.current=e.left+t.getWindowScrollLeft(),b.current=e.top+t.getWindowScrollTop(),w.current=m.current.offsetWidth,x.current=m.current.offsetHeight},X=function(e){var n=(S?100*((e.touches?e.touches[0].pageX:e.pageX)-g.current)/w.current:100*(b.current+x.current-(e.touches?e.touches[0].pageY:e.pageY))/x.current)/100*(i.max-i.min)+i.min;if(i.step){var r=i.range?E[d.current]:E,t=n-r;t<0?n=r+Math.ceil(n/i.step-r/i.step)*i.step:t>0&&(n=r+Math.floor(n/i.step-r/i.step)*i.step)}else n=Math.floor(n);return B(e,n)},B=function(e,n){var r,t=parseFloat(n.toFixed(10)),a=t;return i.range?(0===d.current?t<i.min?t=i.min:t>E[1]&&(t=E[1]):t>i.max?t=i.max:t<E[0]&&(t=E[0]),(a=l(r=E)||u(r)||c(r)||s())[d.current]=t,i.onChange&&i.onChange({originalEvent:e,value:a})):(t<i.min?t=i.min:t>i.max&&(t=i.max),a=t,i.onChange&&i.onChange({originalEvent:e,value:a})),a},$=function(n,r,t){var l={transition:y.current?"none":null,left:null!==n&&n+"%",bottom:r&&r+"%"},u=a("p-slider-handle",{"p-slider-handle-start":0===t,"p-slider-handle-end":1===t,"p-slider-handle-active":d.current===t});return e.createElement("span",o({className:u,style:l,tabIndex:i.tabIndex,role:"slider",onMouseDown:function(e){return _(0,t)},onTouchStart:function(e){return H(0,t)},onKeyDown:function(e){return L(e,t)},"aria-valuemin":i.min,"aria-valuemax":i.max,"aria-valuenow":n||r,"aria-orientation":i.orientation},Z))};e.useImperativeHandle(f,(function(){return{props:i,getElement:function(){return m.current}}}));var q,G,J,Q,V=r.findDiffKeys(i,v.defaultProps),Z=r.reduceKeys(V,t.ARIA_PROPS),ee=a("p-slider p-component",i.className,{"p-disabled":i.disabled,"p-slider-horizontal":S,"p-slider-vertical":A}),ne=i.range?(q=100*(E[0]<i.min?0:E[0]-i.min)/(i.max-i.min),G=100*(E[1]>i.max?100:E[1]-i.min)/(i.max-i.min),J=S?$(q,null,0):$(null,q,0),Q=S?$(G,null,1):$(null,G,1),e.createElement(e.Fragment,null,e.createElement("span",{className:"p-slider-range",style:S?{left:q+"%",width:G-q+"%"}:{bottom:q+"%",height:G-q+"%"}}),J,Q)):function(){var n;n=E<i.min?0:E>i.max?100:100*(E-i.min)/(i.max-i.min);var r=S?{width:n+"%"}:{height:n+"%"},t=S?$(n,null,null):$(null,n,null);return e.createElement(e.Fragment,null,e.createElement("span",{className:"p-slider-range",style:r}),t)}();return e.createElement("div",o({ref:m,id:i.id,style:i.style,className:ee},V,{onClick:function(e){if(!i.disabled){if(!h.current){U();var n=X(e);i.onSlideEnd&&i.onSlideEnd({originalEvent:e,value:n})}h.current=!1}}}),ne)})));v.displayName="Slider",v.defaultProps={__TYPE:"Slider",id:null,value:null,min:0,max:100,orientation:"horizontal",step:null,range:!1,style:null,className:null,disabled:!1,tabIndex:0,onChange:null,onSlideEnd:null};export{v as Slider};