primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 14.1 kB
JavaScript
import{ObjectUtils as e,DomHandler as t,ZIndexUtils as i,ConnectedOverlayScrollHandler as n}from"primevue/utils";import l from"primevue/overlayeventbus";import o from"primevue/ripple";import{resolveComponent as s,resolveDirective as a,openBlock as r,createBlock as p,Fragment as c,renderList as d,withDirectives as h,createVNode as u,resolveDynamicComponent as b,toDisplayString as v,createCommentVNode as y,renderSlot as f,createTextVNode as m,Teleport as g,Transition as O,withCtx as L}from"vue";var w={name:"CascadeSelectSub",emits:["option-select","optiongroup-select"],props:{selectionPath:Array,level:Number,options:Array,optionLabel:String,optionValue:String,optionGroupLabel:String,optionGroupChildren:Array,parentActive:Boolean,dirty:Boolean,templates:null,root:Boolean},data:()=>({activeOption:null}),mounted(){if(this.selectionPath&&this.options&&!this.dirty)for(let e of this.options)if(this.selectionPath.includes(e)){this.activeOption=e;break}this.root||this.position()},watch:{parentActive(e){e||(this.activeOption=null)}},methods:{onOptionClick(e,t){this.isOptionGroup(t)?(this.activeOption=this.activeOption===t?null:t,this.$emit("optiongroup-select",{originalEvent:e,value:t})):this.$emit("option-select",{originalEvent:e,value:this.getOptionValue(t)})},onOptionSelect(e){this.$emit("option-select",e)},onOptionGroupSelect(e){this.$emit("optiongroup-select",e)},getOptionLabel(t){return this.optionLabel?e.resolveFieldData(t,this.optionLabel):t},getOptionValue(t){return this.optionValue?e.resolveFieldData(t,this.optionValue):t},getOptionGroupLabel(t){return this.optionGroupLabel?e.resolveFieldData(t,this.optionGroupLabel):null},getOptionGroupChildren(t){return e.resolveFieldData(t,this.optionGroupChildren[this.level])},isOptionGroup(e){return Object.prototype.hasOwnProperty.call(e,this.optionGroupChildren[this.level])},getOptionLabelToRender(e){return this.isOptionGroup(e)?this.getOptionGroupLabel(e):this.getOptionLabel(e)},getItemClass(e){return["p-cascadeselect-item",{"p-cascadeselect-item-group":this.isOptionGroup(e),"p-cascadeselect-item-active p-highlight":this.isOptionActive(e)}]},isOptionActive(e){return this.activeOption===e},onKeyDown(e,t,i){switch(e.key){case"Down":case"ArrowDown":var n=this.$el.children[i+1];n&&n.children[0].focus();break;case"Up":case"ArrowUp":var l=this.$el.children[i-1];l&&l.children[0].focus();break;case"Right":case"ArrowRight":this.isOptionGroup(t)&&(this.isOptionActive(t)?e.currentTarget.nextElementSibling.children[0].children[0].focus():this.activeOption=t);break;case"Left":case"ArrowLeft":this.activeOption=null;var o=e.currentTarget.parentElement.parentElement.previousElementSibling;o&&o.focus();break;case"Enter":this.onOptionClick(e,t)}e.preventDefault()},position(){const e=this.$el.parentElement,i=t.getOffset(e),n=t.getViewport(),l=this.$el.offsetParent?this.$el.offsetWidth:t.getHiddenElementOuterWidth(this.$el),o=t.getOuterWidth(e.children[0]);parseInt(i.left,10)+o+l>n.width-t.calculateScrollbarWidth()&&(this.$el.style.left="-100%")}},directives:{ripple:o}};const C={class:"p-cascadeselect-panel p-cascadeselect-items",role:"listbox","aria-orientation":"horizontal"},k={key:1,class:"p-cascadeselect-item-text"},S={key:2,class:"p-cascadeselect-group-icon pi pi-angle-right"};w.render=function(e,t,i,n,l,o){const f=s("CascadeSelectSub",!0),m=a("ripple");return r(),p("ul",C,[(r(!0),p(c,null,d(i.options,((e,t)=>(r(),p("li",{key:o.getOptionLabelToRender(e),class:o.getItemClass(e),role:"none"},[h(u("div",{class:"p-cascadeselect-item-content",onClick:t=>o.onOptionClick(t,e),tabindex:"0",onKeydown:i=>o.onKeyDown(i,e,t)},[i.templates.option?(r(),p(b(i.templates.option),{key:0,option:e},null,8,["option"])):(r(),p("span",k,v(o.getOptionLabelToRender(e)),1)),o.isOptionGroup(e)?(r(),p("span",S)):y("",!0)],40,["onClick","onKeydown"]),[[m]]),o.isOptionGroup(e)&&o.isOptionActive(e)?(r(),p(f,{key:0,class:"p-cascadeselect-sublist",selectionPath:i.selectionPath,options:o.getOptionGroupChildren(e),optionLabel:i.optionLabel,optionValue:i.optionValue,level:i.level+1,onOptionSelect:o.onOptionSelect,onOptiongroupSelect:o.onOptionGroupSelect,optionGroupLabel:i.optionGroupLabel,optionGroupChildren:i.optionGroupChildren,parentActive:o.isOptionActive(e),dirty:i.dirty,templates:i.templates},null,8,["selectionPath","options","optionLabel","optionValue","level","onOptionSelect","onOptiongroupSelect","optionGroupLabel","optionGroupChildren","parentActive","dirty","templates"])):y("",!0)],2)))),128))])};var x={name:"CascadeSelect",emits:["update:modelValue","change","group-change","before-show","before-hide","hide","show"],data:()=>({selectionPath:null,focused:!1,overlayVisible:!1,dirty:!1}),props:{modelValue:null,options:Array,optionLabel:String,optionValue:String,optionGroupLabel:String,optionGroupChildren:Array,placeholder:String,disabled:Boolean,dataKey:null,inputId:String,tabindex:String,ariaLabelledBy:null,appendTo:{type:String,default:"body"},panelClass:null,loading:{type:Boolean,default:!1},loadingIcon:{type:String,default:"pi pi-spinner pi-spin"}},outsideClickListener:null,scrollHandler:null,resizeListener:null,overlay:null,beforeUnmount(){this.unbindOutsideClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),this.overlay&&(i.clear(this.overlay),this.overlay=null)},mounted(){this.updateSelectionPath()},watch:{modelValue(){this.updateSelectionPath()}},methods:{onOptionSelect(e){this.$emit("update:modelValue",e.value),this.$emit("change",e),this.hide(),this.$refs.focusInput.focus()},onOptionGroupSelect(e){this.dirty=!0,this.$emit("group-change",e)},getOptionLabel(t){return this.optionLabel?e.resolveFieldData(t,this.optionLabel):t},getOptionValue(t){return this.optionValue?e.resolveFieldData(t,this.optionValue):t},getOptionGroupChildren(t,i){return e.resolveFieldData(t,this.optionGroupChildren[i])},isOptionGroup(e,t){return Object.prototype.hasOwnProperty.call(e,this.optionGroupChildren[t])},updateSelectionPath(){let e;if(null!=this.modelValue&&this.options)for(let t of this.options)if(e=this.findModelOptionInGroup(t,0),e)break;this.selectionPath=e},findModelOptionInGroup(t,i){if(this.isOptionGroup(t,i)){let e;for(let n of this.getOptionGroupChildren(t,i))if(e=this.findModelOptionInGroup(n,i+1),e)return e.unshift(t),e}else if(e.equals(this.modelValue,this.getOptionValue(t),this.dataKey))return[t];return null},show(){this.$emit("before-show"),this.overlayVisible=!0},hide(){this.$emit("before-hide"),this.overlayVisible=!1},onFocus(){this.focused=!0},onBlur(){this.focused=!1},onClick(e){this.disabled||this.loading||this.overlay&&this.overlay.contains(e.target)||(this.overlayVisible?this.hide():this.show(),this.$refs.focusInput.focus())},onOverlayEnter(e){i.set("overlay",e,this.$primevue.config.zIndex.overlay),this.alignOverlay(),this.bindOutsideClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.$emit("show")},onOverlayLeave(){this.unbindOutsideClickListener(),this.unbindScrollListener(),this.unbindResizeListener(),this.$emit("hide"),this.overlay=null,this.dirty=!1},onOverlayAfterLeave(e){i.clear(e)},alignOverlay(){this.appendDisabled?t.relativePosition(this.overlay,this.$el):(this.overlay.style.minWidth=t.getOuterWidth(this.$el)+"px",t.absolutePosition(this.overlay,this.$el))},bindOutsideClickListener(){this.outsideClickListener||(this.outsideClickListener=e=>{this.overlayVisible&&this.overlay&&!this.$el.contains(e.target)&&!this.overlay.contains(e.target)&&this.hide()},document.addEventListener("click",this.outsideClickListener))},unbindOutsideClickListener(){this.outsideClickListener&&(document.removeEventListener("click",this.outsideClickListener),this.outsideClickListener=null)},bindScrollListener(){this.scrollHandler||(this.scrollHandler=new n(this.$refs.container,(()=>{this.overlayVisible&&this.hide()}))),this.scrollHandler.bindScrollListener()},unbindScrollListener(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()},bindResizeListener(){this.resizeListener||(this.resizeListener=()=>{this.overlayVisible&&this.hide()},window.addEventListener("resize",this.resizeListener))},unbindResizeListener(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),this.resizeListener=null)},overlayRef(e){this.overlay=e},onKeyDown(e){switch(e.key){case"Down":case"ArrowDown":this.overlayVisible?t.findSingle(this.overlay,".p-cascadeselect-item").children[0].focus():e.altKey&&this.options&&this.options.length&&this.show(),e.preventDefault();break;case"Escape":this.overlayVisible&&(this.hide(),e.preventDefault());break;case"Tab":this.hide()}},onOverlayClick(e){l.emit("overlay-click",{originalEvent:e,target:this.$el})}},computed:{containerClass(){return["p-cascadeselect p-component p-inputwrapper",{"p-disabled":this.disabled,"p-focus":this.focused,"p-inputwrapper-filled":this.modelValue,"p-inputwrapper-focus":this.focused||this.overlayVisible}]},labelClass(){return["p-cascadeselect-label",{"p-placeholder":this.label===this.placeholder,"p-cascadeselect-label-empty":!this.$slots.value&&("p-emptylabel"===this.label||0===this.label.length)}]},label(){return this.selectionPath?this.getOptionLabel(this.selectionPath[this.selectionPath.length-1]):this.placeholder||"p-emptylabel"},panelStyleClass(){return["p-cascadeselect-panel p-component",this.panelClass,{"p-input-filled":"filled"===this.$primevue.config.inputStyle,"p-ripple-disabled":!1===this.$primevue.config.ripple}]},appendDisabled(){return"self"===this.appendTo},appendTarget(){return this.appendDisabled?null:this.appendTo},dropdownIconClass(){return["p-cascadeselect-trigger-icon",this.loading?this.loadingIcon:"pi pi-chevron-down"]}},components:{CascadeSelectSub:w}};const G={class:"p-hidden-accessible"},V={class:"p-cascadeselect-items-wrapper"};!function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var n=document.head||document.getElementsByTagName("head")[0],l=document.createElement("style");l.type="text/css","top"===i&&n.firstChild?n.insertBefore(l,n.firstChild):n.appendChild(l),l.styleSheet?l.styleSheet.cssText=e:l.appendChild(document.createTextNode(e))}}("\n.p-cascadeselect {\n display: -webkit-inline-box;\n display: -ms-inline-flexbox;\n display: inline-flex;\n cursor: pointer;\n position: relative;\n -webkit-user-select: none;\n -moz-user-select: none;\n -ms-user-select: none;\n user-select: none;\n}\n.p-cascadeselect-trigger {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n -webkit-box-pack: center;\n -ms-flex-pack: center;\n justify-content: center;\n -ms-flex-negative: 0;\n flex-shrink: 0;\n}\n.p-cascadeselect-label {\n display: block;\n white-space: nowrap;\n overflow: hidden;\n -webkit-box-flex: 1;\n -ms-flex: 1 1 auto;\n flex: 1 1 auto;\n width: 1%;\n text-overflow: ellipsis;\n cursor: pointer;\n}\n.p-cascadeselect-label-empty {\n overflow: hidden;\n visibility: hidden;\n}\n.p-cascadeselect .p-cascadeselect-panel {\n min-width: 100%;\n}\n.p-cascadeselect-panel {\n position: absolute;\n}\n.p-cascadeselect-item {\n cursor: pointer;\n font-weight: normal;\n white-space: nowrap;\n}\n.p-cascadeselect-item-content {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n overflow: hidden;\n position: relative;\n}\n.p-cascadeselect-group-icon {\n margin-left: auto;\n}\n.p-cascadeselect-items {\n margin: 0;\n padding: 0;\n list-style-type: none;\n min-width: 100%;\n}\n.p-fluid .p-cascadeselect {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n.p-fluid .p-cascadeselect .p-cascadeselect-label {\n width: 1%;\n}\n.p-cascadeselect-sublist {\n position: absolute;\n min-width: 100%;\n z-index: 1;\n display: none;\n}\n.p-cascadeselect-item-active {\n overflow: visible !important;\n}\n.p-cascadeselect-item-active > .p-cascadeselect-sublist {\n display: block;\n left: 100%;\n top: 0;\n}\n"),x.render=function(e,t,i,n,l,o){const a=s("CascadeSelectSub");return r(),p("div",{ref:"container",class:o.containerClass,onClick:t[5]||(t[5]=e=>o.onClick(e))},[u("div",G,[u("input",{ref:"focusInput",type:"text",id:i.inputId,readonly:"",disabled:i.disabled,onFocus:t[1]||(t[1]=(...e)=>o.onFocus&&o.onFocus(...e)),onBlur:t[2]||(t[2]=(...e)=>o.onBlur&&o.onBlur(...e)),onKeydown:t[3]||(t[3]=(...e)=>o.onKeyDown&&o.onKeyDown(...e)),tabindex:i.tabindex,"aria-haspopup":"listbox","aria-expanded":l.overlayVisible,"aria-labelledby":i.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),u("span",{class:o.labelClass},[f(e.$slots,"value",{value:i.modelValue,placeholder:i.placeholder},(()=>[m(v(o.label),1)]))],2),u("div",{class:"p-cascadeselect-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":l.overlayVisible},[u("span",{class:o.dropdownIconClass},null,2)],8,["aria-expanded"]),(r(),p(g,{to:o.appendTarget,disabled:o.appendDisabled},[u(O,{name:"p-connected-overlay",onEnter:o.onOverlayEnter,onLeave:o.onOverlayLeave,onAfterLeave:o.onOverlayAfterLeave},{default:L((()=>[l.overlayVisible?(r(),p("div",{key:0,ref:o.overlayRef,class:o.panelStyleClass,onClick:t[4]||(t[4]=(...e)=>o.onOverlayClick&&o.onOverlayClick(...e))},[u("div",V,[u(a,{options:i.options,selectionPath:l.selectionPath,optionLabel:i.optionLabel,optionValue:i.optionValue,level:0,templates:e.$slots,optionGroupLabel:i.optionGroupLabel,optionGroupChildren:i.optionGroupChildren,onOptionSelect:o.onOptionSelect,onOptiongroupSelect:o.onOptionGroupSelect,dirty:l.dirty,root:!0},null,8,["options","selectionPath","optionLabel","optionValue","templates","optionGroupLabel","optionGroupChildren","onOptionSelect","onOptiongroupSelect","dirty"])])],2)):y("",!0)])),_:1},8,["onEnter","onLeave","onAfterLeave"])],8,["to","disabled"]))],2)};export default x;