primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ 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) • 8.39 kB
JavaScript
import*as e from"react";import{useEventListener as t}from"primereact/hooks";import{ObjectUtils as r,DomHandler as n,classNames as u}from"primereact/utils";function o(){return o=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},o.apply(this,arguments)}function i(e){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i(e)}function l(e,t){if("object"!==i(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==i(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function c(e){var t=l(e,"string");return"symbol"===i(t)?t:String(t)}function a(e,t,r){return(t=c(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function p(e){if(Array.isArray(e))return s(e)}function f(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function d(e,t){if(e){if("string"==typeof e)return s(e,t);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)?s(e,t):void 0}}function m(){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 y(e){if(Array.isArray(e))return e}function g(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,u,o,i,l=[],c=!0,a=!1;try{if(o=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=o.call(r)).done)&&(l.push(n.value),l.length!==t);c=!0);}catch(e){a=!0,u=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(a)throw u}}return l}}function h(){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 v(e,t){return y(e)||g(e,t)||d(e,t)||h()}var b={defaultProps:{__TYPE:"Splitter",className:null,gutterSize:4,id:null,layout:"horizontal",onResizeEnd:null,stateKey:null,stateStorage:"session",style:null,children:void 0},getProps:function(e){return r.getMergedProps(e,b.defaultProps)},getOtherProps:function(e){return r.getDiffProps(e,b.defaultProps)}},S={defaultProps:{__TYPE:"SplitterPanel",className:null,minSize:null,size:null,style:null,children:void 0},getCProps:function(e){return r.getComponentProps(e,S.defaultProps)},getCOtherProps:function(e){return r.getComponentDiffProps(e,S.defaultProps)},getCProp:function(e,t){return r.getComponentProp(e,t,S.defaultProps)}};function P(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function O(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?P(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):P(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var w=function(){},z=e.memo(e.forwardRef((function(i,l){var c=b.getProps(i),a=e.useRef(null),s=e.useRef(),y=e.useRef({}),g=e.useRef(null),h=e.useRef(null),P=e.useRef(null),w=e.useRef(null),z=e.useRef(null),E=e.useRef(null),j=e.useRef(null),C=e.useRef(null),R=e.useRef(null),N=e.useRef(null),x=v(e.useState([]),2),A=x[0],I=x[1],T=null!=c.stateKey,D=c.children&&c.children.length||1,Y=function(e,t){return t in e?e[t]:c.children&&[].concat(c.children)[t].props.size||100/D},k=v(t({type:"mousemove",listener:function(e){return q(e)}}),2),K=k[0],H=k[1],L=v(t({type:"mouseup",listener:function(e){G(e),_()}}),2),M=L[0],X=L[1],_=function(){H(),X()},B=function(e,t){return S.getCProp(e,t)},W=function(e,t){return!(e>100||e<0)&&(!(t>100||t<0)&&(!(c.children[N.current].props&&c.children[N.current].props.minSize&&c.children[N.current].props.minSize>e)&&!(c.children[N.current+1].props&&c.children[N.current+1].props.minSize&&c.children[N.current+1].props.minSize>t)))},J=e.useCallback((function(){switch(c.stateStorage){case"local":return window.localStorage;case"session":return window.sessionStorage;default:throw new Error(c.stateStorage+' is not a valid value for the state storage, supported values are "local" and "session".')}}),[c.stateStorage]),F=function(e){J().setItem(c.stateKey,JSON.stringify(e))},U=e.useCallback((function(){var e=J().getItem(c.stateKey);e&&I(JSON.parse(e))}),[J,c.stateKey]),$=function(e,t){s.current=y.current[t];var r="touchstart"===e.type?e.touches[0].pageX:e.pageX,u="touchstart"===e.type?e.touches[0].pageY:e.pageY;g.current="horizontal"===c.layout?n.getWidth(a.current):n.getHeight(a.current),h.current=!0,P.current="horizontal"===c.layout?r:u,w.current=s.current.previousElementSibling,z.current=s.current.nextElementSibling,E.current=100*("horizontal"===c.layout?n.getOuterWidth(w.current,!0):n.getOuterHeight(w.current,!0))/g.current,j.current=E.current,C.current=100*("horizontal"===c.layout?n.getOuterWidth(z.current,!0):n.getOuterHeight(z.current,!0))/g.current,R.current=C.current,N.current=t,n.addClass(s.current,"p-splitter-gutter-resizing"),n.addClass(a.current,"p-splitter-resizing")},q=function(e){var t,r=E.current+(t="horizontal"===c.layout?100*("touchmove"===e.type?e.touches[0].pageX:e.pageX)/g.current-100*P.current/g.current:100*("touchmove"===e.type?e.touches[0].pageY:e.pageY)/g.current-100*P.current/g.current),n=C.current-t;W(r,n)&&(j.current=r,R.current=n,w.current.style.flexBasis="calc("+r+"% - "+(c.children.length-1)*c.gutterSize+"px)",z.current.style.flexBasis="calc("+n+"% - "+(c.children.length-1)*c.gutterSize+"px)")},G=function(e){I((function(t){for(var r=[],n=0;n<c.children.length;n++)r[n]=Y(t,n);return r[N.current]=j.current,r[N.current+1]=R.current,c.onResizeEnd&&c.onResizeEnd({originalEvent:e,sizes:r}),T&&F(r),r})),n.removeClass(s.current,"p-splitter-gutter-resizing"),n.removeClass(a.current,"p-splitter-resizing"),h.current=!1,g.current=null,P.current=null,w.current=null,z.current=null,E.current=null,j.current=null,C.current=null,R.current=null,N.current=null},Q=function(e,t){$(e,t),K(),M()},V=function(e,t){$(e,t),window.addEventListener("touchmove",Z,{passive:!1,cancelable:!1}),window.addEventListener("touchend",ee)},Z=function(e){q(e)},ee=function e(t){G(t),window.removeEventListener("touchmove",Z),window.removeEventListener("touchend",e)};e.useImperativeHandle(l,(function(){return{props:c,getElement:function(){return a.current}}})),e.useEffect((function(){var e;(e=a.current.children,p(e)||f(e)||d(e)||m()).filter((function(e){return n.hasClass(e,"p-splitter-panel")})).map((function(e){e.childNodes&&r.isNotEmpty(n.find(e,".p-splitter"))&&n.addClass(e,"p-splitter-panel-nested")}))}),[]),e.useEffect((function(){T&&U()}),[U,T]);var te=function(t,r){var n=S.getCOtherProps(t),i=u("p-splitter-panel",B(t,"className")),l=r!==c.children.length-1&&e.createElement("div",{ref:function(e){return y.current[r]=e},className:"p-splitter-gutter",style:"horizontal"===c.layout?{width:c.gutterSize+"px"}:{height:c.gutterSize+"px"},onMouseDown:function(e){return Q(e,r)},onTouchStart:function(e){return V(e,r)},onTouchMove:function(e){return Z(e)},onTouchEnd:function(e){return ee(e)}},e.createElement("div",{className:"p-splitter-gutter-handle"})),a="calc("+Y(A,r)+"% - "+(D-1)*c.gutterSize+"px)";return e.createElement(e.Fragment,null,e.createElement("div",o({key:r,className:i,style:O(O({},B(t,"style")),{},{flexBasis:a})},n),B(t,"children")),l)},re=b.getOtherProps(c),ne=u("p-splitter p-component p-splitter-".concat(c.layout),c.className),ue=e.Children.map(c.children,te);return e.createElement("div",o({ref:a,id:c.id,className:ne,style:c.style},re),ue)})));w.displayName="SplitterPanel",z.displayName="Splitter";export{z as Splitter,w as SplitterPanel};