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) • 10.1 kB
JavaScript
import*as e from"react";import{PrimeReactContext as n}from"primereact/api";import{ComponentBase as t,useHandleStyle as r}from"primereact/componentbase";import{useMergeProps as o}from"primereact/hooks";import{classNames as l,IconUtils as i,ObjectUtils as a,DomHandler as c}from"primereact/utils";import{ChevronDownIcon as s}from"primereact/icons/chevrondown";import{ChevronUpIcon as u}from"primereact/icons/chevronup";function d(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=Array(n);t<n;t++)r[t]=e[t];return r}function p(e){if(Array.isArray(e))return d(e)}function m(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function g(e,n){if(e){if("string"==typeof e)return d(e,n);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?d(e,n):void 0}}function f(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h=t.extend({defaultProps:{__TYPE:"OrganizationChart",id:null,value:null,style:null,className:null,selectionMode:null,selection:null,nodeTemplate:null,onSelectionChange:null,onNodeSelect:null,onNodeUnselect:null,togglerIcon:null,children:void 0},css:{classes:{root:"p-organizationchart p-component",table:"p-organizationchart-table",node:function(e){var n=e.node;return l("p-organizationchart-node-content",{"p-organizationchart-selectable-node":e.nodeProps.selectionMode&&!1!==n.selectable,"p-highlight":e.selected},n.className)},nodes:"p-organizationchart-nodes",lines:"p-organizationchart-lines",lineLeft:function(e){return l("p-organizationchart-line-left",{"p-organizationchart-line-top":0!==e.index})},lineRight:function(e){return l("p-organizationchart-line-right",{"p-organizationchart-line-top":e.index!==e.nodeChildLength-1})},lineDown:"p-organizationchart-line-down",nodeTogglerIcon:"p-node-toggler-icon",nodeToggler:"p-node-toggler"},styles:"\n@layer primereact {\n .p-organizationchart-table {\n border-spacing: 0;\n border-collapse: separate;\n margin: 0 auto;\n }\n \n .p-organizationchart-table > tbody > tr > td {\n text-align: center;\n vertical-align: top;\n padding: 0 .75rem;\n }\n \n .p-organizationchart-node-content {\n display: inline-block;\n position: relative;\n }\n \n .p-organizationchart-node-content .p-node-toggler {\n position: absolute;\n bottom: -.75rem;\n margin-left: -.75rem;\n z-index: 2;\n left: 50%;\n user-select: none;\n cursor: pointer;\n width: 1.5rem;\n height: 1.5rem;\n text-decoration: none;\n }\n \n .p-organizationchart-node-content .p-node-toggler .p-node-toggler-icon {\n position: relative;\n top: .25rem;\n }\n \n .p-organizationchart-line-down {\n margin: 0 auto;\n height: 20px;\n width: 1px;\n }\n \n .p-organizationchart-line-right {\n border-radius: 0px;\n }\n \n .p-organizationchart-line-left {\n border-radius: 0;\n }\n \n .p-organizationchart-selectable-node {\n cursor: pointer;\n }\n}\n"}});function y(){return y=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)({}).hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},y.apply(null,arguments)}function b(e){return b="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},b(e)}function v(e,n){if("object"!=b(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,n||"default");if("object"!=b(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===n?String:Number)(e)}function E(e){var n=v(e,"string");return"symbol"==b(n)?n:n+""}function S(e,n,t){return(n=E(n))in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function N(e){if(Array.isArray(e))return e}function x(e,n){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var r,o,l,i,a=[],c=!0,s=!1;try{if(l=(t=t.call(e)).next,0===n){if(Object(t)!==t)return;c=!1}else for(;!(c=(r=l.call(t)).done)&&(a.push(r.value),a.length!==n);c=!0);}catch(e){s=!0,o=e}finally{try{if(!c&&null!=t.return&&(i=t.return(),Object(i)!==i))return}finally{if(s)throw o}}return a}}function O(){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 w(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter(function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable})),t.push.apply(t,r)}return t}function z(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?w(Object(t),!0).forEach(function(n){S(e,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):w(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))})}return e}var j=function(e,n){for(var t=[],r=0;r<e.length;r+=n)t.push(e.slice(r,r+n));return t},C=e.memo(function(n){var t,r,l,c,d,p,m,f=o(),h=n.node,b=e.useState(h.expanded),v=(r=2,N(t=b)||x(t,r)||g(t,r)||O()),E=v[0],S=v[1],w=!1!==h.leaf&&!(h.children&&h.children.length),I=n.isSelected(h),P=!w&&E?"inherit":"hidden",T=n.ptm,k=n.cx,M=n.sx,D=function(e,t){return T(e,z({hostName:n.hostName},t))},A=function(e){return D(e,{state:{expanded:E},context:{selected:n.isSelected(h)}})},U=function(e,n){return D(n,{context:{lineTop:e}})},L=function(e,t){n.onNodeClick(e,t)},R=function(e,n){S(function(e){return!e}),e.preventDefault()},F=function(){if(!w){var t=f({className:k("nodeTogglerIcon")},D("nodeTogglerIcon")),r=i.getJSXIcon(E?n.togglerIcon||e.createElement(s,t):n.togglerIcon||e.createElement(u,t),z({},t),{props:n}),o=f({className:k("nodeToggler"),tabIndex:0,onKeyDown:function(e){var n;"Enter"!==(n=e).code&&"NumpadEnter"!==n.code&&"Space"!==n.code||(R(n),n.preventDefault())},onClick:function(e){return R(e)},href:"#"},A("nodeToggler"));return e.createElement("a",o,e.createElement("i",null," ",r," "))}return null},J=function(){var t=n.nodeTemplate&&a.getJSXElement(n.nodeTemplate,h)||h.label;return e.createElement("div",null,t)},X=(l=J(),c=F(),d=f({colSpan:h.children&&h.children.length?2*Math.min(h.children.length,10):2},D("cell")),p=f({className:k("node",{selected:I,node:h,nodeProps:n}),style:h.style,onClick:function(e){return L(e,h)}},A("node")),m=f(D("row")),e.createElement("tr",m,e.createElement("td",d,e.createElement("div",p,l,c)))),_=f({className:k("table")},D("table"));return e.createElement("table",_,e.createElement("tbody",null,X,h.children&&!1!==h.expanded?j(h.children,10).map(function(t,r){return e.createElement(e.Fragment,{key:r},h.children&&!1!==h.expanded?j(h.children,10).map(function(n,t){var r=2*n.length,o=f({className:k("lines"),style:{visibility:P}},D("lines")),l=f({colSpan:r},D("lineCell")),i=f({className:k("lineDown")},D("lineDown"));return e.createElement("tr",y({},o,{key:t}),e.createElement("td",l,e.createElement("div",i)))}):null,h.children&&!1!==h.expanded?j(h.children,10).map(function(n,t){var r=n.length,o=f({className:k("lines"),style:{visibility:P}},D("lines"));return e.createElement("tr",y({},o,{key:t}),n.map(function(n,t){var o=f({className:k("lineLeft",{index:t})},U(0!==t,"lineLeft")),l=f({className:k("lineRight",{index:t,nodeChildLength:r})},U(t!==r-1,"lineRight"));return e.createElement(e.Fragment,{key:t},e.createElement("td",o," "),e.createElement("td",l," "))}))}):null,h.children&&!1!==h.expanded?j(h.children,10).map(function(t,r){var o=f({className:k("nodes"),style:{visibility:P}},D("nodes")),l=f({colSpan:"2"},D("nodeCell"));return e.createElement("tr",y({},o,{key:r}),t.map(function(t,r){return e.createElement("td",y({key:r},l),e.createElement(C,{node:t,nodeTemplate:n.nodeTemplate,selectionMode:n.selectionMode,onNodeClick:n.onNodeClick,isSelected:n.isSelected,togglerIcon:n.togglerIcon,ptm:T,cx:k,sx:M}))}))}):null)}):null))});C.displayName="OrganizationChartNode";var I=e.memo(e.forwardRef(function(t,i){var a=o(),s=e.useContext(n),u=h.getProps(t,s),d=h.setMetaData({props:u}),y=d.ptm,b=d.cx,v=d.sx;r(h.css.styles,d.isUnstyled,{name:"orgchart"});var E=e.useRef(null),S=u.value&&u.value.length?u.value[0]:null,N=function(e){if(u.selectionMode&&u.selection){if("single"===u.selectionMode)return u.selection===e?0:-1;if("multiple"===u.selectionMode)return u.selection.findIndex(function(n){return n===e})}return-1};e.useImperativeHandle(i,function(){return{props:u,getElement:function(){return E.current}}});var x=a({id:u.id,ref:E,style:u.style,className:l(u.className,b("root"))},h.getOtherProps(u),y("root"));return e.createElement("div",x,e.createElement(C,{hostName:"OrganizationChart",node:S,nodeTemplate:u.nodeTemplate,selectionMode:u.selectionMode,onNodeClick:function(e,n){if(u.selectionMode){var t=e.target;if(!1===n.selectable||c.hasClass(t,"p-node-toggler")||c.hasClass(t,"p-node-toggler-icon"))return;var r,o=N(n),l=o>=0;"single"===u.selectionMode?l?(r=null,u.onNodeUnselect&&u.onNodeUnselect({originalEvent:e,node:n})):(r=n,u.onNodeSelect&&u.onNodeSelect({originalEvent:e,node:n})):"multiple"===u.selectionMode&&(l?(r=u.selection.filter(function(e,n){return n!==o}),u.onNodeUnselect&&u.onNodeUnselect({originalEvent:e,node:n})):(r=[].concat(p(i=u.selection||[])||m(i)||g(i)||f(),[n]),u.onNodeSelect&&u.onNodeSelect({originalEvent:e,node:n}))),u.onSelectionChange&&u.onSelectionChange({originalEvent:e,data:r})}var i},isSelected:function(e){return-1!==N(e)},togglerIcon:u.togglerIcon,ptm:y,cx:b,sx:v}))}));I.displayName="OrganizationChart";export{I as OrganizationChart};