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) • 5.76 kB
JavaScript
import*as e from"react";import{ObjectUtils as n,classNames as t,DomHandler as r}from"primereact/utils";function l(){return l=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},l.apply(this,arguments)}function o(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}function a(e){if(Array.isArray(e))return o(e)}function i(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 o(e,n);var t=Object.prototype.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)?o(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.")}var d={defaultProps:{__TYPE:"OrganizationChart",id:null,value:null,style:null,className:null,selectionMode:null,selection:null,nodeTemplate:null,onSelectionChange:null,onNodeSelect:null,onNodeUnselect:null,children:void 0},getProps:function(e){return n.getMergedProps(e,d.defaultProps)},getOtherProps:function(e){return n.getDiffProps(e,d.defaultProps)}};function u(e){if(Array.isArray(e))return e}function p(e,n){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var r,l,o,a,i=[],c=!0,s=!1;try{if(o=(t=t.call(e)).next,0===n){if(Object(t)!==t)return;c=!1}else for(;!(c=(r=o.call(t)).done)&&(i.push(r.value),i.length!==n);c=!0);}catch(e){s=!0,l=e}finally{try{if(!c&&null!=t.return&&(a=t.return(),Object(a)!==a))return}finally{if(s)throw l}}return i}}function m(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f=e.memo((function(r){var l,o,a,i,s,d,h=r.node,g=e.useState(h.expanded),v=(o=2,u(l=g)||p(l,o)||c(l,o)||m()),y=v[0],b=v[1],N=!1!==h.leaf&&!(h.children&&h.children.length),E=h.children&&h.children.length?2*h.children.length:null,S=r.isSelected(h),z=!N&&y?"inherit":"hidden",C=function(e,n){r.onNodeClick(e,n)},M=function(){if(!N){var n=t("p-node-toggler-icon",{"pi pi-chevron-down":y,"pi pi-chevron-up":!y});return e.createElement("a",{href:"#",className:"p-node-toggler",onClick:function(e){return n=e,b((function(e){return!e})),void n.preventDefault();var n}},e.createElement("i",{className:n}))}return null},O=function(){var t=r.nodeTemplate&&n.getJSXElement(r.nodeTemplate,h)||h.label;return e.createElement("div",null,t)},w=(a=t("p-organizationchart-node-content",{"p-organizationchart-selectable-node":r.selectionMode&&!1!==h.selectable,"p-highlight":S},h.className),i=O(),s=M(),e.createElement("tr",null,e.createElement("td",{colSpan:E},e.createElement("div",{className:a,style:h.style,onClick:function(e){return C(e,h)}},i,s)))),P=e.createElement("tr",{style:{visibility:z},className:"p-organizationchart-lines"},e.createElement("td",{colSpan:E},e.createElement("div",{className:"p-organizationchart-line-down"}))),T=(d=h.children&&h.children.length,e.createElement("tr",{style:{visibility:z},className:"p-organizationchart-lines"},h.children&&1===h.children.length&&e.createElement("td",{colSpan:E},e.createElement("div",{className:"p-organizationchart-line-down"})),h.children&&h.children.length>1&&h.children.map((function(n,r){var l=t("p-organizationchart-line-left",{"p-organizationchart-line-top":0!==r}),o=t("p-organizationchart-line-right",{"p-organizationchart-line-top":r!==d-1});return[e.createElement("td",{key:r+"_lineleft",className:l}," "),e.createElement("td",{key:r+"_lineright",className:o}," ")]})))),j=e.createElement("tr",{style:{visibility:z},className:"p-organizationchart-nodes"},h.children&&h.children.map((function(n,t){return e.createElement("td",{key:t,colSpan:"2"},e.createElement(f,{node:n,nodeTemplate:r.nodeTemplate,selectionMode:r.selectionMode,onNodeClick:r.onNodeClick,isSelected:r.isSelected}))})));return e.createElement("table",{className:"p-organizationchart-table"},e.createElement("tbody",null,w,P,T,j))}));f.displayName="OrganizationChartNode";var h=e.memo(e.forwardRef((function(n,o){var u=d.getProps(n),p=e.useRef(null),m=u.value&&u.value.length?u.value[0]:null,h=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(o,(function(){return{props:u,getElement:function(){return p.current}}}));var g=d.getOtherProps(u),v=t("p-organizationchart p-component",u.className);return e.createElement("div",l({id:u.id,ref:p,style:u.style,className:v},g),e.createElement(f,{node:m,nodeTemplate:u.nodeTemplate,selectionMode:u.selectionMode,onNodeClick:function(e,n){if(u.selectionMode){var t=e.target;if(!1===n.selectable||r.hasClass(t,"p-node-toggler")||r.hasClass(t,"p-node-toggler-icon"))return;var l,o=h(n),d=o>=0;"single"===u.selectionMode?d?(l=null,u.onNodeUnselect&&u.onNodeUnselect({originalEvent:e,node:n})):(l=n,u.onNodeSelect&&u.onNodeSelect({originalEvent:e,node:n})):"multiple"===u.selectionMode&&(d?(l=u.selection.filter((function(e,n){return n!==o})),u.onNodeUnselect&&u.onNodeUnselect({originalEvent:e,node:n})):(l=[].concat(a(p=u.selection||[])||i(p)||c(p)||s(),[n]),u.onNodeSelect&&u.onNodeSelect({originalEvent:e,node:n}))),u.onSelectionChange&&u.onSelectionChange({originalEvent:e,data:l})}var p},isSelected:function(e){return-1!==h(e)}}))})));h.displayName="OrganizationChart";export{h as OrganizationChart};