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) 3.9 kB
import e from"primevue/ripple";import{DomHandler as t,ObjectUtils as i}from"primevue/utils";import{resolveDirective as l,openBlock as n,createElementBlock as a,normalizeClass as o,Fragment as s,renderList as r,withDirectives as d,renderSlot as u,createElementVNode as h,toDisplayString as p}from"vue";var c={name:"SelectButton",emits:["update:modelValue","focus","blur","change"],props:{modelValue:null,options:Array,optionLabel:null,optionValue:null,optionDisabled:null,multiple:Boolean,unselectable:{type:Boolean,default:!0},disabled:Boolean,dataKey:null,"aria-labelledby":{type:String,default:null}},data:()=>({focusedIndex:0}),mounted(){this.defaultTabIndexes()},methods:{defaultTabIndexes(){let e=t.find(this.$refs.container,".p-button"),l=t.findSingle(this.$refs.container,".p-highlight");for(let n=0;n<e.length;n++)(t.hasClass(e[n],"p-highlight")&&i.equals(e[n],l)||null===l&&0==n)&&(this.focusedIndex=n)},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)},onOptionSelect(e,t,l){if(this.disabled||this.isOptionDisabled(t))return;let n=this.isSelected(t);if(n&&!this.unselectable)return;let a,o=this.getOptionValue(t);a=this.multiple?n?this.modelValue.filter((e=>!i.equals(e,o,this.equalityKey))):this.modelValue?[...this.modelValue,o]:[o]:n?null:o,this.focusedIndex=l,this.$emit("update:modelValue",a),this.$emit("change",{event:e,value:a})},isSelected(e){let t=!1,l=this.getOptionValue(e);if(this.multiple){if(this.modelValue)for(let e of this.modelValue)if(i.equals(e,l,this.equalityKey)){t=!0;break}}else t=i.equals(this.modelValue,l,this.equalityKey);return t},onKeydown(e,t,i){switch(e.code){case"Space":this.onOptionSelect(e,t,i),e.preventDefault();break;case"ArrowDown":case"ArrowRight":this.changeTabIndexes(e,"next"),e.preventDefault();break;case"ArrowUp":case"ArrowLeft":this.changeTabIndexes(e,"prev"),e.preventDefault()}},changeTabIndexes(e,t){let i,l;for(let e=0;e<=this.$refs.container.children.length-1;e++)"0"===this.$refs.container.children[e].getAttribute("tabindex")&&(i={elem:this.$refs.container.children[e],index:e});l="prev"===t?0===i.index?this.$refs.container.children.length-1:i.index-1:i.index===this.$refs.container.children.length-1?0:i.index+1,this.focusedIndex=l,this.$refs.container.children[l].focus()},onFocus(e){this.$emit("focus",e)},onBlur(e,t){e.target&&e.relatedTarget&&e.target.parentElement!==e.relatedTarget.parentElement&&this.defaultTabIndexes(),this.$emit("blur",e,t)},getButtonClass(e){return["p-button p-component",{"p-highlight":this.isSelected(e),"p-disabled":this.isOptionDisabled(e)}]}},computed:{containerClass(){return["p-selectbutton p-buttonset p-component",{"p-disabled":this.disabled}]},equalityKey(){return this.optionValue?null:this.dataKey}},directives:{ripple:e}};const b=["aria-labelledby"],f=["tabindex","aria-label","role","aria-checked","aria-disabled","onClick","onKeydown","onBlur"],g={class:"p-button-label"};c.render=function(e,t,i,c,m,y){const x=l("ripple");return n(),a("div",{ref:"container",class:o(y.containerClass),role:"group","aria-labelledby":e.ariaLabelledby},[(n(!0),a(s,null,r(i.options,((l,s)=>d((n(),a("div",{key:y.getOptionRenderKey(l),tabindex:s===m.focusedIndex?"0":"-1","aria-label":y.getOptionLabel(l),role:i.multiple?"checkbox":"radio","aria-checked":y.isSelected(l),"aria-disabled":i.optionDisabled,class:o(y.getButtonClass(l,s)),onClick:e=>y.onOptionSelect(e,l,s),onKeydown:e=>y.onKeydown(e,l,s),onFocus:t[0]||(t[0]=e=>y.onFocus(e)),onBlur:e=>y.onBlur(e,l)},[u(e.$slots,"option",{option:l,index:s},(()=>[h("span",g,p(y.getOptionLabel(l)),1)]))],42,f)),[[x]]))),128))],10,b)};export{c as default};