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.5 kB
"use strict";var e=require("primevue/utils"),t=require("primevue/overlayeventbus"),i=require("primevue/api"),l=require("primevue/ripple"),n=require("vue");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=s(t),a={name:"MultiSelect",emits:["update:modelValue","before-show","before-hide","change","show","hide","filter"],props:{modelValue:null,options:Array,optionLabel:null,optionValue:null,optionDisabled:null,optionGroupLabel:null,optionGroupChildren:null,scrollHeight:{type:String,default:"200px"},placeholder:String,disabled:Boolean,tabindex:String,inputId:String,dataKey:null,filter:Boolean,filterPlaceholder:String,filterLocale:String,filterMatchMode:{type:String,default:"contains"},filterFields:{type:Array,default:null},ariaLabelledBy:null,appendTo:{type:String,default:"body"},emptyFilterMessage:{type:String,default:null},emptyMessage:{type:String,default:null},display:{type:String,default:"comma"},panelClass:null,selectionLimit:{type:Number,default:null},showToggleAll:{type:Boolean,default:!0},loading:{type:Boolean,default:!1},loadingIcon:{type:String,default:"pi pi-spinner pi-spin"}},data:()=>({focused:!1,headerCheckboxFocused:!1,filterValue:null,overlayVisible:!1}),outsideClickListener:null,resizeListener:null,scrollHandler:null,overlay:null,beforeUnmount(){this.unbindOutsideClickListener(),this.unbindResizeListener(),this.scrollHandler&&(this.scrollHandler.destroy(),this.scrollHandler=null),this.overlay&&(e.ZIndexUtils.clear(this.overlay),this.overlay=null)},methods:{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)},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)},isOptionDisabled(t){return!(!this.maxSelectionLimitReached||this.isSelected(t))||!!this.optionDisabled&&e.ObjectUtils.resolveFieldData(t,this.optionDisabled)},isSelected(t){let i=!1,l=this.getOptionValue(t);if(this.modelValue)for(let t of this.modelValue)if(e.ObjectUtils.equals(t,l,this.equalityKey)){i=!0;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},onHeaderCheckboxFocus(){this.headerCheckboxFocused=!0},onHeaderCheckboxBlur(){this.headerCheckboxFocused=!1},onClick(t){this.disabled||this.loading||this.overlay&&this.overlay.contains(t.target)||e.DomHandler.hasClass(t.target,"p-multiselect-close")||(e.DomHandler.hasClass(t.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(t,i){if(this.disabled||this.isOptionDisabled(i))return;let l=null;l=this.isSelected(i)?this.modelValue.filter((t=>!e.ObjectUtils.equals(t,this.getOptionValue(i),this.equalityKey))):[...this.modelValue||[],this.getOptionValue(i)],this.$emit("update:modelValue",l),this.$emit("change",{originalEvent:t,value:l})},onOptionKeyDown(e,t){let i=e.target;switch(e.which){case 40:var l=this.findNextItem(i);l&&l.focus(),e.preventDefault();break;case 38:var n=this.findPrevItem(i);n&&n.focus(),e.preventDefault();break;case 13:this.onOptionSelect(e,t),e.preventDefault()}},findNextItem(t){let i=t.nextElementSibling;return i?e.DomHandler.hasClass(i,"p-disabled")||e.DomHandler.hasClass(i,"p-multiselect-item-group")?this.findNextItem(i):i:null},findPrevItem(t){let i=t.previousElementSibling;return i?e.DomHandler.hasClass(i,"p-disabled")||e.DomHandler.hasClass(i,"p-multiselect-item-group")?this.findPrevItem(i):i:null},onOverlayEnter(t){e.ZIndexUtils.set("overlay",t,this.$primevue.config.zIndex.overlay),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},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))},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 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.isAndroid()&&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(e){let t;if(this.options)if(this.optionGroupLabel){for(let i of this.options)if(t=this.findOptionByValue(e,this.getOptionGroupChildren(i)),t)break}else t=this.findOptionByValue(e,this.options);return t?this.getOptionLabel(t):null},findOptionByValue(t,i){for(let l of i){let i=this.getOptionValue(l);if(e.ObjectUtils.equals(i,t,this.equalityKey))return l}return null},onToggleAll(e){let t=null;this.allSelected?t=[]:this.visibleOptions&&(this.optionGroupLabel?(t=[],this.visibleOptions.forEach((e=>t=[...t,...this.getOptionGroupChildren(e)]))):t=this.visibleOptions.map((e=>this.getOptionValue(e)))),this.$emit("update:modelValue",t),this.$emit("change",{originalEvent:e,value:t})},onFilterChange(e){this.$emit("filter",{originalEvent:e,value:e.target.value}),this.overlayVisible&&this.alignOverlay()},overlayRef(e){this.overlay=e},removeChip(t){let i=this.modelValue.filter((i=>!e.ObjectUtils.equals(i,t,this.equalityKey)));this.$emit("update:modelValue",i),this.$emit("change",{originalEvent:event,value:i})},onOverlayClick(e){o.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);l&&l.length&&e.push({...t,items:l})}return e}return i.FilterService.filter(this.options,this.searchFields,this.filterValue,"contains",this.filterLocale)}return 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)}]},panelStyleClass(){return["p-multiselect-panel p-component",this.panelClass,{"p-input-filled":"filled"===this.$primevue.config.inputStyle,"p-ripple-disabled":!1===this.$primevue.config.ripple}]},label(){let e;if(this.modelValue&&this.modelValue.length){e="";for(let t=0;t<this.modelValue.length;t++)0!==t&&(e+=", "),e+=this.getLabelByValue(this.modelValue[t])}else e=this.placeholder;return e},allSelected(){if(this.filterValue&&this.filterValue.trim().length>0){if(0===this.visibleOptions.length)return!1;if(this.optionGroupLabel){for(let e of this.visibleOptions)for(let t of this.getOptionGroupChildren(e))if(!this.isSelected(t))return!1}else for(let e of this.visibleOptions)if(!this.isSelected(e))return!1;return!0}if(this.modelValue&&this.options){let e=0;return this.optionGroupLabel?this.options.forEach((t=>e+=this.getOptionGroupChildren(t).length)):e=this.options.length,e>0&&e===this.modelValue.length}return!1},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},appendTarget(){return this.appendDisabled?null:this.appendTo},maxSelectionLimitReached(){return this.selectionLimit&&this.modelValue&&this.modelValue.length===this.selectionLimit},dropdownIconClass(){return["p-multiselect-trigger-icon",this.loading?this.loadingIcon:"pi pi-chevron-down"]}},directives:{ripple:s(l).default}};const r={class:"p-hidden-accessible"},c={class:"p-multiselect-label-container"},p={class:"p-multiselect-token-label"},d={class:"p-multiselect-trigger"},u={key:0,class:"p-multiselect-header"},h={class:"p-hidden-accessible"},m={key:1,class:"p-multiselect-filter-container"},b=n.createVNode("span",{class:"p-multiselect-filter-icon pi pi-search"},null,-1),f=n.createVNode("span",{class:"p-multiselect-close-icon pi pi-times"},null,-1),y={class:"p-multiselect-items p-component",role:"listbox","aria-multiselectable":"true"},g={class:"p-checkbox p-component"},v={class:"p-multiselect-item-group"},k={class:"p-checkbox p-component"},x={key:2,class:"p-multiselect-empty-message"},V={key:3,class:"p-multiselect-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-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-item-group {\n cursor: auto;\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 margin-left: auto;\n}\n.p-fluid .p-multiselect {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n}\n"),a.render=function(e,t,i,l,s,o){const a=n.resolveDirective("ripple");return n.openBlock(),n.createBlock("div",{ref:"container",class:o.containerClass,onClick:t[11]||(t[11]=(...e)=>o.onClick&&o.onClick(...e))},[n.createVNode("div",r,[n.createVNode("input",{ref:"focusInput",type:"text",role:"listbox",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":"true","aria-expanded":s.overlayVisible,"aria-labelledby":i.ariaLabelledBy},null,40,["id","disabled","tabindex","aria-expanded","aria-labelledby"])]),n.createVNode("div",c,[n.createVNode("div",{class:o.labelClass},[n.renderSlot(e.$slots,"value",{value:i.modelValue,placeholder:i.placeholder},(()=>["comma"===i.display?(n.openBlock(),n.createBlock(n.Fragment,{key:0},[n.createTextVNode(n.toDisplayString(o.label||"empty"),1)],64)):"chip"===i.display?(n.openBlock(),n.createBlock(n.Fragment,{key:1},[(n.openBlock(!0),n.createBlock(n.Fragment,null,n.renderList(i.modelValue,(t=>(n.openBlock(),n.createBlock("div",{class:"p-multiselect-token",key:o.getLabelByValue(t)},[n.renderSlot(e.$slots,"chip",{value:t},(()=>[n.createVNode("span",p,n.toDisplayString(o.getLabelByValue(t)),1)])),i.disabled?n.createCommentVNode("",!0):(n.openBlock(),n.createBlock("span",{key:0,class:"p-multiselect-token-icon pi pi-times-circle",onClick:e=>o.removeChip(t)},null,8,["onClick"]))])))),128)),i.modelValue&&0!==i.modelValue.length?n.createCommentVNode("",!0):(n.openBlock(),n.createBlock(n.Fragment,{key:0},[n.createTextVNode(n.toDisplayString(i.placeholder||"empty"),1)],64))],64)):n.createCommentVNode("",!0)]))],2)]),n.createVNode("div",d,[n.createVNode("span",{class:o.dropdownIconClass},null,2)]),(n.openBlock(),n.createBlock(n.Teleport,{to:o.appendTarget,disabled:o.appendDisabled},[n.createVNode(n.Transition,{name:"p-connected-overlay",onEnter:o.onOverlayEnter,onLeave:o.onOverlayLeave,onAfterLeave:o.onOverlayAfterLeave},{default:n.withCtx((()=>[s.overlayVisible?(n.openBlock(),n.createBlock("div",{key:0,ref:o.overlayRef,class:o.panelStyleClass,onClick:t[10]||(t[10]=(...e)=>o.onOverlayClick&&o.onOverlayClick(...e))},[n.renderSlot(e.$slots,"header",{value:i.modelValue,options:o.visibleOptions}),i.showToggleAll&&null==i.selectionLimit||i.filter?(n.openBlock(),n.createBlock("div",u,[i.showToggleAll&&null==i.selectionLimit?(n.openBlock(),n.createBlock("div",{key:0,class:"p-checkbox p-component",onClick:t[6]||(t[6]=(...e)=>o.onToggleAll&&o.onToggleAll(...e)),role:"checkbox","aria-checked":o.allSelected},[n.createVNode("div",h,[n.createVNode("input",{type:"checkbox",readonly:"",onFocus:t[4]||(t[4]=(...e)=>o.onHeaderCheckboxFocus&&o.onHeaderCheckboxFocus(...e)),onBlur:t[5]||(t[5]=(...e)=>o.onHeaderCheckboxBlur&&o.onHeaderCheckboxBlur(...e))},null,32)]),n.createVNode("div",{class:["p-checkbox-box",{"p-highlight":o.allSelected,"p-focus":s.headerCheckboxFocused}],role:"checkbox","aria-checked":o.allSelected},[n.createVNode("span",{class:["p-checkbox-icon",{"pi pi-check":o.allSelected}]},null,2)],10,["aria-checked"])],8,["aria-checked"])):n.createCommentVNode("",!0),i.filter?(n.openBlock(),n.createBlock("div",m,[n.withDirectives(n.createVNode("input",{type:"text",ref:"filterInput","onUpdate:modelValue":t[7]||(t[7]=e=>s.filterValue=e),class:"p-multiselect-filter p-inputtext p-component",placeholder:i.filterPlaceholder,onInput:t[8]||(t[8]=(...e)=>o.onFilterChange&&o.onFilterChange(...e))},null,40,["placeholder"]),[[n.vModelText,s.filterValue]]),b])):n.createCommentVNode("",!0),n.withDirectives(n.createVNode("button",{class:"p-multiselect-close p-link",onClick:t[9]||(t[9]=(...e)=>o.onCloseClick&&o.onCloseClick(...e)),type:"button"},[f],512),[[a]])])):n.createCommentVNode("",!0),n.createVNode("div",{class:"p-multiselect-items-wrapper",style:{"max-height":i.scrollHeight}},[n.createVNode("ul",y,[i.optionGroupLabel?(n.openBlock(!0),n.createBlock(n.Fragment,{key:1},n.renderList(o.visibleOptions,((t,l)=>(n.openBlock(),n.createBlock(n.Fragment,{key:o.getOptionGroupRenderKey(t)},[n.createVNode("li",v,[n.renderSlot(e.$slots,"optiongroup",{option:t,index:l},(()=>[n.createTextVNode(n.toDisplayString(o.getOptionGroupLabel(t)),1)]))]),(n.openBlock(!0),n.createBlock(n.Fragment,null,n.renderList(o.getOptionGroupChildren(t),((t,l)=>n.withDirectives((n.openBlock(),n.createBlock("li",{class:["p-multiselect-item",{"p-highlight":o.isSelected(t),"p-disabled":o.isOptionDisabled(t)}],role:"option","aria-selected":o.isSelected(t),key:o.getOptionRenderKey(t),onClick:e=>o.onOptionSelect(e,t),onKeydown:e=>o.onOptionKeyDown(e,t),tabindex:i.tabindex||"0","aria-label":o.getOptionLabel(t)},[n.createVNode("div",k,[n.createVNode("div",{class:["p-checkbox-box",{"p-highlight":o.isSelected(t)}]},[n.createVNode("span",{class:["p-checkbox-icon",{"pi pi-check":o.isSelected(t)}]},null,2)],2)]),n.renderSlot(e.$slots,"option",{option:t,index:l},(()=>[n.createVNode("span",null,n.toDisplayString(o.getOptionLabel(t)),1)]))],42,["aria-selected","onClick","onKeydown","tabindex","aria-label"])),[[a]]))),128))],64)))),128)):(n.openBlock(!0),n.createBlock(n.Fragment,{key:0},n.renderList(o.visibleOptions,((t,l)=>n.withDirectives((n.openBlock(),n.createBlock("li",{class:["p-multiselect-item",{"p-highlight":o.isSelected(t),"p-disabled":o.isOptionDisabled(t)}],role:"option","aria-selected":o.isSelected(t),key:o.getOptionRenderKey(t),onClick:e=>o.onOptionSelect(e,t),onKeydown:e=>o.onOptionKeyDown(e,t),tabindex:i.tabindex||"0","aria-label":o.getOptionLabel(t)},[n.createVNode("div",g,[n.createVNode("div",{class:["p-checkbox-box",{"p-highlight":o.isSelected(t)}]},[n.createVNode("span",{class:["p-checkbox-icon",{"pi pi-check":o.isSelected(t)}]},null,2)],2)]),n.renderSlot(e.$slots,"option",{option:t,index:l},(()=>[n.createVNode("span",null,n.toDisplayString(o.getOptionLabel(t)),1)]))],42,["aria-selected","onClick","onKeydown","tabindex","aria-label"])),[[a]]))),128)),s.filterValue&&(!o.visibleOptions||o.visibleOptions&&0===o.visibleOptions.length)?(n.openBlock(),n.createBlock("li",x,[n.renderSlot(e.$slots,"emptyfilter",{},(()=>[n.createTextVNode(n.toDisplayString(o.emptyFilterMessageText),1)]))])):!i.options||i.options&&0===i.options.length?(n.openBlock(),n.createBlock("li",V,[n.renderSlot(e.$slots,"empty",{},(()=>[n.createTextVNode(n.toDisplayString(o.emptyMessageText),1)]))])):n.createCommentVNode("",!0)])],4),n.renderSlot(e.$slots,"footer",{value:i.modelValue,options:o.visibleOptions})],2)):n.createCommentVNode("",!0)])),_:3},8,["onEnter","onLeave","onAfterLeave"])],8,["to","disabled"]))],2)},module.exports=a;