UNPKG

primevue

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primevue.svg)](https://badge.fury.io/js/primevue) [![Discord Chat](https://img.shields.io/discord/55794023

2 lines (1 loc) 20.2 kB
"use strict";var e=require("primevue/utils"),t=require("primevue/overlayeventbus"),i=require("primevue/api"),l=require("primevue/ripple"),n=require("primevue/virtualscroller"),o=require("vue");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=r(t),a=r(l),p=r(n),d={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.ZIndexUtils.clear(this.overlay),this.overlay=null)},methods:{getOptionIndex(e,t){return this.virtualScrollerDisabled?e:t&&t(e).index},getOptionLabel(t){return this.optionLabel?e.ObjectUtils.resolveFieldData(t,this.optionLabel):t},getOptionValue(t){return this.optionValue?e.ObjectUtils.resolveFieldData(t,this.optionValue):t},getOptionRenderKey(t){return this.dataKey?e.ObjectUtils.resolveFieldData(t,this.dataKey):this.getOptionLabel(t)},isOptionDisabled(t){return!!this.optionDisabled&&e.ObjectUtils.resolveFieldData(t,this.optionDisabled)},getOptionGroupRenderKey(t){return e.ObjectUtils.resolveFieldData(t,this.optionGroupLabel)},getOptionGroupLabel(t){return e.ObjectUtils.resolveFieldData(t,this.optionGroupLabel)},getOptionGroupChildren(t){return e.ObjectUtils.resolveFieldData(t,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 t=this.findOptionIndexInList(this.modelValue,this.getOptionGroupChildren(this.visibleOptions[e]));if(-1!==t)return{group:e,option:t}}}return-1},findOptionIndexInList(t,i){for(let l=0;l<i.length;l++)if(e.ObjectUtils.equals(t,this.getOptionValue(i[l]),this.equalityKey))return l;return-1},isSelected(t){return e.ObjectUtils.equals(this.modelValue,this.getOptionValue(t),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 t=this.visibleOptions&&this.visibleOptions.length>0?this.findNextOption(this.getSelectedOptionIndex()):null;t&&this.updateModel(e,this.getOptionValue(t))}e.preventDefault()},onUpKey(e){if(this.visibleOptions){let t=this.findPrevOption(this.getSelectedOptionIndex());t&&this.updateModel(e,this.getOptionValue(t))}e.preventDefault()},findNextOption(e){if(this.optionGroupLabel){let t=-1===e?0:e.group,i=-1===e?-1:e.option,l=this.findNextOptionInList(this.getOptionGroupChildren(this.visibleOptions[t]),i);return l||(t+1!==this.visibleOptions.length?this.findNextOption({group:t+1,option:-1}):null)}return this.findNextOptionInList(this.visibleOptions,e)},findNextOptionInList(e,t){let i=t+1;if(i===e.length)return null;let l=e[i];return this.isOptionDisabled(l)?this.findNextOptionInList(i):l},findPrevOption(e){if(-1===e)return null;if(this.optionGroupLabel){let t=e.group,i=e.option,l=this.findPrevOptionInList(this.getOptionGroupChildren(this.visibleOptions[t]),i);return l||(t>0?this.findPrevOption({group:t-1,option:this.getOptionGroupChildren(this.visibleOptions[t-1]).length}):null)}return this.findPrevOptionInList(this.visibleOptions,e)},findPrevOptionInList(e,t){let i=t-1;if(i<0)return null;let l=e[i];return this.isOptionDisabled(l)?this.findPrevOption(i):l},onClearClick(e){this.updateModel(e,null)},onClick(t){this.disabled||this.loading||e.DomHandler.hasClass(t.target,"p-dropdown-clear-icon")||"INPUT"===t.target.tagName||this.overlay&&this.overlay.contains(t.target)||(this.overlayVisible?this.hide():this.show(),this.$refs.focusInput.focus())},onOptionSelect(e,t){let i=this.getOptionValue(t);this.updateModel(e,i),this.$refs.focusInput.focus(),setTimeout((()=>{this.hide()}),200)},onEditableInput(e){this.$emit("update:modelValue",e.target.value)},onOverlayEnter(t){if(e.ZIndexUtils.set("overlay",t,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(t){e.ZIndexUtils.clear(t)},alignOverlay(){this.appendDisabled?e.DomHandler.relativePosition(this.overlay,this.$el):(this.overlay.style.minWidth=e.DomHandler.getOuterWidth(this.$el)+"px",e.DomHandler.absolutePosition(this.overlay,this.$el))},updateModel(e,t){this.$emit("update:modelValue",t),this.$emit("change",{originalEvent:e,value:t})},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 e.ConnectedOverlayScrollHandler(this.$refs.container,(()=>{this.overlayVisible&&this.hide()}))),this.scrollHandler.bindScrollListener()},unbindScrollListener(){this.scrollHandler&&this.scrollHandler.unbindScrollListener()},bindResizeListener(){this.resizeListener||(this.resizeListener=()=>{this.overlayVisible&&!e.DomHandler.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 t=e.key;if(this.previousSearchChar=this.currentSearchChar,this.currentSearchChar=t,this.previousSearchChar===this.currentSearchChar?this.searchValue=this.currentSearchChar:this.searchValue=this.searchValue?this.searchValue+t:t,this.searchValue){let t=this.getSelectedOptionIndex(),i=this.optionGroupLabel?this.searchOptionInGroup(t):this.searchOption(++t);i&&this.updateModel(e,this.getOptionValue(i))}this.searchTimeout=setTimeout((()=>{this.searchValue=null}),250)},searchOption(e){let t;return this.searchValue&&(t=this.searchOptionInRange(e,this.visibleOptions.length),t||(t=this.searchOptionInRange(0,e))),t},searchOptionInRange(e,t){for(let i=e;i<t;i++){let e=this.visibleOptions[i];if(this.matchesSearchValue(e))return e}return null},searchOptionInGroup(e){let t=-1===e?{group:0,option:-1}:e;for(let e=t.group;e<this.visibleOptions.length;e++){let i=this.getOptionGroupChildren(this.visibleOptions[e]);for(let l=t.group===e?t.option+1:0;l<i.length;l++)if(this.matchesSearchValue(i[l]))return i[l]}for(let e=0;e<=t.group;e++){let i=this.getOptionGroupChildren(this.visibleOptions[e]);for(let l=0;l<(t.group===e?t.option:i.length);l++)if(this.matchesSearchValue(i[l]))return i[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 t=e.DomHandler.findSingle(this.overlay,"li.p-highlight");t&&t.scrollIntoView({block:"nearest",inline:"start"})}},onOverlayClick(e){s.default.emit("overlay-click",{originalEvent:e,target:this.$el})}},computed:{visibleOptions(){if(this.filterValue){if(this.optionGroupLabel){let e=[];for(let t of this.options){let l=i.FilterService.filter(this.getOptionGroupChildren(t),this.searchFields,this.filterValue,this.filterMatchMode,this.filterLocale);if(l&&l.length){let i={...t};i[this.optionGroupChildren]=l,e.push(i)}}return e}return i.FilterService.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:a.default},components:{VirtualScroller:p.default}};const h={class:"p-hidden-accessible"},u={key:0,class:"p-dropdown-header"},c={class:"p-dropdown-filter-container"},b=o.createVNode("span",{class:"p-dropdown-filter-icon pi pi-search"},null,-1),f={class:"p-dropdown-item-group"},v={key:2,class:"p-dropdown-empty-message"},g={key:3,class:"p-dropdown-empty-message"};!function(e,t){void 0===t&&(t={});var i=t.insertAt;if(e&&"undefined"!=typeof document){var l=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css","top"===i&&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"),d.render=function(e,t,i,l,n,r){const s=o.resolveComponent("VirtualScroller"),a=o.resolveDirective("ripple");return o.openBlock(),o.createBlock("div",{ref:"container",class:r.containerClass,onClick:t[13]||(t[13]=e=>r.onClick(e))},[o.createVNode("div",h,[o.createVNode("input",{ref:"focusInput",type:"text",id:i.inputId,readonly:"",disabled:i.disabled,onFocus:t[1]||(t[1]=(...e)=>r.onFocus&&r.onFocus(...e)),onBlur:t[2]||(t[2]=(...e)=>r.onBlur&&r.onBlur(...e)),onKeydown:t[3]||(t[3]=(...e)=>r.onKeyDown&&r.onKeyDown(...e)),tabindex:i.tabindex,"aria-haspopup":"true","aria-expanded":n.overlayVisible,"aria-labelledby":i.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),i.editable?(o.openBlock(),o.createBlock("input",{key:0,type:"text",class:"p-dropdown-label p-inputtext",disabled:i.disabled,onFocus:t[4]||(t[4]=(...e)=>r.onFocus&&r.onFocus(...e)),onBlur:t[5]||(t[5]=(...e)=>r.onBlur&&r.onBlur(...e)),placeholder:i.placeholder,value:r.editableInputValue,onInput:t[6]||(t[6]=(...e)=>r.onEditableInput&&r.onEditableInput(...e)),"aria-haspopup":"listbox","aria-expanded":n.overlayVisible},null,40,["disabled","placeholder","value","aria-expanded"])):o.createCommentVNode("",!0),i.editable?o.createCommentVNode("",!0):(o.openBlock(),o.createBlock("span",{key:1,class:r.labelClass},[o.renderSlot(e.$slots,"value",{value:i.modelValue,placeholder:i.placeholder},(()=>[o.createTextVNode(o.toDisplayString(r.label||"empty"),1)]))],2)),i.showClear&&null!=i.modelValue?(o.openBlock(),o.createBlock("i",{key:2,class:"p-dropdown-clear-icon pi pi-times",onClick:t[7]||(t[7]=e=>r.onClearClick(e))})):o.createCommentVNode("",!0),o.createVNode("div",{class:"p-dropdown-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":n.overlayVisible},[o.renderSlot(e.$slots,"indicator",{},(()=>[o.createVNode("span",{class:r.dropdownIconClass},null,2)]))],8,["aria-expanded"]),(o.openBlock(),o.createBlock(o.Teleport,{to:r.appendTarget,disabled:r.appendDisabled},[o.createVNode(o.Transition,{name:"p-connected-overlay",onEnter:r.onOverlayEnter,onLeave:r.onOverlayLeave,onAfterLeave:r.onOverlayAfterLeave},{default:o.withCtx((()=>[n.overlayVisible?(o.openBlock(),o.createBlock("div",{key:0,ref:r.overlayRef,class:r.panelStyleClass,onClick:t[12]||(t[12]=(...e)=>r.onOverlayClick&&r.onOverlayClick(...e))},[o.renderSlot(e.$slots,"header",{value:i.modelValue,options:r.visibleOptions}),i.filter?(o.openBlock(),o.createBlock("div",u,[o.createVNode("div",c,[o.withDirectives(o.createVNode("input",{type:"text",ref:"filterInput","onUpdate:modelValue":t[8]||(t[8]=e=>n.filterValue=e),onVnodeUpdated:t[9]||(t[9]=(...e)=>r.onFilterUpdated&&r.onFilterUpdated(...e)),autoComplete:"off",class:"p-dropdown-filter p-inputtext p-component",placeholder:i.filterPlaceholder,onKeydown:t[10]||(t[10]=(...e)=>r.onFilterKeyDown&&r.onFilterKeyDown(...e)),onInput:t[11]||(t[11]=(...e)=>r.onFilterChange&&r.onFilterChange(...e))},null,40,["placeholder"]),[[o.vModelText,n.filterValue]]),b])])):o.createCommentVNode("",!0),o.createVNode("div",{ref:r.itemsWrapperRef,class:"p-dropdown-items-wrapper",style:{"max-height":r.virtualScrollerDisabled?i.scrollHeight:""}},[o.createVNode(s,o.mergeProps({ref:r.virtualScrollerRef},i.virtualScrollerOptions,{items:r.visibleOptions,style:{height:i.scrollHeight},disabled:r.virtualScrollerDisabled}),o.createSlots({content:o.withCtx((({styleClass:t,contentRef:l,items:s,getItemOptions:p})=>[o.createVNode("ul",{ref:l,class:["p-dropdown-items",t],role:"listbox"},[i.optionGroupLabel?(o.openBlock(!0),o.createBlock(o.Fragment,{key:1},o.renderList(s,((t,i)=>(o.openBlock(),o.createBlock(o.Fragment,{key:r.getOptionGroupRenderKey(t)},[o.createVNode("li",f,[o.renderSlot(e.$slots,"optiongroup",{option:t,index:r.getOptionIndex(i,p)},(()=>[o.createTextVNode(o.toDisplayString(r.getOptionGroupLabel(t)),1)]))]),(o.openBlock(!0),o.createBlock(o.Fragment,null,o.renderList(r.getOptionGroupChildren(t),((t,i)=>o.withDirectives((o.openBlock(),o.createBlock("li",{class:["p-dropdown-item",{"p-highlight":r.isSelected(t),"p-disabled":r.isOptionDisabled(t)}],key:r.getOptionRenderKey(t),onClick:e=>r.onOptionSelect(e,t),role:"option","aria-label":r.getOptionLabel(t),"aria-selected":r.isSelected(t)},[o.renderSlot(e.$slots,"option",{option:t,index:r.getOptionIndex(i,p)},(()=>[o.createTextVNode(o.toDisplayString(r.getOptionLabel(t)),1)]))],10,["onClick","aria-label","aria-selected"])),[[a]]))),128))],64)))),128)):(o.openBlock(!0),o.createBlock(o.Fragment,{key:0},o.renderList(s,((t,i)=>o.withDirectives((o.openBlock(),o.createBlock("li",{class:["p-dropdown-item",{"p-highlight":r.isSelected(t),"p-disabled":r.isOptionDisabled(t)}],key:r.getOptionRenderKey(t),onClick:e=>r.onOptionSelect(e,t),role:"option","aria-label":r.getOptionLabel(t),"aria-selected":r.isSelected(t)},[o.renderSlot(e.$slots,"option",{option:t,index:r.getOptionIndex(i,p)},(()=>[o.createTextVNode(o.toDisplayString(r.getOptionLabel(t)),1)]))],10,["onClick","aria-label","aria-selected"])),[[a]]))),128)),n.filterValue&&(!s||s&&0===s.length)?(o.openBlock(),o.createBlock("li",v,[o.renderSlot(e.$slots,"emptyfilter",{},(()=>[o.createTextVNode(o.toDisplayString(r.emptyFilterMessageText),1)]))])):!i.options||i.options&&0===i.options.length?(o.openBlock(),o.createBlock("li",g,[o.renderSlot(e.$slots,"empty",{},(()=>[o.createTextVNode(o.toDisplayString(r.emptyMessageText),1)]))])):o.createCommentVNode("",!0)],2)])),_:2},[e.$slots.loader?{name:"loader",fn:o.withCtx((({options:t})=>[o.renderSlot(e.$slots,"loader",{options:t})]))}:void 0]),1040,["items","style","disabled"])],4),o.renderSlot(e.$slots,"footer",{value:i.modelValue,options:r.visibleOptions})],2)):o.createCommentVNode("",!0)])),_:3},8,["onEnter","onLeave","onAfterLeave"])],8,["to","disabled"]))],2)},module.exports=d;