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

114 lines
3.9 KiB
Vue
Raw Normal View History

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')">
2024-03-26 10:25:08 +00:00
<template v-for="(option, index) of options" :key="getOptionRenderKey(option)">
2024-03-26 12:44:29 +00:00
<ToggleButton
:modelValue="isSelected(option)"
:onLabel="getOptionLabel(option)"
:offLabel="getOptionLabel(option)"
:disabled="disabled || isOptionDisabled(option)"
2024-03-26 12:44:29 +00:00
:unstyled="unstyled"
@change="onOptionSelect($event, option, index)"
:pt="ptm('button')"
>
2024-03-26 10:25:08 +00:00
<template v-if="$slots.option" #default>
<slot name="option" :option="option" :index="index">
<span v-bind="ptm('button')['label']">{{ getOptionLabel(option) }}</span>
</slot>
</template>
</ToggleButton>
</template>
2022-09-06 12:03:37 +00:00
</div>
</template>
<script>
import Ripple from 'primevue/ripple';
2024-03-26 10:25:08 +00:00
import ToggleButton from 'primevue/togglebutton';
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),
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;
}
},
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
2024-03-26 10:25:08 +00:00
},
components: {
ToggleButton
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>