2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
import e,{Component as t,createRef as n}from"react";import{classNames as r,ObjectUtils as i,DomHandler as o,ZIndexUtils as l,ConnectedOverlayScrollHandler as s}from"primereact/utils";import a,{localeOption as p}from"primereact/api";import{OverlayService as c}from"primereact/overlayservice";import{Tree as u}from"primereact/tree";import{CSSTransition as h}from"primereact/csstransition";import{Portal as d}from"primereact/portal";import{Ripple as f}from"primereact/ripple";function y(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function v(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function m(e,t,n){return t&&v(e.prototype,t),n&&v(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function b(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function g(e,t){return g=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},g(e,t)}function k(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&g(e,t)}function E(e){return E="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},E(e)}function O(e,t){if(t&&("object"===E(t)||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return b(e)}function S(e){return S=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},S(e)}function w(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function C(){return C=Object.assign||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},C.apply(this,arguments)}function N(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var x=function(n){k(s,t);var i,o,l=(i=s,o=N(),function(){var e,t=S(i);if(o){var n=S(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return O(this,e)});function s(){return y(this,s),l.apply(this,arguments)}return m(s,[{key:"renderElement",value:function(){var t=r("p-treeselect-panel p-component",this.props.panelClassName);return e.createElement(h,{nodeRef:this.props.forwardRef,classNames:"p-connected-overlay",in:this.props.in,timeout:{enter:120,exit:100},options:this.props.transitionOptions,unmountOnExit:!0,onEnter:this.props.onEnter,onEntering:this.props.onEntering,onEntered:this.props.onEntered,onExit:this.props.onExit,onExited:this.props.onExited},e.createElement("div",{ref:this.props.forwardRef,className:t,style:this.props.panelStyle,onClick:this.props.onClick},this.props.header,e.createElement("div",{className:"p-treeselect-items-wrapper",style:{maxHeight:this.props.scrollHeight||"auto"}},this.props.children),this.props.footer))}},{key:"render",value:function(){var t=this.renderElement();return e.createElement(d,{element:t,appendTo:this.props.appendTo})}}]),s}(),I=e.forwardRef((function(t,n){return e.createElement(x,C({forwardRef:n},t))}));function L(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=V(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,i=function(){};return{s:i,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,l=!0,s=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return l=e.done,e},e:function(e){s=!0,o=e},f:function(){try{l||null==n.return||n.return()}finally{if(s)throw o}}}}function V(e,t){if(e){if("string"==typeof e)return F(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)?F(e,t):void 0}}function F(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 R(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function j(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?R(Object(n),!0).forEach((function(t){w(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):R(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function T(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}var D=function(h){k(E,t);var d,v,g=(d=E,v=T(),function(){var e,t=S(d);if(v){var n=S(this).constructor;e=Reflect.construct(t,arguments,n)}else e=t.apply(this,arguments);return O(this,e)});function E(e){var t;return y(this,E),(t=g.call(this,e)).state={focused:!1,overlayVisible:!1,expandedKeys:{}},t.props.onFilterValueChange||(t.state.filterValue=""),t.onClick=t.onClick.bind(b(t)),t.onInputFocus=t.onInputFocus.bind(b(t)),t.onInputBlur=t.onInputBlur.bind(b(t)),t.onInputKeyDown=t.onInputKeyDown.bind(b(t)),t.onFilterInputChange=t.onFilterInputChange.bind(b(t)),t.onFilterInputKeyDown=t.onFilterInputKeyDown.bind(b(t)),t.onOverlayClick=t.onOverlayClick.bind(b(t)),t.onOverlayEnter=t.onOverlayEnter.bind(b(t)),t.onOverlayEntered=t.onOverlayEntered.bind(b(t)),t.onOverlayExit=t.onOverlayExit.bind(b(t)),t.onOverlayExited=t.onOverlayExited.bind(b(t)),t.onSelectionChange=t.onSelectionChange.bind(b(t)),t.onNodeSelect=t.onNodeSelect.bind(b(t)),t.onNodeUnselect=t.onNodeUnselect.bind(b(t)),t.onNodeToggle=t.onNodeToggle.bind(b(t)),t.onFilterValueChange=t.onFilterValueChange.bind(b(t)),t.hide=t.hide.bind(b(t)),t.show=t.show.bind(b(t)),t.overlayRef=n(),t}return m(E,[{key:"getFilterValue",value:function(){return this.props.onFilterValueChange?this.props.filterValue:this.state.filterValue}},{key:"getSelectedNodes",value:function(){var e=[];if(i.isNotEmpty(this.props.value)&&this.props.options){var t="single"===this.props.selectionMode?w({},"".concat(this.props.value),!0):j({},this.props.value);this.findSelectedNodes(null,t,e)}return e}},{key:"getLabel",value:function(){var e=this.getSelectedNodes();return e.length?e.map((function(e){return e.label})).join(", "):this.props.placeholder}},{key:"isValueEmpty",value:function(){return!this.props.value||0===Object.keys(this.props.value).length}},{key:"hasNoOptions",value:function(){return!this.props.options||0===this.props.options.length}},{key:"show",value:function(){this.setState({overlayVisible:!0})}},{key:"hide",value:function(){this.setState({overlayVisible:!1})}},{key:"onInputFocus",value:function(){this.setState({focused:!0})}},{key:"onInputBlur",value:function(){this.setState({focused:!1})}},{key:"onClick",value:function(e){this.props.disabled||this.overlayRef&&this.overlayRef.current&&this.overlayRef.current.contains(e.target)||o.hasClass(e.target,"p-treeselect-close")||(this.focusInput.focus(),this.state.overlayVisible?this.hide():this.show())}},{key:"onSelectionChange",value:function(e){this.props.onChange&&(this.selfChange=!0,this.props.onChange({originalEvent:e.originalEvent,value:e.value,stopPropagation:function(){},preventDefault:function(){},target:{name:this.props.name,id:this.props.id,value:e.value}}))}},{key:"onNodeSelect",value:function(e){this.props.onNodeSelect&&this.props.onNodeSelect(e),"single"===this.props.selectionMode&&this.hide()}},{key:"onNodeUnselect",value:function(e){this.props.onNodeUnselect&&this.props.onNodeUnselect(e)}},{key:"onNodeToggle",value:function(e){this.setState({expandedKeys:e.value})}},{key:"onFilterValueChange",value:function(e){this.setState({filterValue:e.value})}},{key:"onOverlayClick",value:function(e){c.emit("overlay-click",{originalEvent:e,target:this.container})}},{key:"onInputKeyDown",value:function(e){switch(e.which){case 40:!this.state.overlayVisible&&e.altKey&&this.show();break;case 32:this.state.overlayVisible||(this.show(),e.preventDefault());break;case 13:case 27:this.state.overlayVisible&&(this.hide(),e.preventDefault());break;case 9:this.hide()}}},{key:"onFilterInputKeyDown",value:function(e){13===e.which&&e.preventDefault()}},{key:"onFilterInputChange",value:function(e){var t=e.target.value;this.props.onFilterValueChange?this.props.onFilterValueChange({originalEvent:e,value:t}):this.setState({filterValue:t})}},{key:"resetFilter",value:function(){this.setState({filterValue:""})}},{key:"onOverlayEnter",value:function(){l.set("overlay",this.overlayRef.current,a.autoZIndex,a.zIndex.overlay),this.alignOverlay(),this.scrollInView()}},{key:"onOverlayEntered",value:function(){this.bindDocumentClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.props.filter&&this.props.filterInputAutoFocus&&this.filterInput.focus(),this.props.onShow&&this.props.onShow()}},{key:"onOverlayExit",value:function(){this.unbindDocumentClickListener(),this.unbindScrollListener(),this.unbindResizeListener()}},{key:"onOverlayExited",value:function(){this.props.filter&&this.props.resetFilterOnHide&&this.resetFilter(),l.clear(this.overlayRef.current),this.props.onHide&&this.props.onHide()}},{key:"alignOverlay",value:function(){o.alignOverlay(this.overlayRef.current,this.trigger.parentElement,this.props.appendTo||a.appendTo)}},{key:"scrollInView",value:function(){var e=o.findSingle(this.overlayRef.current,".p-treenode-content.p-highlight");e&&e.scrollIntoView({block:"nearest",inline:"start"})}},{key:"bindDocumentClickListener",value:function(){var e=this;this.documentClickListener||(this.documentClickListener=function(t){e.state.overlayVisible&&e.isOutsideClicked(t)&&e.hide()},document.addEventListener("click",this.documentClickListener))}},{key:"unbindDocumentClickListener",value:function(){this.documentClickListener&&(document.removeEventListener("click",this.documentClickListener),this.documentClickListener=null)}},{key:"bindScrollListener",value:function(){var e=this;this.scrollHandler||(this.scrollHandler=new s(this.container,(function(){e.state.overlayVisible&&e.hide()}))),this.scrollHandler.bindScrollListener()}},{key:"unbindScrollListener",value:function(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()}},{key:"bindResizeListener",value:function(){var e=this;this.resizeListener||(this.resizeListener=function(){e.state.overlayVisible&&!o.isTouchDevice()&&e.hide()},window.addEventListener("resize",this.resizeListener))}},{key:"unbindResizeListener",value:function(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),this.resizeListener=null)}},{key:"isOutsideClicked",value:function(e){return this.container&&!(this.container.isSameNode(e.target)||this.container.contains(e.target)||this.overlayRef&&this.overlayRef.current.contains(e.target))}},{key:"findSelectedNodes",value:function(e,t,n){if(e){if(this.isSelected(e,t)&&(n.push(e),delete t[e.key]),Object.keys(t).length&&e.children){var r,i=L(e.children);try{for(i.s();!(r=i.n()).done;){this.findSelectedNodes(r.value,t,n)}}catch(e){i.e(e)}finally{i.f()}}}else{var o,l=L(this.props.options);try{for(l.s();!(o=l.n()).done;){this.findSelectedNodes(o.value,t,n)}}catch(e){l.e(e)}finally{l.f()}}}},{key:"isSelected",value:function(e,t){return"checkbox"===this.props.selectionMode?t[e.key]&&t[e.key].checked:t[e.key]}},{key:"updateTreeState",value:function(){var e="single"===this.props.selectionMode?w({},"".concat(this.props.value),!0):j({},this.props.value);this.setState({expandedKeys:{}}),e&&this.props.options&&this.updateTreeBranchState(null,null,e)}},{key:"updateTreeBranchState",value:function(e,t,n){if(e){if(this.isSelected(e,n)&&(this.expandPath(t),delete n[e.key]),Object.keys(n).length&&e.children){var r,i=L(e.children);try{for(i.s();!(r=i.n()).done;){var o=r.value;t.push(e.key),this.updateTreeBranchState(o,t,n)}}catch(e){i.e(e)}finally{i.f()}}}else{var l,s=L(this.props.options);try{for(s.s();!(l=s.n()).done;){this.updateTreeBranchState(l.value,[],n)}}catch(e){s.e(e)}finally{s.f()}}}},{key:"expandPath",value:function(e){if(e.length>0){var t,n=j({},this.state.expandedKeys||{}),r=L(e);try{for(r.s();!(t=r.n()).done;){n[t.value]=!0}}catch(e){r.e(e)}finally{r.f()}this.setState({expandedKeys:n})}}},{key:"componentDidMount",value:function(){this.updateTreeState()}},{key:"componentDidUpdate",value:function(e,t){this.state.overlayVisible&&((this.props.filter||t.expandedKeys!==this.state.expandedKeys)&&this.alignOverlay(),e.value!==this.props.value&&(this.selfChange||this.updateTreeState(),this.scrollInView(),this.selfChange=!1)),e.options!==this.props.options&&this.updateTreeState()}},{key:"componentWillUnmount",value:function(){this.unbindDocumentClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),l.clear(this.overlayRef.current)}},{key:"renderKeyboardHelper",value:function(){var t=this;return e.createElement("div",{className:"p-hidden-accessible"},e.createElement("input",{ref:function(e){return t.focusInput=e},role:"listbox",id:this.props.inputId,type:"text",readOnly:!0,"aria-haspopup":"true","aria-expanded":this.state.overlayVisible,onFocus:this.onInputFocus,onBlur:this.onInputBlur,onKeyDown:this.onInputKeyDown,disabled:this.props.disabled,tabIndex:this.props.tabIndex,"aria-label":this.props.ariaLabel,"aria-labelledby":this.props.ariaLabelledBy}))}},{key:"renderLabel",value:function(t){var n=this.isValueEmpty(),o=r("p-treeselect-label",{"p-placeholder":this.getLabel()===this.props.placeholder,"p-treeselect-label-empty":!this.props.placeholder&&n}),l=null;if(this.props.valueTemplate)l=i.getJSXElement(this.props.valueTemplate,t,this.props);else if("comma"===this.props.display)l=this.getLabel()||"empty";else if("chip"===this.props.display){var s=this.getSelectedNodes();l=e.createElement(e.Fragment,null,s&&s.map((function(t,n){return e.createElement("div",{className:"p-treeselect-token",key:"".concat(t.key,"_").concat(n)},e.createElement("span",{className:"p-treeselect-token-label"},t.label))})),n&&(this.props.placeholder||"empty"))}return e.createElement("div",{className:"p-treeselect-label-container"},e.createElement("div",{className:o},l))}},{key:"renderDropdownIcon",value:function(){var t=this,n=r("p-treeselect-trigger-icon p-clickable",this.props.dropdownIcon);return e.createElement("div",{ref:function(e){return t.trigger=e},className:"p-treeselect-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":this.state.overlayVisible},e.createElement("span",{className:n}))}},{key:"renderContent",value:function(){var t=this.getFilterValue();return e.createElement(e.Fragment,null,e.createElement(u,{value:this.props.options,selectionMode:this.props.selectionMode,selectionKeys:this.props.value,metaKeySelection:this.props.metaKeySelection,onSelectionChange:this.onSelectionChange,onSelect:this.onNodeSelect,onUnselect:this.onNodeUnselect,expandedKeys:this.state.expandedKeys,onToggle:this.onNodeToggle,onExpand:this.props.onNodeExpand,onCollapse:this.props.onNodeCollapse,filter:this.props.filter,filterValue:t,filterBy:this.props.filterBy,filterMode:this.props.filterMode,filterPlaceholder:this.props.filterPlaceholder,filterLocale:this.props.filterLocale,showHeader:!1,onFilterValueChange:this.onFilterValueChange}),this.hasNoOptions()&&e.createElement("div",{className:"p-treeselect-empty-message"},this.props.emptyMessage||p("emptyMessage")))}},{key:"renderFilterElement",value:function(){var t=this;if(this.props.filter){var n=this.getFilterValue();return n=i.isNotEmpty(n)?n:"",e.createElement("div",{className:"p-treeselect-filter-container"},e.createElement("input",{ref:function(e){return t.filterInput=e},type:"text",value:n,autoComplete:"off",className:"p-treeselect-filter p-inputtext p-component",placeholder:this.props.filterPlaceholder,onKeyDown:this.onFilterInputKeyDown,onChange:this.onFilterInputChange,disabled:this.props.disabled}),e.createElement("span",{className:"p-treeselect-filter-icon pi pi-search"}))}return null}},{key:"renderHeader",value:function(){var t=this.renderFilterElement(),n=e.createElement("button",{type:"button",className:"p-treeselect-close p-link",onClick:this.hide},e.createElement("span",{className:"p-treeselect-close-icon pi pi-times"}),e.createElement(f,null)),r=e.createElement("div",{className:"p-treeselect-header"},t,n);return this.props.header?i.getJSXElement(this.props.header,{className:"p-treeselect-header",filterElement:t,closeElement:n,closeElementClassName:"p-treeselect-close p-link",closeIconClassName:"p-treeselect-close-icon pi pi-times",onCloseClick:this.hide,element:r,props:this.props}):r}},{key:"render",value:function(){var t=this,n=r("p-treeselect p-component p-inputwrapper",{"p-treeselect-chip":"chip"===this.props.display,"p-disabled":this.props.disabled,"p-focus":this.state.focused,"p-inputwrapper-filled":!this.isValueEmpty(),"p-inputwrapper-focus":this.state.focused||this.state.overlayVisible},this.props.className),o=this.getSelectedNodes(),l=this.renderKeyboardHelper(),s=this.renderLabel(o),a=this.renderDropdownIcon(),p=this.renderContent(),c=this.renderHeader(),u=i.getJSXElement(this.props.footer,this.props);return e.createElement("div",{id:this.props.id,ref:function(e){return t.container=e},className:n,style:this.props.style,onClick:this.onClick},l,s,a,e.createElement(I,{ref:this.overlayRef,appendTo:this.props.appendTo,panelStyle:this.props.panelStyle,panelClassName:this.props.panelClassName,scrollHeight:this.props.scrollHeight,onClick:this.onOverlayClick,header:c,footer:u,transitionOptions:this.props.transitionOptions,in:this.state.overlayVisible,onEnter:this.onOverlayEnter,onEntered:this.onOverlayEntered,onExit:this.onOverlayExit,onExited:this.onOverlayExited},p))}}]),E}();w(D,"defaultProps",{id:null,value:null,name:null,style:null,className:null,disabled:!1,options:null,scrollHeight:"400px",placeholder:null,tabIndex:null,inputId:null,ariaLabel:null,ariaLabelledBy:null,selectionMode:"single",panelStyle:null,panelClassName:null,appendTo:null,emptyMessage:null,display:"comma",metaKeySelection:!0,valueTemplate:null,panelHeaderTemplate:null,panelFooterTemplate:null,transitionOptions:null,dropdownIcon:"pi pi-chevron-down",filter:!1,filterValue:null,filterBy:"label",filterMode:"lenient",filterPlaceholder:null,filterLocale:void 0,filterInputAutoFocus:!0,resetFilterOnHide:!1,onShow:null,onHide:null,onChange:null,onNodeSelect:null,onNodeUnselect:null,onNodeExpand:null,onNodeCollapse:null,onFilterValueChange:null});export{D as TreeSelect};
|