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)

2 lines (1 loc) 13.4 kB
import{ObjectUtils as e,DomHandler as i,ConnectedOverlayScrollHandler as t}from"primevue/utils";import l from"primevue/ripple";import{resolveDirective as n,openBlock as s,createBlock as o,createVNode as r,createCommentVNode as a,renderSlot as d,createTextVNode as p,toDisplayString as h,Transition as u,withCtx as c,withDirectives as b,vModelText as y,Fragment as f,renderList as v}from"vue";var m={emits:["update:modelValue","before-show","before-hide","show","hide","change","filter"],props:{modelValue:null,options:Array,optionLabel:null,optionValue:null,optionDisabled:null,scrollHeight:{type:String,default:"200px"},filter:Boolean,filterPlaceholder:String,filterLocale:String,editable:Boolean,placeholder:String,disabled:Boolean,dataKey:null,showClear:Boolean,inputId:String,tabindex:String,ariaLabelledBy:null,appendTo:{type:String,default:null},emptyFilterMessage:{type:String,default:"No results found"}},data:()=>({focused:!1,filterValue:null,overlayVisible:!1}),outsideClickListener:null,scrollHandler:null,resizeListener:null,searchTimeout:null,currentSearchChar:null,previousSearchChar:null,searchValue:null,overlay:null,beforeUnmount(){this.restoreAppend(),this.unbindOutsideClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),this.overlay=null},methods:{getOptionLabel(i){return this.optionLabel?e.resolveFieldData(i,this.optionLabel):i},getOptionValue(i){return this.optionValue?e.resolveFieldData(i,this.optionValue):i},getOptionRenderKey(i){return this.dataKey?e.resolveFieldData(i,this.dataKey):this.getOptionLabel(i)},isOptionDisabled(i){return!!this.optionDisabled&&e.resolveFieldData(i,this.optionDisabled)},getSelectedOption(){let i;if(null!=this.modelValue&&this.options)for(let t of this.options)if(e.equals(this.modelValue,this.getOptionValue(t),this.equalityKey)){i=t;break}return i},isSelected(i){return e.equals(this.modelValue,this.getOptionValue(i),this.equalityKey)},getSelectedOptionIndex(){let i=-1;if(null!=this.modelValue&&this.visibleOptions)for(let t=0;t<this.visibleOptions.length;t++)if(e.equals(this.modelValue,this.getOptionValue(this.visibleOptions[t]),this.equalityKey)){i=t;break}return i},show(){this.$emit("before-show"),this.overlayVisible=!0},hide(){this.$emit("before-hide"),this.overlayVisible=!1},onFocus(){this.focused=!0},onBlur(){this.focused=!1},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.findNextOption(this.getSelectedOptionIndex());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){let i=e+1;if(i===this.visibleOptions.length)return null;let t=this.visibleOptions[i];return this.isOptionDisabled(t)?this.findNextOption(i):t},findPrevOption(e){let i=e-1;if(i<0)return null;let t=this.visibleOptions[i];return this.isOptionDisabled(t)?this.findPrevOption(i):t},onClearClick(e){this.updateModel(e,null)},onClick(e){this.disabled||i.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(){this.overlay.style.zIndex=String(i.generateZIndex()),this.appendContainer(),this.alignOverlay(),this.bindOutsideClickListener(),this.bindScrollListener(),this.bindResizeListener(),this.filter&&this.$refs.filterInput.focus(),this.$emit("show")},onOverlayLeave(){this.unbindOutsideClickListener(),this.unbindScrollListener(),this.unbindResizeListener(),this.$emit("hide"),this.overlay=null},alignOverlay(){this.appendTo?(i.absolutePosition(this.overlay,this.$el),this.overlay.style.minWidth=i.getOuterWidth(this.$el)+"px"):i.relativePosition(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 t(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)},search(e){if(!this.visibleOptions)return;this.searchTimeout&&clearTimeout(this.searchTimeout);const i=String.fromCharCode(e.keyCode);this.previousSearchChar=this.currentSearchChar,this.currentSearchChar=i,this.previousSearchChar===this.currentSearchChar?this.searchValue=this.currentSearchChar:this.searchValue=this.searchValue?this.searchValue+i:i;let t=this.getSelectedOptionIndex(),l=this.searchOption(++t);l&&this.updateModel(e,this.getOptionValue(l)),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.getOptionLabel(e).toLocaleLowerCase(this.filterLocale).startsWith(this.searchValue.toLocaleLowerCase(this.filterLocale)))return e}return null},appendContainer(){this.appendTo&&("body"===this.appendTo?document.body.appendChild(this.overlay):document.getElementById(this.appendTo).appendChild(this.overlay))},restoreAppend(){this.overlay&&this.appendTo&&("body"===this.appendTo?document.body.removeChild(this.overlay):document.getElementById(this.appendTo).removeChild(this.overlay))},onFilterChange(e){this.$emit("filter",{originalEvent:e,value:e.target.value}),this.overlayVisible&&this.alignOverlay()},overlayRef(e){this.overlay=e}},computed:{visibleOptions(){return this.filterValue&&this.filterValue.trim().length>0?this.options.filter((e=>this.getOptionLabel(e).toLocaleLowerCase(this.filterLocale).indexOf(this.filterValue.toLocaleLowerCase(this.filterLocale))>-1)):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)}]},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}},directives:{ripple:l}};const w={class:"p-hidden-accessible"},g=r("span",{class:"p-dropdown-trigger-icon pi pi-chevron-down"},null,-1),O={key:0,class:"p-dropdown-header"},L={class:"p-dropdown-filter-container"},x=r("span",{class:"p-dropdown-filter-icon pi pi-search"},null,-1),V={class:"p-dropdown-items",role:"listbox"},C={key:0,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}\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-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"),m.render=function(e,i,t,l,m,k){const S=n("ripple");return s(),o("div",{ref:"container",class:k.containerClass,onClick:i[11]||(i[11]=e=>k.onClick(e))},[r("div",w,[r("input",{ref:"focusInput",type:"text",id:t.inputId,readonly:"",disabled:t.disabled,onFocus:i[1]||(i[1]=(...e)=>k.onFocus&&k.onFocus(...e)),onBlur:i[2]||(i[2]=(...e)=>k.onBlur&&k.onBlur(...e)),onKeydown:i[3]||(i[3]=(...e)=>k.onKeyDown&&k.onKeyDown(...e)),tabindex:t.tabindex,"aria-haspopup":"listbox","aria-expanded":m.overlayVisible,"aria-labelledby":t.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),t.editable?(s(),o("input",{key:0,type:"text",class:"p-dropdown-label p-inputtext",disabled:t.disabled,onFocus:i[4]||(i[4]=(...e)=>k.onFocus&&k.onFocus(...e)),onBlur:i[5]||(i[5]=(...e)=>k.onBlur&&k.onBlur(...e)),placeholder:t.placeholder,value:k.editableInputValue,onInput:i[6]||(i[6]=(...e)=>k.onEditableInput&&k.onEditableInput(...e)),"aria-haspopup":"listbox","aria-expanded":m.overlayVisible},null,40,["disabled","placeholder","value","aria-expanded"])):a("",!0),t.editable?a("",!0):(s(),o("span",{key:1,class:k.labelClass},[d(e.$slots,"value",{value:t.modelValue,placeholder:t.placeholder},(()=>[p(h(k.label),1)]))],2)),t.showClear&&null!=t.modelValue?(s(),o("i",{key:2,class:"p-dropdown-clear-icon pi pi-times",onClick:i[7]||(i[7]=e=>k.onClearClick(e))})):a("",!0),r("div",{class:"p-dropdown-trigger",role:"button","aria-haspopup":"listbox","aria-expanded":m.overlayVisible},[g],8,["aria-expanded"]),r(u,{name:"p-connected-overlay",onEnter:k.onOverlayEnter,onLeave:k.onOverlayLeave},{default:c((()=>[m.overlayVisible?(s(),o("div",{key:0,ref:k.overlayRef,class:"p-dropdown-panel p-component"},[t.filter?(s(),o("div",O,[r("div",L,[b(r("input",{type:"text",ref:"filterInput","onUpdate:modelValue":i[8]||(i[8]=e=>m.filterValue=e),autoComplete:"off",class:"p-dropdown-filter p-inputtext p-component",placeholder:t.filterPlaceholder,onKeydown:i[9]||(i[9]=(...e)=>k.onFilterKeyDown&&k.onFilterKeyDown(...e)),onInput:i[10]||(i[10]=(...e)=>k.onFilterChange&&k.onFilterChange(...e))},null,40,["placeholder"]),[[y,m.filterValue]]),x])])):a("",!0),r("div",{class:"p-dropdown-items-wrapper",style:{"max-height":t.scrollHeight}},[r("ul",V,[(s(!0),o(f,null,v(k.visibleOptions,((i,t)=>b((s(),o("li",{class:["p-dropdown-item",{"p-highlight":k.isSelected(i),"p-disabled":k.isOptionDisabled(i)}],"aria-label":k.getOptionLabel(i),key:k.getOptionRenderKey(i),onClick:e=>k.onOptionSelect(e,i),role:"option","aria-selected":k.isSelected(i)},[d(e.$slots,"option",{option:i,index:t},(()=>[p(h(k.getOptionLabel(i)),1)]))],10,["aria-label","onClick","aria-selected"])),[[S]]))),128)),m.filterValue&&(!k.visibleOptions||k.visibleOptions&&0===k.visibleOptions.length)?(s(),o("li",C,h(t.emptyFilterMessage),1)):a("",!0)])],4)],512)):a("",!0)])),_:1},8,["onEnter","onLeave"])],2)};export default m;