primevue-mirror/components/lib/selectbutton/SelectButton.vue

210 lines
6.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div ref="container" :class="containerClass" role="group" :aria-labelledby="ariaLabelledby">
2022-09-14 11:26:01 +00:00
<div
v-for="(option, i) of options"
:key="getOptionRenderKey(option)"
v-ripple
:tabindex="i === focusedIndex ? '0' : '-1'"
:aria-label="getOptionLabel(option)"
:role="multiple ? 'checkbox' : 'radio'"
:aria-checked="isSelected(option)"
:aria-disabled="optionDisabled"
:class="getButtonClass(option, i)"
@click="onOptionSelect($event, option, i)"
@keydown="onKeydown($event, option, i)"
@focus="onFocus($event)"
@blur="onBlur($event, option)"
>
2022-09-06 12:03:37 +00:00
<slot name="option" :option="option" :index="i">
2022-09-14 11:26:01 +00:00
<span class="p-button-label">{{ getOptionLabel(option) }}</span>
2022-09-06 12:03:37 +00:00
</slot>
</div>
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'SelectButton',
emits: ['update:modelValue', 'focus', 'blur', 'change'],
props: {
modelValue: null,
options: Array,
optionLabel: null,
optionValue: null,
optionDisabled: null,
2022-09-14 11:26:01 +00:00
multiple: Boolean,
2022-09-06 12:03:37 +00:00
unselectable: {
type: Boolean,
default: false
2022-09-06 12:03:37 +00:00
},
disabled: Boolean,
dataKey: null,
'aria-labelledby': {
type: String,
2022-09-14 11:26:01 +00:00
default: null
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
focusedIndex: 0
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
mounted() {
this.defaultTabIndexes();
},
methods: {
defaultTabIndexes() {
let opts = DomHandler.find(this.$refs.container, '.p-button');
let firstHighlight = DomHandler.findSingle(this.$refs.container, '.p-highlight');
for (let i = 0; i < opts.length; i++) {
if ((DomHandler.hasClass(opts[i], 'p-highlight') && ObjectUtils.equals(opts[i], firstHighlight)) || (firstHighlight === null && i == 0)) {
this.focusedIndex = i;
}
}
},
getOptionLabel(option) {
return this.optionLabel ? ObjectUtils.resolveFieldData(option, this.optionLabel) : option;
},
getOptionValue(option) {
return this.optionValue ? ObjectUtils.resolveFieldData(option, this.optionValue) : option;
},
getOptionRenderKey(option) {
return this.dataKey ? ObjectUtils.resolveFieldData(option, this.dataKey) : this.getOptionLabel(option);
},
isOptionDisabled(option) {
return this.optionDisabled ? ObjectUtils.resolveFieldData(option, this.optionDisabled) : false;
},
onOptionSelect(event, option, index) {
if (this.disabled || this.isOptionDisabled(option)) {
return;
}
let selected = this.isSelected(option);
2022-09-14 11:26:01 +00:00
if (selected && this.unselectable) {
2022-09-06 12:03:37 +00:00
return;
}
let optionValue = this.getOptionValue(option);
let newValue;
if (this.multiple) {
2022-09-14 11:26:01 +00:00
if (selected) newValue = this.modelValue.filter((val) => !ObjectUtils.equals(val, optionValue, this.equalityKey));
else newValue = this.modelValue ? [...this.modelValue, optionValue] : [optionValue];
} else {
2022-09-06 12:03:37 +00:00
newValue = selected ? null : optionValue;
}
this.focusedIndex = index;
this.$emit('update:modelValue', newValue);
2022-09-14 11:26:01 +00:00
this.$emit('change', { event: event, value: newValue });
2022-09-06 12:03:37 +00:00
},
isSelected(option) {
let selected = false;
let optionValue = this.getOptionValue(option);
if (this.multiple) {
if (this.modelValue) {
for (let val of this.modelValue) {
if (ObjectUtils.equals(val, optionValue, this.equalityKey)) {
selected = true;
break;
}
}
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
selected = ObjectUtils.equals(this.modelValue, optionValue, this.equalityKey);
}
return selected;
},
onKeydown(event, option, index) {
switch (event.code) {
case 'Space': {
this.onOptionSelect(event, option, index);
event.preventDefault();
break;
}
case 'ArrowDown':
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
case 'ArrowRight': {
this.changeTabIndexes(event, 'next');
event.preventDefault();
break;
}
case 'ArrowUp':
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
case 'ArrowLeft': {
this.changeTabIndexes(event, 'prev');
event.preventDefault();
break;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
default:
//no op
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
}
},
changeTabIndexes(event, direction) {
let firstTabableChild, index;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let i = 0; i <= this.$refs.container.children.length - 1; i++) {
2022-09-14 11:26:01 +00:00
if (this.$refs.container.children[i].getAttribute('tabindex') === '0') firstTabableChild = { elem: this.$refs.container.children[i], index: i };
2022-09-06 12:03:37 +00:00
}
if (direction === 'prev') {
if (firstTabableChild.index === 0) index = this.$refs.container.children.length - 1;
else index = firstTabableChild.index - 1;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (firstTabableChild.index === this.$refs.container.children.length - 1) index = 0;
else index = firstTabableChild.index + 1;
}
this.focusedIndex = index;
this.$refs.container.children[index].focus();
},
onFocus(event) {
this.$emit('focus', event);
},
onBlur(event, option) {
if (event.target && event.relatedTarget && event.target.parentElement !== event.relatedTarget.parentElement) {
this.defaultTabIndexes();
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$emit('blur', event, option);
},
getButtonClass(option) {
2022-09-14 11:26:01 +00:00
return [
'p-button p-component',
{
'p-highlight': this.isSelected(option),
'p-disabled': this.isOptionDisabled(option)
}
];
2022-09-06 12:03:37 +00:00
}
},
2022-09-14 11:26:01 +00:00
computed: {
2022-09-06 12:03:37 +00:00
containerClass() {
2022-09-14 11:26:01 +00:00
return [
'p-selectbutton p-buttonset p-component',
{
'p-disabled': this.disabled
}
];
2022-09-06 12:03:37 +00:00
},
equalityKey() {
return this.optionValue ? null : this.dataKey;
}
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>