primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 19.1 kB
JavaScript
import{ZIndexUtils as e,ObjectUtils as i,DomHandler as t,ConnectedOverlayScrollHandler as l}from"primevue/utils";import n from"primevue/overlayeventbus";import{FilterService as o}from"primevue/api";import s from"primevue/ripple";import r from"primevue/virtualscroller";import{resolveComponent as a,resolveDirective as p,openBlock as d,createBlock as h,createVNode as u,createCommentVNode as c,renderSlot as b,createTextVNode as f,toDisplayString as v,Teleport as g,Transition as y,withCtx as m,withDirectives as O,vModelText as w,mergeProps as L,createSlots as V,Fragment as x,renderList as C}from"vue";var S={name:"Dropdown",emits:["update:modelValue","before-show","before-hide","show","hide","change","filter","focus","blur"],props:{modelValue:null,options:Array,optionLabel:null,optionValue:null,optionDisabled:null,optionGroupLabel:null,optionGroupChildren:null,scrollHeight:{type:String,default:"200px"},filter:Boolean,filterPlaceholder:String,filterLocale:String,filterMatchMode:{type:String,default:"contains"},filterFields:{type:Array,default:null},editable:Boolean,placeholder:String,disabled:Boolean,dataKey:null,showClear:Boolean,inputId:String,tabindex:String,ariaLabelledBy:null,appendTo:{type:String,default:"body"},emptyFilterMessage:{type:String,default:null},emptyMessage:{type:String,default:null},panelClass:null,loading:{type:Boolean,default:!1},loadingIcon:{type:String,default:"pi pi-spinner pi-spin"},virtualScrollerOptions:{type:Object,default:null}},data:()=>({focused:!1,filterValue:null,overlayVisible:!1}),watch:{modelValue(){this.isModelValueChanged=!0}},outsideClickListener:null,scrollHandler:null,resizeListener:null,searchTimeout:null,currentSearchChar:null,previousSearchChar:null,searchValue:null,overlay:null,itemsWrapper:null,virtualScroller:null,isModelValueChanged:!1,updated(){this.overlayVisible&&this.isModelValueChanged&&this.scrollValueInView(),this.isModelValueChanged=!1},beforeUnmount(){this.unbindOutsideClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),this.itemsWrapper=null,this.overlay&&(e.clear(this.overlay),this.overlay=null)},methods:{getOptionIndex(e,i){return this.virtualScrollerDisabled?e:i&&i(e).index},getOptionLabel(e){return this.optionLabel?i.resolveFieldData(e,this.optionLabel):e},getOptionValue(e){return this.optionValue?i.resolveFieldData(e,this.optionValue):e},getOptionRenderKey(e){return this.dataKey?i.resolveFieldData(e,this.dataKey):this.getOptionLabel(e)},isOptionDisabled(e){return!!this.optionDisabled&&i.resolveFieldData(e,this.optionDisabled)},getOptionGroupRenderKey(e){return i.resolveFieldData(e,this.optionGroupLabel)},getOptionGroupLabel(e){return i.resolveFieldData(e,this.optionGroupLabel)},getOptionGroupChildren(e){return i.resolveFieldData(e,this.optionGroupChildren)},getSelectedOption(){let e=this.getSelectedOptionIndex();return-1!==e?this.optionGroupLabel?this.getOptionGroupChildren(this.visibleOptions[e.group])[e.option]:this.visibleOptions[e]:null},getSelectedOptionIndex(){if(null!=this.modelValue&&this.visibleOptions){if(!this.optionGroupLabel)return this.findOptionIndexInList(this.modelValue,this.visibleOptions);for(let e=0;e<this.visibleOptions.length;e++){let i=this.findOptionIndexInList(this.modelValue,this.getOptionGroupChildren(this.visibleOptions[e]));if(-1!==i)return{group:e,option:i}}}return-1},findOptionIndexInList(e,t){for(let l=0;l<t.length;l++)if(i.equals(e,this.getOptionValue(t[l]),this.equalityKey))return l;return-1},isSelected(e){return i.equals(this.modelValue,this.getOptionValue(e),this.equalityKey)},show(){this.$emit("before-show"),this.overlayVisible=!0},hide(){this.$emit("before-hide"),this.overlayVisible=!1},onFocus(e){this.focused=!0,this.$emit("focus",e)},onBlur(e){this.focused=!1,this.$emit("blur",e)},onKeyDown(e){switch(e.which){case 40:this.onDownKey(e);break;case 38:this.onUpKey(e);break;case 32:this.overlayVisible||(this.show(),e.preventDefault());break;case 13:case 27:this.overlayVisible&&(this.hide(),e.preventDefault());break;case 9:this.hide();break;default:this.search(e)}},onFilterKeyDown(e){switch(e.which){case 40:this.onDownKey(e);break;case 38:this.onUpKey(e);break;case 13:case 27:this.overlayVisible=!1,e.preventDefault()}},onDownKey(e){if(this.visibleOptions)if(!this.overlayVisible&&e.altKey)this.show();else{let i=this.visibleOptions&&this.visibleOptions.length>0?this.findNextOption(this.getSelectedOptionIndex()):null;i&&this.updateModel(e,this.getOptionValue(i))}e.preventDefault()},onUpKey(e){if(this.visibleOptions){let i=this.findPrevOption(this.getSelectedOptionIndex());i&&this.updateModel(e,this.getOptionValue(i))}e.preventDefault()},findNextOption(e){if(this.optionGroupLabel){let i=-1===e?0:e.group,t=-1===e?-1:e.option,l=this.findNextOptionInList(this.getOptionGroupChildren(this.visibleOptions[i]),t);return l||(i+1!==this.visibleOptions.length?this.findNextOption({group:i+1,option:-1}):null)}return this.findNextOptionInList(this.visibleOptions,e)},findNextOptionInList(e,i){let t=i+1;if(t===e.length)return null;let l=e[t];return this.isOptionDisabled(l)?this.findNextOptionInList(t):l},findPrevOption(e){if(-1===e)return null;if(this.optionGroupLabel){let i=e.group,t=e.option,l=this.findPrevOptionInList(this.getOptionGroupChildren(this.visibleOptions[i]),t);return l||(i>0?this.findPrevOption({group:i-1,option:this.getOptionGroupChildren(this.visibleOptions[i-1]).length}):null)}return this.findPrevOptionInList(this.visibleOptions,e)},findPrevOptionInList(e,i){let t=i-1;if(t<0)return null;let l=e[t];return this.isOptionDisabled(l)?this.findPrevOption(t):l},onClearClick(e){this.updateModel(e,null)},onClick(e){this.disabled||this.loading||t.hasClass(e.target,"p-dropdown-clear-icon")||"INPUT"===e.target.tagName||this.overlay&&this.overlay.contains(e.target)||(this.overlayVisible?this.hide():this.show(),this.$refs.focusInput.focus())},onOptionSelect(e,i){let t=this.getOptionValue(i);this.updateModel(e,t),this.$refs.focusInput.focus(),setTimeout((()=>{this.hide()}),200)},onEditableInput(e){this.$emit("update:modelValue",e.target.value)},onOverlayEnter(i){if(e.set("overlay",i,this.$primevue.config.zIndex.overlay),this.alignOverlay(),this.bindOutsideClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.scrollValueInView(),this.filter&&this.$refs.filterInput.focus(),!this.virtualScrollerDisabled){const e=this.getSelectedOptionIndex();-1!==e&&this.virtualScroller.scrollToIndex(e)}this.$emit("show")},onOverlayLeave(){this.unbindOutsideClickListener(),this.unbindScrollListener(),this.unbindResizeListener(),this.$emit("hide"),this.itemsWrapper=null,this.overlay=null},onOverlayAfterLeave(i){e.clear(i)},alignOverlay(){this.appendDisabled?t.relativePosition(this.overlay,this.$el):(this.overlay.style.minWidth=t.getOuterWidth(this.$el)+"px",t.absolutePosition(this.overlay,this.$el))},updateModel(e,i){this.$emit("update:modelValue",i),this.$emit("change",{originalEvent:e,value:i})},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 l(this.$refs.container,(()=>{this.overlayVisible&&this.hide()}))),this.scrollHandler.bindScrollListener()},unbindScrollListener(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()},bindResizeListener(){this.resizeListener||(this.resizeListener=()=>{this.overlayVisible&&!t.isTouchDevice()&&this.hide()},window.addEventListener("resize",this.resizeListener))},unbindResizeListener(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),this.resizeListener=null)},search(e){if(!this.visibleOptions)return;this.searchTimeout&&clearTimeout(this.searchTimeout);const i=e.key;if(this.previousSearchChar=this.currentSearchChar,this.currentSearchChar=i,this.previousSearchChar===this.currentSearchChar?this.searchValue=this.currentSearchChar:this.searchValue=this.searchValue?this.searchValue+i:i,this.searchValue){let i=this.getSelectedOptionIndex(),t=this.optionGroupLabel?this.searchOptionInGroup(i):this.searchOption(++i);t&&this.updateModel(e,this.getOptionValue(t))}this.searchTimeout=setTimeout((()=>{this.searchValue=null}),250)},searchOption(e){let i;return this.searchValue&&(i=this.searchOptionInRange(e,this.visibleOptions.length),i||(i=this.searchOptionInRange(0,e))),i},searchOptionInRange(e,i){for(let t=e;t<i;t++){let e=this.visibleOptions[t];if(this.matchesSearchValue(e))return e}return null},searchOptionInGroup(e){let i=-1===e?{group:0,option:-1}:e;for(let e=i.group;e<this.visibleOptions.length;e++){let t=this.getOptionGroupChildren(this.visibleOptions[e]);for(let l=i.group===e?i.option+1:0;l<t.length;l++)if(this.matchesSearchValue(t[l]))return t[l]}for(let e=0;e<=i.group;e++){let t=this.getOptionGroupChildren(this.visibleOptions[e]);for(let l=0;l<(i.group===e?i.option:t.length);l++)if(this.matchesSearchValue(t[l]))return t[l]}return null},matchesSearchValue(e){return this.getOptionLabel(e).toLocaleLowerCase(this.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale))},onFilterChange(e){this.$emit("filter",{originalEvent:e,value:e.target.value})},onFilterUpdated(){this.overlayVisible&&this.alignOverlay()},overlayRef(e){this.overlay=e},itemsWrapperRef(e){this.itemsWrapper=e},virtualScrollerRef(e){this.virtualScroller=e},scrollValueInView(){if(this.overlay){let e=t.findSingle(this.overlay,"li.p-highlight");e&&e.scrollIntoView({block:"nearest",inline:"start"})}},onOverlayClick(e){n.emit("overlay-click",{originalEvent:e,target:this.$el})}},computed:{visibleOptions(){if(this.filterValue){if(this.optionGroupLabel){let e=[];for(let i of this.options){let t=o.filter(this.getOptionGroupChildren(i),this.searchFields,this.filterValue,this.filterMatchMode,this.filterLocale);if(t&&t.length){let l={...i};l[this.optionGroupChildren]=t,e.push(l)}}return e}return o.filter(this.options,this.searchFields,this.filterValue,this.filterMatchMode,this.filterLocale)}return this.options},containerClass(){return["p-dropdown p-component p-inputwrapper",{"p-disabled":this.disabled,"p-dropdown-clearable":this.showClear&&!this.disabled,"p-focus":this.focused,"p-inputwrapper-filled":this.modelValue,"p-inputwrapper-focus":this.focused||this.overlayVisible}]},labelClass(){return["p-dropdown-label p-inputtext",{"p-placeholder":this.label===this.placeholder,"p-dropdown-label-empty":!this.$slots.value&&("p-emptylabel"===this.label||0===this.label.length)}]},panelStyleClass(){return["p-dropdown-panel p-component",this.panelClass,{"p-input-filled":"filled"===this.$primevue.config.inputStyle,"p-ripple-disabled":!1===this.$primevue.config.ripple}]},label(){let e=this.getSelectedOption();return e?this.getOptionLabel(e):this.placeholder||"p-emptylabel"},editableInputValue(){let e=this.getSelectedOption();return e?this.getOptionLabel(e):this.modelValue},equalityKey(){return this.optionValue?null:this.dataKey},searchFields(){return this.filterFields||[this.optionLabel]},emptyFilterMessageText(){return this.emptyFilterMessage||this.$primevue.config.locale.emptyFilterMessage},emptyMessageText(){return this.emptyMessage||this.$primevue.config.locale.emptyMessage},appendDisabled(){return"self"===this.appendTo},virtualScrollerDisabled(){return!this.virtualScrollerOptions},appendTarget(){return this.appendDisabled?null:this.appendTo},dropdownIconClass(){return["p-dropdown-trigger-icon",this.loading?this.loadingIcon:"pi pi-chevron-down"]}},directives:{ripple:s},components:{VirtualScroller:r}};const k={class:"p-hidden-accessible"},I={key:0,class:"p-dropdown-header"},D={class:"p-dropdown-filter-container"},$=u("span",{class:"p-dropdown-filter-icon pi pi-search"},null,-1),F={class:"p-dropdown-item-group"},G={key:2,class:"p-dropdown-empty-message"},K={key:3,class:"p-dropdown-empty-message"};!function(e,i){void 0===i&&(i={});var t=i.insertAt;if(e&&"undefined"!=typeof document){var l=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===t&&l.firstChild?l.insertBefore(n,l.firstChild):l.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}}("\n.p-dropdown {\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-dropdown-clear-icon {\n position: absolute;\n top: 50%;\n margin-top: -.5rem;\n}\n.p-dropdown-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-dropdown-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-dropdown-label-empty {\n overflow: hidden;\n visibility: hidden;\n}\ninput.p-dropdown-label {\n cursor: default;\n}\n.p-dropdown .p-dropdown-panel {\n min-width: 100%;\n}\n.p-dropdown-panel {\n position: absolute;\n top: 0;\n left: 0;\n}\n.p-dropdown-items-wrapper {\n overflow: auto;\n}\n.p-dropdown-item {\n cursor: pointer;\n font-weight: normal;\n white-space: nowrap;\n position: relative;\n overflow: hidden;\n}\n.p-dropdown-item-group {\n cursor: auto;\n}\n.p-dropdown-items {\n margin: 0;\n padding: 0;\n list-style-type: none;\n}\n.p-dropdown-filter {\n width: 100%;\n}\n.p-dropdown-filter-container {\n position: relative;\n}\n.p-dropdown-filter-icon {\n position: absolute;\n top: 50%;\n margin-top: -.5rem;\n}\n.p-fluid .p-dropdown {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n.p-fluid .p-dropdown .p-dropdown-label {\n width: 1%;\n}\n"),S.render=function(e,i,t,l,n,o){const s=a("VirtualScroller"),r=p("ripple");return d(),h("div",{ref:"container",class:o.containerClass,onClick:i[13]||(i[13]=e=>o.onClick(e))},[u("div",k,[u("input",{ref:"focusInput",type:"text",id:t.inputId,readonly:"",disabled:t.disabled,onFocus:i[1]||(i[1]=(...e)=>o.onFocus&&o.onFocus(...e)),onBlur:i[2]||(i[2]=(...e)=>o.onBlur&&o.onBlur(...e)),onKeydown:i[3]||(i[3]=(...e)=>o.onKeyDown&&o.onKeyDown(...e)),tabindex:t.tabindex,"aria-haspopup":"true","aria-expanded":n.overlayVisible,"aria-labelledby":t.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),t.editable?(d(),h("input",{key:0,type:"text",class:"p-dropdown-label p-inputtext",disabled:t.disabled,onFocus:i[4]||(i[4]=(...e)=>o.onFocus&&o.onFocus(...e)),onBlur:i[5]||(i[5]=(...e)=>o.onBlur&&o.onBlur(...e)),placeholder:t.placeholder,value:o.editableInputValue,onInput:i[6]||(i[6]=(...e)=>o.onEditableInput&&o.onEditableInput(...e)),"aria-haspopup":"listbox","aria-expanded":n.overlayVisible},null,40,["disabled","placeholder","value","aria-expanded"])):c("",!0),t.editable?c("",!0):(d(),h("span",{key:1,class:o.labelClass},[b(e.$slots,"value",{value:t.modelValue,placeholder:t.placeholder},(()=>[f(v(o.label||"empty"),1)]))],2)),t.showClear&&null!=t.modelValue?(d(),h("i",{key:2,class:"p-dropdown-clear-icon pi pi-times",onClick:i[7]||(i[7]=e=>o.onClearClick(e))})):c("",!0),u("div",{class:"p-dropdown-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":n.overlayVisible},[b(e.$slots,"indicator",{},(()=>[u("span",{class:o.dropdownIconClass},null,2)]))],8,["aria-expanded"]),(d(),h(g,{to:o.appendTarget,disabled:o.appendDisabled},[u(y,{name:"p-connected-overlay",onEnter:o.onOverlayEnter,onLeave:o.onOverlayLeave,onAfterLeave:o.onOverlayAfterLeave},{default:m((()=>[n.overlayVisible?(d(),h("div",{key:0,ref:o.overlayRef,class:o.panelStyleClass,onClick:i[12]||(i[12]=(...e)=>o.onOverlayClick&&o.onOverlayClick(...e))},[b(e.$slots,"header",{value:t.modelValue,options:o.visibleOptions}),t.filter?(d(),h("div",I,[u("div",D,[O(u("input",{type:"text",ref:"filterInput","onUpdate:modelValue":i[8]||(i[8]=e=>n.filterValue=e),onVnodeUpdated:i[9]||(i[9]=(...e)=>o.onFilterUpdated&&o.onFilterUpdated(...e)),autoComplete:"off",class:"p-dropdown-filter p-inputtext p-component",placeholder:t.filterPlaceholder,onKeydown:i[10]||(i[10]=(...e)=>o.onFilterKeyDown&&o.onFilterKeyDown(...e)),onInput:i[11]||(i[11]=(...e)=>o.onFilterChange&&o.onFilterChange(...e))},null,40,["placeholder"]),[[w,n.filterValue]]),$])])):c("",!0),u("div",{ref:o.itemsWrapperRef,class:"p-dropdown-items-wrapper",style:{"max-height":o.virtualScrollerDisabled?t.scrollHeight:""}},[u(s,L({ref:o.virtualScrollerRef},t.virtualScrollerOptions,{items:o.visibleOptions,style:{height:t.scrollHeight},disabled:o.virtualScrollerDisabled}),V({content:m((({styleClass:i,contentRef:l,items:s,getItemOptions:a})=>[u("ul",{ref:l,class:["p-dropdown-items",i],role:"listbox"},[t.optionGroupLabel?(d(!0),h(x,{key:1},C(s,((i,t)=>(d(),h(x,{key:o.getOptionGroupRenderKey(i)},[u("li",F,[b(e.$slots,"optiongroup",{option:i,index:o.getOptionIndex(t,a)},(()=>[f(v(o.getOptionGroupLabel(i)),1)]))]),(d(!0),h(x,null,C(o.getOptionGroupChildren(i),((i,t)=>O((d(),h("li",{class:["p-dropdown-item",{"p-highlight":o.isSelected(i),"p-disabled":o.isOptionDisabled(i)}],key:o.getOptionRenderKey(i),onClick:e=>o.onOptionSelect(e,i),role:"option","aria-label":o.getOptionLabel(i),"aria-selected":o.isSelected(i)},[b(e.$slots,"option",{option:i,index:o.getOptionIndex(t,a)},(()=>[f(v(o.getOptionLabel(i)),1)]))],10,["onClick","aria-label","aria-selected"])),[[r]]))),128))],64)))),128)):(d(!0),h(x,{key:0},C(s,((i,t)=>O((d(),h("li",{class:["p-dropdown-item",{"p-highlight":o.isSelected(i),"p-disabled":o.isOptionDisabled(i)}],key:o.getOptionRenderKey(i),onClick:e=>o.onOptionSelect(e,i),role:"option","aria-label":o.getOptionLabel(i),"aria-selected":o.isSelected(i)},[b(e.$slots,"option",{option:i,index:o.getOptionIndex(t,a)},(()=>[f(v(o.getOptionLabel(i)),1)]))],10,["onClick","aria-label","aria-selected"])),[[r]]))),128)),n.filterValue&&(!s||s&&0===s.length)?(d(),h("li",G,[b(e.$slots,"emptyfilter",{},(()=>[f(v(o.emptyFilterMessageText),1)]))])):!t.options||t.options&&0===t.options.length?(d(),h("li",K,[b(e.$slots,"empty",{},(()=>[f(v(o.emptyMessageText),1)]))])):c("",!0)],2)])),_:2},[e.$slots.loader?{name:"loader",fn:m((({options:i})=>[b(e.$slots,"loader",{options:i})]))}:void 0]),1040,["items","style","disabled"])],4),b(e.$slots,"footer",{value:t.modelValue,options:o.visibleOptions})],2)):c("",!0)])),_:3},8,["onEnter","onLeave","onAfterLeave"])],8,["to","disabled"]))],2)};export default S;