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) 15.1 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 a,renderSlot as r,Fragment as c,createTextVNode as d,toDisplayString as p,renderList as h,createCommentVNode as u,Transition as b,withCtx as m,withDirectives as y,vModelText as f}from"vue";var v={emits:["update:modelValue","before-show","before-hide","change","show","hide","filter"],props:{modelValue:null,options:Array,optionLabel:null,optionValue:null,optionDisabled:null,scrollHeight:{type:String,default:"200px"},placeholder:String,disabled:Boolean,filter:Boolean,tabindex:String,inputId:String,dataKey:null,filterPlaceholder:String,filterLocale:String,ariaLabelledBy:null,appendTo:{type:String,default:null},emptyFilterMessage:{type:String,default:"No results found"},display:{type:String,default:"comma"}},data:()=>({focused:!1,headerCheckboxFocused:!1,filterValue:null,overlayVisible:!1}),outsideClickListener:null,resizeListener:null,scrollHandler: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)},isSelected(i){let t=!1,l=this.getOptionValue(i);if(this.modelValue)for(let i of this.modelValue)if(e.equals(i,l,this.equalityKey)){t=!0;break}return t},show(){this.$emit("before-show"),this.overlayVisible=!0},hide(){this.$emit("before-hide"),this.overlayVisible=!1},onFocus(){this.focused=!0},onBlur(){this.focused=!1},onHeaderCheckboxFocus(){this.headerCheckboxFocused=!0},onHeaderCheckboxBlur(){this.headerCheckboxFocused=!1},onClick(e){this.disabled||this.overlay&&this.overlay.contains(e.target)||i.hasClass(e.target,"p-multiselect-close")||(i.hasClass(e.target,"p-multiselect-close"),this.overlayVisible?this.hide():this.show(),this.$refs.focusInput.focus())},onCloseClick(){this.hide()},onKeyDown(e){switch(e.which){case 40:this.visibleOptions&&!this.overlayVisible&&e.altKey&&this.show();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()}},onOptionSelect(i,t){if(this.disabled||this.isOptionDisabled(t))return;let l=null;l=this.isSelected(t)?this.modelValue.filter((i=>!e.equals(i,this.getOptionValue(t),this.equalityKey))):[...this.modelValue||[],this.getOptionValue(t)],this.$emit("update:modelValue",l),this.$emit("change",{originalEvent:i,value:l})},onOptionKeyDown(e,i){let t=e.target;switch(e.which){case 40:var l=this.findNextItem(t);l&&l.focus(),e.preventDefault();break;case 38:var n=this.findPrevItem(t);n&&n.focus(),e.preventDefault();break;case 13:this.onOptionSelect(e,i),e.preventDefault()}},findNextItem(e){let t=e.nextElementSibling;return t?i.hasClass(t,"p-disabled")?this.findNextItem(t):t:null},findPrevItem(e){let t=e.previousElementSibling;return t?i.hasClass(t,"p-disabled")?this.findPrevItem(t):t:null},onOverlayEnter(){this.overlay.style.zIndex=String(i.generateZIndex()),this.appendContainer(),this.alignOverlay(),this.bindOutsideClickListener(),this.bindScrollListener(),this.bindResizeListener(),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)},bindOutsideClickListener(){this.outsideClickListener||(this.outsideClickListener=e=>{this.overlayVisible&&this.isOutsideClicked(e)&&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)},isOutsideClicked(e){return!(this.$el.isSameNode(e.target)||this.$el.contains(e.target)||this.overlay&&this.overlay.contains(e.target))},getLabelByValue(i){let t=null;if(this.options)for(let l of this.options){let n=this.getOptionValue(l);if(e.equals(n,i,this.equalityKey)){t=this.getOptionLabel(l);break}}return t},onToggleAll(e){const i=this.allSelected?[]:this.visibleOptions&&this.visibleOptions.map((e=>this.getOptionValue(e)));this.$emit("update:modelValue",i),this.$emit("change",{originalEvent:e,value:i})},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},removeChip(i){let t=this.modelValue.filter((t=>!e.equals(t,i,this.equalityKey)));this.$emit("update:modelValue",t),this.$emit("change",{originalEvent:event,value:t})}},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-multiselect p-component p-inputwrapper",{"p-multiselect-chip":"chip"===this.display,"p-disabled":this.disabled,"p-focus":this.focused,"p-inputwrapper-filled":this.modelValue&&this.modelValue.length,"p-inputwrapper-focus":this.focused||this.overlayVisible}]},labelClass(){return["p-multiselect-label",{"p-placeholder":this.label===this.placeholder,"p-multiselect-label-empty":!(this.placeholder||this.modelValue&&0!==this.modelValue.length)}]},label(){let e;if(this.modelValue&&this.modelValue.length){e="";for(let i=0;i<this.modelValue.length;i++)0!==i&&(e+=", "),e+=this.getLabelByValue(this.modelValue[i])}else e=this.placeholder;return e},allSelected(){if(this.filterValue&&this.filterValue.trim().length>0){let e=!0;if(this.visibleOptions.length>0){for(let i of this.visibleOptions)if(!this.isSelected(i)){e=!1;break}}else e=!1;return e}return this.modelValue&&this.options&&this.modelValue.length>0&&this.modelValue.length===this.options.length},equalityKey(){return this.optionValue?null:this.dataKey}},directives:{ripple:l}};const g={class:"p-hidden-accessible"},x={class:"p-multiselect-label-container"},k={class:"p-multiselect-token-label"},w=a("div",{class:"p-multiselect-trigger"},[a("span",{class:"p-multiselect-trigger-icon pi pi-chevron-down"})],-1),C={class:"p-multiselect-header"},V={class:"p-hidden-accessible"},L={key:0,class:"p-multiselect-filter-container"},O=a("span",{class:"p-multiselect-filter-icon pi pi-search"},null,-1),S=a("span",{class:"p-multiselect-close-icon pi pi-times"},null,-1),F={class:"p-multiselect-items p-component",role:"listbox","aria-multiselectable":"true"},B={class:"p-checkbox p-component"},D={key:0,class:"p-multiselect-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-multiselect {\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-multiselect-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-multiselect-label-container {\n overflow: hidden;\n -webkit-box-flex: 1;\n -ms-flex: 1 1 auto;\n flex: 1 1 auto;\n cursor: pointer;\n}\n.p-multiselect-label {\n display: block;\n white-space: nowrap;\n cursor: pointer;\n overflow: hidden;\n text-overflow: ellipsis;\n}\n.p-multiselect-label-empty {\n overflow: hidden;\n visibility: hidden;\n}\n.p-multiselect-token {\n cursor: default;\n display: -webkit-inline-box;\n display: -ms-inline-flexbox;\n display: inline-flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n -webkit-box-flex: 0;\n -ms-flex: 0 0 auto;\n flex: 0 0 auto;\n}\n.p-multiselect-token-icon {\n cursor: pointer;\n}\n.p-multiselect .p-multiselect-panel {\n min-width: 100%;\n}\n.p-multiselect-panel {\n position: absolute;\n}\n.p-multiselect-items-wrapper {\n overflow: auto;\n}\n.p-multiselect-items {\n margin: 0;\n padding: 0;\n list-style-type: none;\n}\n.p-multiselect-item {\n cursor: pointer;\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 font-weight: normal;\n white-space: nowrap;\n position: relative;\n overflow: hidden;\n}\n.p-multiselect-header {\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: justify;\n -ms-flex-pack: justify;\n justify-content: space-between;\n}\n.p-multiselect-filter-container {\n position: relative;\n -webkit-box-flex: 1;\n -ms-flex: 1 1 auto;\n flex: 1 1 auto;\n}\n.p-multiselect-filter-icon {\n position: absolute;\n top: 50%;\n margin-top: -.5rem;\n}\n.p-multiselect-filter-container .p-inputtext {\n width: 100%;\n}\n.p-multiselect-close {\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 overflow: hidden;\n position: relative;\n}\n.p-fluid .p-multiselect {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n"),v.render=function(e,i,t,l,v,K){const $=n("ripple");return s(),o("div",{ref:"container",class:K.containerClass,onClick:i[10]||(i[10]=(...e)=>K.onClick&&K.onClick(...e))},[a("div",g,[a("input",{ref:"focusInput",type:"text",role:"listbox",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":v.overlayVisible,"aria-labelledby":t.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),a("div",x,[a("div",{class:K.labelClass},[r(e.$slots,"value",{value:t.modelValue,placeholder:t.placeholder},(()=>["comma"===t.display?(s(),o(c,{key:0},[d(p(K.label||"empty"),1)],64)):"chip"===t.display?(s(),o(c,{key:1},[(s(!0),o(c,null,h(t.modelValue,(e=>(s(),o("div",{class:"p-multiselect-token",key:K.getLabelByValue(e)},[a("span",k,p(K.getLabelByValue(e)),1),t.disabled?u("",!0):(s(),o("span",{key:0,class:"p-multiselect-token-icon pi pi-times-circle",onClick:i=>K.removeChip(e)},null,8,["onClick"]))])))),128)),t.modelValue&&0!==t.modelValue.length?u("",!0):(s(),o(c,{key:0},[d(p(t.placeholder||"empty"),1)],64))],64)):u("",!0)]))],2)]),w,a(b,{name:"p-connected-overlay",onEnter:K.onOverlayEnter,onLeave:K.onOverlayLeave},{default:m((()=>[v.overlayVisible?(s(),o("div",{key:0,ref:K.overlayRef,class:"p-multiselect-panel p-component"},[a("div",C,[a("div",{class:"p-checkbox p-component",onClick:i[6]||(i[6]=(...e)=>K.onToggleAll&&K.onToggleAll(...e)),role:"checkbox","aria-checked":K.allSelected},[a("div",V,[a("input",{type:"checkbox",readonly:"",onFocus:i[4]||(i[4]=(...e)=>K.onHeaderCheckboxFocus&&K.onHeaderCheckboxFocus(...e)),onBlur:i[5]||(i[5]=(...e)=>K.onHeaderCheckboxBlur&&K.onHeaderCheckboxBlur(...e))},null,32)]),a("div",{class:["p-checkbox-box",{"p-highlight":K.allSelected,"p-focus":v.headerCheckboxFocused}],role:"checkbox","aria-checked":K.allSelected},[a("span",{class:["p-checkbox-icon",{"pi pi-check":K.allSelected}]},null,2)],10,["aria-checked"])],8,["aria-checked"]),t.filter?(s(),o("div",L,[y(a("input",{type:"text","onUpdate:modelValue":i[7]||(i[7]=e=>v.filterValue=e),class:"p-multiselect-filter p-inputtext p-component",placeholder:t.filterPlaceholder,onInput:i[8]||(i[8]=(...e)=>K.onFilterChange&&K.onFilterChange(...e))},null,40,["placeholder"]),[[f,v.filterValue]]),O])):u("",!0),y(a("button",{class:"p-multiselect-close p-link",onClick:i[9]||(i[9]=(...e)=>K.onCloseClick&&K.onCloseClick(...e)),type:"button"},[S],512),[[$]])]),a("div",{class:"p-multiselect-items-wrapper",style:{"max-height":t.scrollHeight}},[a("ul",F,[(s(!0),o(c,null,h(K.visibleOptions,((i,l)=>y((s(),o("li",{class:["p-multiselect-item",{"p-highlight":K.isSelected(i),"p-disabled":K.isOptionDisabled(i)}],role:"option","aria-selected":K.isSelected(i),"aria-label":K.getOptionLabel(i),key:K.getOptionRenderKey(i),onClick:e=>K.onOptionSelect(e,i),onKeydown:e=>K.onOptionKeyDown(e,i),tabindex:t.tabindex||"0"},[a("div",B,[a("div",{class:["p-checkbox-box",{"p-highlight":K.isSelected(i)}]},[a("span",{class:["p-checkbox-icon",{"pi pi-check":K.isSelected(i)}]},null,2)],2)]),r(e.$slots,"option",{option:i,index:l},(()=>[a("span",null,p(K.getOptionLabel(i)),1)]))],42,["aria-selected","aria-label","onClick","onKeydown","tabindex"])),[[$]]))),128)),v.filterValue&&(!K.visibleOptions||K.visibleOptions&&0===K.visibleOptions.length)?(s(),o("li",D,p(t.emptyFilterMessage),1)):u("",!0)])],4)],512)):u("",!0)])),_:1},8,["onEnter","onLeave"])],2)};export default v;