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) • 6.51 kB
JavaScript
import*as e from"react";import t from"primereact/api";import{useEventListener as n,useMountEffect as r,useUpdateEffect as l,useUnmountEffect as o}from"primereact/hooks";import{ObjectUtils as a,classNames as u,IconUtils as i,DomHandler as c,ZIndexUtils as s}from"primereact/utils";import{Ripple as m}from"primereact/ripple";function f(){return f=Object.assign?Object.assign.bind():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},f.apply(this,arguments)}function p(e){if(Array.isArray(e))return e}function d(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,l,o,a,u=[],i=!0,c=!1;try{if(o=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;i=!1}else for(;!(i=(r=o.call(n)).done)&&(u.push(r.value),u.length!==t);i=!0);}catch(e){c=!0,l=e}finally{try{if(!i&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(c)throw l}}return u}}function b(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function v(e,t){if(e){if("string"==typeof e)return b(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?b(e,t):void 0}}function g(){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 h(e,t){return p(e)||d(e,t)||v(e,t)||g()}var y={defaultProps:{__TYPE:"Menubar",id:null,model:null,style:null,className:null,start:null,end:null,children:void 0},getProps:function(e){return a.getMergedProps(e,y.defaultProps)},getOtherProps:function(e){return a.getDiffProps(e,y.defaultProps)}},E=e.memo(e.forwardRef((function(t,o){var s=h(e.useState(null),2),f=s[0],p=s[1],d=h(n({type:"click",listener:function(e){o&&o.current&&!o.current.contains(e.target)&&p(null)}}),1)[0],b=function(e,n){n.disabled||t.mobileActive?e.preventDefault():t.root?(f||t.popup)&&p(n):p(n)},v=function(e,t){t.disabled?e.preventDefault():(t.url||e.preventDefault(),t.command&&t.command({originalEvent:e,item:t}),t.items?p(f&&t===f?null:t):D())},g=function(e,n){var r=e.currentTarget.parentElement;switch(e.which){case 40:t.root?n.items&&N(n,r):w(r),e.preventDefault();break;case 38:!t.root&&k(r),e.preventDefault();break;case 39:if(t.root){var l=S(r);l&&l.children[0].focus()}else n.items&&N(n,r);e.preventDefault();break;case 37:t.root&&k(r),e.preventDefault()}t.onKeyDown&&t.onKeyDown(e,r)},y=function(e,n){t.root?38===e.which&&null==n.previousElementSibling&&C(n):37===e.which&&C(n)},N=function(e,t){p(e),setTimeout((function(){t.children[1].children[0].children[0].focus()}),50)},C=function(e){p(null),e.parentElement.previousElementSibling.focus()},w=function(e){var t=S(e);t&&t.children[0].focus()},k=function(e){var t=P(e);t&&t.children[0].focus()},S=function e(t){var n=t.nextElementSibling;return n?c.hasClass(n,"p-disabled")||!c.hasClass(n,"p-menuitem")?e(n):n:null},P=function e(t){var n=t.previousElementSibling;return n?c.hasClass(n,"p-disabled")||!c.hasClass(n,"p-menuitem")?e(n):n:null},D=function(){p(null),t.onLeafClick&&t.onLeafClick()};r((function(){d()})),l((function(){!t.parentActive&&p(null)}),[t.parentActive]);var A=function(t){return e.createElement("li",{key:"separator_"+t,className:"p-menu-separator",role:"separator"})},O=function(n){return n.items?e.createElement(E,{menuProps:t.menuProps,model:n.items,mobileActive:t.mobileActive,onLeafClick:D,onKeyDown:y,parentActive:n===f}):null},I=function(n,r){if(!1===n.visible)return null;var l=n.label+"_"+r,o=u("p-menuitem",{"p-menuitem-active":f===n},n.className),c=u("p-menuitem-link",{"p-disabled":n.disabled}),s=u("p-menuitem-icon",n.icon),p=u("p-submenu-icon pi",{"pi-angle-down":t.root,"pi-angle-right":!t.root}),d=i.getJSXIcon(n.icon,{className:"p-menuitem-icon"},{props:t.menuProps}),h=n.label&&e.createElement("span",{className:"p-menuitem-text"},n.label),y=n.items&&e.createElement("span",{className:p}),E=O(n),N=e.createElement("a",{href:n.url||"#",role:"menuitem",className:c,target:n.target,"aria-haspopup":null!=n.items,onClick:function(e){return v(e,n)},onKeyDown:function(e){return g(e,n)}},d,h,y,e.createElement(m,null));n.template&&(N=a.getJSXElement(n.template,n,{onClick:function(e){return v(e,n)},onKeyDown:function(e){return g(e,n)},className:c,labelClassName:"p-menuitem-text",iconClassName:s,submenuIconClassName:p,element:N,props:t}));return e.createElement("li",{key:l,role:"none",id:n.id,className:o,style:n.style,onMouseEnter:function(e){return b(e,n)}},N,E)},j=function(e,t){return e.separator?A(t):I(e,t)},x=t.root?"menubar":"menu",M=u({"p-submenu-list":!t.root,"p-menubar-root-list":t.root}),R=t.model?t.model.map(j):null;return e.createElement("ul",{ref:o,className:M,role:x},R)})));E.displayName="MenubarSub";var N=e.memo(e.forwardRef((function(r,i){var c=y.getProps(r),m=h(e.useState(!1),2),p=m[0],d=m[1],b=e.useRef(null),v=e.useRef(null),g=e.useRef(null),N=h(n({type:"click",listener:function(e){p&&S(e)&&d(!1)}}),2),C=N[0],w=N[1],k=function(e){e.preventDefault(),d((function(e){return!e}))},S=function(e){return v.current!==e.target&&!v.current.contains(e.target)&&g.current!==e.target&&!g.current.contains(e.target)};l((function(){p?(s.set("menu",v.current,t.autoZIndex,t.zIndex.menu),C()):(w(),s.clear(v.current))}),[p]),o((function(){s.clear(v.current)})),e.useImperativeHandle(i,(function(){return{props:c,toggle:k,useCustomContent:useCustomContent,getElement:function(){return b.current},getRootMenu:function(){return v.current},getMenuButton:function(){return g.current}}}));var P=y.getOtherProps(c),D=u("p-menubar p-component",{"p-menubar-mobile-active":p},c.className),A=function(){if(c.start){var t=a.getJSXElement(c.start,c);return e.createElement("div",{className:"p-menubar-start"},t)}return null}(),O=function(){if(c.end){var t=a.getJSXElement(c.end,c);return e.createElement("div",{className:"p-menubar-end"},t)}return null}(),I=c.model&&c.model.length<1?null:e.createElement("a",{ref:g,href:"#",role:"button",tabIndex:0,className:"p-menubar-button",onClick:k},e.createElement("i",{className:"pi pi-bars"})),j=e.createElement(E,{ref:v,menuProps:c,model:c.model,root:!0,mobileActive:p,onLeafClick:function(){d(!1)}});return e.createElement("div",f({id:c.id,className:D,style:c.style},P),A,I,j,O)})));N.displayName="Menubar";export{N as Menubar};