2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2024-03-21 13:21:24 +00:00
|
|
|
<div :class="cx('root')" role="group" :aria-labelledby="ariaLabelledby" v-bind="ptmi('root')">
|
|
|
|
<button
|
2022-09-14 11:26:01 +00:00
|
|
|
v-for="(option, i) of options"
|
|
|
|
:key="getOptionRenderKey(option)"
|
|
|
|
v-ripple
|
2024-03-21 13:21:24 +00:00
|
|
|
:aria-pressed="isSelected(option)"
|
2024-02-08 06:51:58 +00:00
|
|
|
: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)"
|
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>
|
2024-03-21 13:21:24 +00:00
|
|
|
</button>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import Ripple from 'primevue/ripple';
|
2024-03-21 13:21:24 +00:00
|
|
|
import { 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,
|
2024-03-21 13:21:24 +00:00
|
|
|
emits: ['update:modelValue', 'change'],
|
2022-09-06 12:03:37 +00:00
|
|
|
methods: {
|
2024-03-21 13:21:24 +00:00
|
|
|
getOptionLabel(option) {
|
2022-09-06 12:03:37 +00:00
|
|
|
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),
|
2023-08-18 07:08:25 +00:00
|
|
|
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
|
|
|
|
2023-10-17 06:32:11 +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;
|
|
|
|
}
|
|
|
|
},
|
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>
|