UNPKG

react-dropdown-z

Version:
2 lines (1 loc) 7.82 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("@delpi/common"),require("rc-tc-ifn"),require("react-if-vz"),require("react-perfect-scrollbar-z")):"function"==typeof define&&define.amd?define(["exports","react","@delpi/common","rc-tc-ifn","react-if-vz","react-perfect-scrollbar-z"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactDropDown={},e.React,e.common,e.TooltipIfn,e.reactIfVz,e.Scrollbar)}(this,(function(e,t,l,a,n,o){"use strict";function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=r(t),u=r(a),d=r(o),s=function(){return s=Object.assign||function(e){for(var t,l=1,a=arguments.length;l<a;l++)for(var n in t=arguments[l])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},s.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var c=function(e){return"string"==typeof e&&""!==e.trim()},f=function(e,t,a,n){if(l.CmUtils.isNil(e))return{value:"",label:""};if((null==e?void 0:e.isGroup)&&Array.isArray(e.items)){var o=e.items.map((function(e){return f(e,t,a)})).find((function(e){return e.value===n}));if(o)return o}var r="string"==typeof e?e:JSON.stringify(e);if(!c(a))return{value:r,label:r};var i=l.CmUtils.isNil(null==e?void 0:e[a])?r:e[a];return{value:i,label:l.CmUtils.isNil(null==e?void 0:e[t])?i:e[t]}},m=function(e){var t=e.item,a=e.labelName,o=void 0===a?"":a,r=e.keyName,d=void 0===r?"":r,c=e.tabIndex,m=e.selectValue,p=e.tooltipIfDropdownProps,v=e.handleSelectValue,b=f(t,o,d),E=b.value,w=b.label,h=E===m;return i.default.createElement("button",{tabIndex:c,className:l.HtmlUtils.joinClass("dropdown2-item",p&&"dropdown2-item-tooltip",h&&"dropdown2-item-selected",t.className,t.disabled&&"dropdown2-item-disabled"),onClick:function(e){e.preventDefault(),!t.disabled&&v(t)},role:"option","aria-selected":h?"true":"false"},i.default.createElement(n.Ifz,null,i.default.createElement(n.Ifz.If,{condition:Boolean(p)},i.default.createElement(u.default,s({width:"100%",right:!0,tagName:"span"},p),w)),i.default.createElement(n.Ifz.Else,null,i.default.createElement("span",null,w))))},p=function(e){var t=e.options,a=e.groupItemClassName,o=e.tabIndex,r=e.keyName,u=e.labelName,d=e.selectValue,s=e.tooltipIfDropdownProps,c=e.handleSelectValue;return i.default.createElement(i.default.Fragment,null,t.map((function(e,t){var f;return i.default.createElement(n.Ifz,{key:t},i.default.createElement(n.If,{condition:e.isGroup},i.default.createElement(i.default.Fragment,null,i.default.createElement("div",{className:l.HtmlUtils.joinClass("dropdown2-group-item",a,e.className)},e.groupName),null===(f=e.items)||void 0===f?void 0:f.map((function(e,t){return i.default.createElement(m,{key:t,item:e,keyName:r,labelName:u,tabIndex:o,selectValue:d,tooltipIfDropdownProps:s,handleSelectValue:c})})))),i.default.createElement(n.Else,null,i.default.createElement(m,{item:e,keyName:r,labelName:u,tabIndex:o,selectValue:d,tooltipIfDropdownProps:s,handleSelectValue:c})))})))};e.DropDownItem=m,e.DropdownListItem=p,e.default=function(e){var a,o,r=e.className,m=e.arrowClassName,v=e.groupItemClassName,b=e.dropdownClassName,E=e.placeholderClassName,w=e.showAbove,h=e.options,N=e.keyName,g=void 0===N?"":N,I=e.labelName,y=void 0===I?"":I,x=e.value,C=e.customizeArrow,S=e.placeholder,H=void 0===S?"":S,k=e.noDataText,z=void 0===k?"No option":k,D=e.width,V=void 0===D?"100%":D,j=e.height,P=void 0===j?"40px":j,O=e.fullWidth,U=void 0===O||O,T=e.enablePerfectScroll,A=void 0===T||T,R=e.tabIndex,q=void 0===R?-1:R,W=e.disabled,F=e.dropdownHeight,L=e.fitToWindowHeight,B=void 0===L||L,M=e.autoAdjustDirection,G=void 0===M||M,_=e.spacingVerticalOffset,J=void 0===_?-2:_,Y=e.preserveScrollPosition,K=void 0===Y||Y,Q=e.closeOnResize,X=void 0===Q||Q,Z=e.closeOnEscape,$=void 0===Z||Z,ee=e.closeOnOutside,te=void 0===ee||ee,le=e.tooltipIfLabelProps,ae=e.tooltipIfDropdownProps,ne=e.handleSelection,oe=void 0===ne?function(){}:ne,re=l.Hooks.useWindowSize(),ie=t.useRef(null),ue=t.useRef(null),de=t.useRef(null),se=t.useState(!1),ce=se[0],fe=se[1],me=t.useState(null),pe=me[0],ve=me[1],be=t.useState({value:null,label:i.default.createElement(i.default.Fragment,null)}),Ee=be[0],we=be[1],he=Array.isArray(h)&&h.length>0;t.useEffect((function(){var e=h.map((function(e){return f(e,y,g,x)})),t=c(g)?e.find((function(e){return e[g]===x})):e.find((function(e){return e.value===x}));we(t?{value:t.value,label:t.label}:{value:null,label:i.default.createElement(i.default.Fragment,null)})}),[x,y,g,h]),t.useEffect((function(){X&&ce&&Ne()}),[re]),l.Hooks.useEventListener("scroll",(function(){return Ne()})),l.Hooks.useEventListener("keydown",(function(e){$&&"Escape"===e.key&&Ne()})),l.Hooks.useOutsideClick(ie,(function(){return te&&Ne()}));var Ne=function(){ve(null),fe(!1)},ge=function(e){var t=f(e,y,g),l=t.value,a=t.label;we({value:l,label:a}),oe(l,e),fe(!1),ve(null)};return i.default.createElement("div",{ref:ie,className:l.HtmlUtils.joinClass("dp-dropdown2-container",r),style:{width:U?V:"auto",height:P}},i.default.createElement("button",{className:l.HtmlUtils.joinClass("dropdown2-button",!he&&"dropdown2-button-no-data"),disabled:W,"aria-haspopup":"listbox",onClick:function(){he&&fe((function(e){var t=!e;return t||ve(null),t}))}},i.default.createElement(n.Ifz,null,i.default.createElement(n.If,{condition:!he},i.default.createElement("span",{className:"dropdown2-label dropdown2-no-message"},z)),i.default.createElement(n.ElseIf,{condition:l.CmUtils.isNil(null==Ee?void 0:Ee.value)},i.default.createElement("span",{className:l.HtmlUtils.joinClass("dropdown2-label dropdown2-placeholder",E)},H)),i.default.createElement(n.ElseIf,{condition:Boolean(le)},i.default.createElement(u.default,s({width:"100%",right:!0,tagName:"span",className:"dropdown2-label dropdown2-selected"},le),Ee.label)),i.default.createElement(n.Else,null,i.default.createElement("span",{className:"dropdown2-label dropdown2-selected"},Ee.label))),i.default.createElement("span",{className:"dropdown2-anything"}),i.default.createElement("span",{className:l.HtmlUtils.joinClass("dropdown2-arrow",m,ce&&"dropdown2-arrow-active")},C)),i.default.createElement(n.When,{condition:ce},i.default.createElement("div",{ref:function(e){if(e instanceof Element&&(K&&setTimeout((function(){var t=e.querySelector(".scroll-content");t&&(t.scrollTop=de.current||0)}),10),B&&ie.current)){var t=ie.current.getBoundingClientRect(),l=t.bottom,a=t.top,n=2*t.height+4,o=Math.round(a)-8,r=Math.round(window.innerHeight-l)-8,i=w?o:r,u=e.clientHeight;if(!G)return u>i&&ve("".concat(u=i,"px")),void(w&&(e.style.top="".concat(-u-4,"px")));u>i&&(u=i),u<n?(w?i=r:(e.style.top="".concat(-e.clientHeight-4,"px"),i=o),(u=e.clientHeight)>i&&(u=i)):w&&(e.style.top="".concat(-u-4,"px")),ve("".concat(u,"px"))}},tabIndex:q,className:l.HtmlUtils.joinClass("dropdown2-list-items",b),style:{top:(null===(a=ie.current)||void 0===a?void 0:a.offsetHeight)+J,width:U?null===(o=ie.current)||void 0===o?void 0:o.offsetWidth:"auto"},"aria-expanded":"true"},i.default.createElement(n.Ifz,null,i.default.createElement(n.Ifz.If,{condition:A},i.default.createElement(d.default,{style:{maxHeight:pe||F},effectData:[h,F,B,G,w],always:!0,wheelStop:!0,onScrollY:function(e){de.current=e.target.scrollTop}},i.default.createElement(p,{groupItemClassName:v,options:h,tabIndex:q,keyName:g,labelName:y,tooltipIfDropdownProps:ae,selectValue:Ee.value,handleSelectValue:ge}))),i.default.createElement(n.Ifz.Else,null,i.default.createElement("div",{ref:ue,className:"dropdown2-list-default scroll-content",style:{maxHeight:pe||F},onScroll:function(e){de.current=e.target.scrollTop}},i.default.createElement(p,{groupItemClassName:v,options:h,tabIndex:q,keyName:g,labelName:y,tooltipIfDropdownProps:ae,selectValue:Ee.value,handleSelectValue:ge})))))))},Object.defineProperty(e,"__esModule",{value:!0})}));