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

216 lines
7.4 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2024-02-11 23:48:31 +00:00
<div ref="container" :class="cx('root')" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptmi('root')">
2022-09-14 11:26:01 +00:00
<div
v-for="(option, i) of options"
:key="getOptionRenderKey(option)"
v-ripple
2024-02-14 09:29:39 +00:00
:tabindex="findTabindex(option, i)"
2022-09-14 11:26:01 +00:00
:aria-label="getOptionLabel(option)"
:role="multiple ? 'checkbox' : 'radio'"
:aria-checked="isSelected(option)"
:aria-disabled="isOptionDisabled(option)"
2023-05-25 14:09:22 +00:00
:class="cx('button', { option })"
2022-09-14 11:26:01 +00:00
@click="onOptionSelect($event, option, i)"
@keydown="onKeydown($event, option, i)"
@focus="onFocus($event)"
@blur="onBlur($event, option)"
2023-05-09 08:43:08 +00:00
v-bind="getPTOptions(option, 'button')"
2023-05-25 14:09:22 +00:00
:data-p-highlight="isSelected(option)"
:data-p-disabled="isOptionDisabled(option)"
2022-09-14 11:26:01 +00:00
>
2023-05-25 14:09:22 +00:00
<slot name="option" :option="option" :index="i" :class="cx('label')">
<span :class="cx('label')" v-bind="getPTOptions(option, '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';
2023-05-25 14:09:22 +00:00
import BaseSelectButton from './BaseSelectButton.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'SelectButton',
2023-05-25 14:09:22 +00:00
extends: BaseSelectButton,
2024-02-11 23:48:31 +00:00
inheritAttrs: false,
2022-09-06 12:03:37 +00:00
emits: ['update:modelValue', 'focus', 'blur', 'change'],
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() {
2023-05-25 14:09:22 +00:00
let opts = DomHandler.find(this.$refs.container, '[data-pc-section="button"]');
let firstHighlight = DomHandler.findSingle(this.$refs.container, '[data-p-highlight="true"]');
2022-09-06 12:03:37 +00:00
for (let i = 0; i < opts.length; i++) {
2023-05-25 14:09:22 +00:00
if ((DomHandler.getAttribute(opts[i], 'data-p-highlight') === true && ObjectUtils.equals(opts[i], firstHighlight)) || (firstHighlight === null && i == 0)) {
2022-09-06 12:03:37 +00:00
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);
},
2023-05-09 08:43:08 +00:00
getPTOptions(option, key) {
return this.ptm(key, {
context: {
active: this.isSelected(option),
disabled: this.isOptionDisabled(option),
option
2023-05-09 08:43:08 +00:00
}
});
},
2022-09-06 12:03:37 +00:00
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 && this.allowEmpty)) {
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.onArrowRightKey(event.target);
2022-09-06 12:03:37 +00:00
event.preventDefault();
break;
}
case 'ArrowUp':
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
case 'ArrowLeft': {
this.onArrowLeftKey(event.target);
2022-09-06 12:03:37 +00:00
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
}
},
onArrowRightKey(target) {
const nextEl = this.findNextElement(target);
2022-09-14 11:26:01 +00:00
if (nextEl) {
this.focusedIndex = ObjectUtils.findIndexInList(nextEl, this.findAllElements());
DomHandler.focus(nextEl);
2022-09-06 12:03:37 +00:00
}
},
onArrowLeftKey(target) {
const prevEl = this.findPrevElement(target);
2022-09-06 12:03:37 +00:00
if (prevEl) {
this.focusedIndex = ObjectUtils.findIndexInList(prevEl, this.findAllElements());
DomHandler.focus(prevEl);
2022-09-06 12:03:37 +00:00
}
},
findAllElements() {
return DomHandler.find(this.$refs.container, '[data-pc-section="button"]');
},
findNextElement(target) {
if (target.nextElementSibling) {
if (DomHandler.getAttribute(target.nextElementSibling, 'data-p-disabled')) {
return this.findNextElement(target.nextElementSibling);
}
2022-09-06 12:03:37 +00:00
return target.nextElementSibling;
}
return null;
},
findPrevElement(target) {
if (target.previousElementSibling) {
if (DomHandler.getAttribute(target.previousElementSibling, 'data-p-disabled')) {
return this.findPrevElement(target.previousElementSibling);
}
return target.previousElementSibling;
}
return null;
2022-09-06 12:03:37 +00:00
},
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);
2024-02-14 09:29:39 +00:00
},
findTabindex(option, index) {
return this.disabled || this.isOptionDisabled(option) || index !== this.focusedIndex ? '-1' : '0';
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
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>