2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<div class="p-accordion p-component">
|
2022-09-14 11:26:01 +00:00
|
|
|
<div v-for="(tab, i) of tabs" :key="getKey(tab, i)" :class="getTabClass(i)" :data-index="i">
|
|
|
|
<div :style="getTabProp(tab, 'headerStyle')" :class="getTabHeaderClass(tab, i)" v-bind="getTabProp(tab, 'headerProps')">
|
|
|
|
<a
|
|
|
|
:id="getTabHeaderActionId(i)"
|
|
|
|
class="p-accordion-header-link p-accordion-header-action"
|
|
|
|
:tabindex="getTabProp(tab, 'disabled') ? -1 : tabindex"
|
|
|
|
role="button"
|
|
|
|
:aria-disabled="getTabProp(tab, 'disabled')"
|
|
|
|
:aria-expanded="isTabActive(i)"
|
|
|
|
:aria-controls="getTabContentId(i)"
|
|
|
|
@click="onTabClick($event, tab, i)"
|
|
|
|
@keydown="onTabKeyDown($event, tab, i)"
|
|
|
|
v-bind="getTabProp(tab, 'headerActionProps')"
|
|
|
|
>
|
|
|
|
<span :class="getTabHeaderIconClass(i)" aria-hidden="true"></span>
|
|
|
|
<span v-if="tab.props && tab.props.header" class="p-accordion-header-text">{{ tab.props.header }}</span>
|
|
|
|
<component v-if="tab.children && tab.children.header" :is="tab.children.header"></component>
|
2022-09-06 12:03:37 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<transition name="p-toggleable-content">
|
2022-09-14 11:26:01 +00:00
|
|
|
<div
|
|
|
|
v-if="lazy ? isTabActive(i) : true"
|
|
|
|
v-show="lazy ? true : isTabActive(i)"
|
|
|
|
:id="getTabContentId(i)"
|
|
|
|
:style="getTabProp(tab, 'contentStyle')"
|
|
|
|
:class="getTabContentClass(tab)"
|
|
|
|
role="region"
|
|
|
|
:aria-labelledby="getTabHeaderActionId(i)"
|
|
|
|
v-bind="getTabProp(tab, 'contentProps')"
|
|
|
|
>
|
2022-09-06 12:03:37 +00:00
|
|
|
<div class="p-accordion-content">
|
|
|
|
<component :is="tab"></component>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-09-14 11:26:01 +00:00
|
|
|
import Ripple from 'primevue/ripple';
|
2022-12-08 11:04:25 +00:00
|
|
|
import { DomHandler, UniqueComponentId } from 'primevue/utils';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Accordion',
|
2022-09-14 11:26:01 +00:00
|
|
|
emits: ['update:activeIndex', 'tab-open', 'tab-close', 'tab-click'],
|
2022-09-06 12:03:37 +00:00
|
|
|
props: {
|
|
|
|
multiple: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
activeIndex: {
|
2022-09-14 11:26:01 +00:00
|
|
|
type: [Number, Array],
|
2022-09-06 12:03:37 +00:00
|
|
|
default: null
|
|
|
|
},
|
|
|
|
lazy: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
expandIcon: {
|
|
|
|
type: String,
|
2022-12-08 11:04:25 +00:00
|
|
|
default: 'pi pi-chevron-right'
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
collapseIcon: {
|
|
|
|
type: String,
|
2022-12-08 11:04:25 +00:00
|
|
|
default: 'pi pi-chevron-down'
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
|
|
|
tabindex: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
selectOnFocus: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2023-01-19 04:01:03 +00:00
|
|
|
id: this.$attrs.id,
|
2022-09-06 12:03:37 +00:00
|
|
|
d_activeIndex: this.activeIndex
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2023-01-19 04:01:03 +00:00
|
|
|
'$attrs.id': function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
activeIndex(newValue) {
|
|
|
|
this.d_activeIndex = newValue;
|
|
|
|
}
|
|
|
|
},
|
2023-01-19 04:01:03 +00:00
|
|
|
mounted() {
|
|
|
|
this.id = this.id || UniqueComponentId();
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
methods: {
|
2022-09-14 11:26:01 +00:00
|
|
|
isAccordionTab(child) {
|
|
|
|
return child.type.name === 'AccordionTab';
|
|
|
|
},
|
|
|
|
isTabActive(index) {
|
|
|
|
return this.multiple ? this.d_activeIndex && this.d_activeIndex.includes(index) : this.d_activeIndex === index;
|
|
|
|
},
|
|
|
|
getTabProp(tab, name) {
|
|
|
|
return tab.props ? tab.props[name] : undefined;
|
|
|
|
},
|
|
|
|
getKey(tab, index) {
|
|
|
|
return this.getTabProp(tab, 'header') || index;
|
|
|
|
},
|
|
|
|
getTabHeaderActionId(index) {
|
|
|
|
return `${this.id}_${index}_header_action`;
|
|
|
|
},
|
|
|
|
getTabContentId(index) {
|
|
|
|
return `${this.id}_${index}_content`;
|
|
|
|
},
|
|
|
|
onTabClick(event, tab, index) {
|
|
|
|
this.changeActiveIndex(event, tab, index);
|
|
|
|
this.$emit('tab-click', { originalEvent: event, index });
|
|
|
|
},
|
|
|
|
onTabKeyDown(event, tab, index) {
|
|
|
|
switch (event.code) {
|
|
|
|
case 'ArrowDown':
|
|
|
|
this.onTabArrowDownKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ArrowUp':
|
|
|
|
this.onTabArrowUpKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Home':
|
|
|
|
this.onTabHomeKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'End':
|
|
|
|
this.onTabEndKey(event);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Enter':
|
|
|
|
case 'Space':
|
|
|
|
this.onTabEnterKey(event, tab, index);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onTabArrowDownKey(event) {
|
|
|
|
const nextHeaderAction = this.findNextHeaderAction(event.target.parentElement.parentElement);
|
|
|
|
|
|
|
|
nextHeaderAction ? this.changeFocusedTab(event, nextHeaderAction) : this.onTabHomeKey(event);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onTabArrowUpKey(event) {
|
|
|
|
const prevHeaderAction = this.findPrevHeaderAction(event.target.parentElement.parentElement);
|
|
|
|
|
|
|
|
prevHeaderAction ? this.changeFocusedTab(event, prevHeaderAction) : this.onTabEndKey(event);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onTabHomeKey(event) {
|
|
|
|
const firstHeaderAction = this.findFirstHeaderAction();
|
|
|
|
|
|
|
|
this.changeFocusedTab(event, firstHeaderAction);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onTabEndKey(event) {
|
|
|
|
const lastHeaderAction = this.findLastHeaderAction();
|
|
|
|
|
|
|
|
this.changeFocusedTab(event, lastHeaderAction);
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onTabEnterKey(event, tab, index) {
|
|
|
|
this.changeActiveIndex(event, tab, index);
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
findNextHeaderAction(tabElement, selfCheck = false) {
|
|
|
|
const nextTabElement = selfCheck ? tabElement : tabElement.nextElementSibling;
|
|
|
|
const headerElement = DomHandler.findSingle(nextTabElement, '.p-accordion-header');
|
|
|
|
|
|
|
|
return headerElement ? (DomHandler.hasClass(headerElement, 'p-disabled') ? this.findNextHeaderAction(headerElement.parentElement) : DomHandler.findSingle(headerElement, '.p-accordion-header-action')) : null;
|
|
|
|
},
|
|
|
|
findPrevHeaderAction(tabElement, selfCheck = false) {
|
|
|
|
const prevTabElement = selfCheck ? tabElement : tabElement.previousElementSibling;
|
|
|
|
const headerElement = DomHandler.findSingle(prevTabElement, '.p-accordion-header');
|
|
|
|
|
|
|
|
return headerElement ? (DomHandler.hasClass(headerElement, 'p-disabled') ? this.findPrevHeaderAction(headerElement.parentElement) : DomHandler.findSingle(headerElement, '.p-accordion-header-action')) : null;
|
|
|
|
},
|
|
|
|
findFirstHeaderAction() {
|
|
|
|
return this.findNextHeaderAction(this.$el.firstElementChild, true);
|
|
|
|
},
|
|
|
|
findLastHeaderAction() {
|
|
|
|
return this.findPrevHeaderAction(this.$el.lastElementChild, true);
|
|
|
|
},
|
|
|
|
changeActiveIndex(event, tab, index) {
|
|
|
|
if (!this.getTabProp(tab, 'disabled')) {
|
|
|
|
const active = this.isTabActive(index);
|
2022-09-06 12:03:37 +00:00
|
|
|
const eventName = active ? 'tab-close' : 'tab-open';
|
|
|
|
|
|
|
|
if (this.multiple) {
|
|
|
|
if (active) {
|
2022-09-14 11:26:01 +00:00
|
|
|
this.d_activeIndex = this.d_activeIndex.filter((i) => i !== index);
|
|
|
|
} else {
|
|
|
|
if (this.d_activeIndex) this.d_activeIndex.push(index);
|
|
|
|
else this.d_activeIndex = [index];
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
|
|
|
this.d_activeIndex = this.d_activeIndex === index ? null : index;
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$emit('update:activeIndex', this.d_activeIndex);
|
2022-09-14 11:26:01 +00:00
|
|
|
this.$emit(eventName, { originalEvent: event, index });
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
changeFocusedTab(event, element) {
|
|
|
|
if (element) {
|
|
|
|
DomHandler.focus(element);
|
|
|
|
|
|
|
|
if (this.selectOnFocus) {
|
|
|
|
const index = parseInt(element.parentElement.parentElement.dataset.index, 10);
|
|
|
|
const tab = this.tabs[index];
|
|
|
|
|
|
|
|
this.changeActiveIndex(event, tab, index);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getTabClass(i) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-accordion-tab',
|
|
|
|
{
|
|
|
|
'p-accordion-tab-active': this.isTabActive(i)
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
getTabHeaderClass(tab, i) {
|
2022-09-14 11:26:01 +00:00
|
|
|
return [
|
|
|
|
'p-accordion-header',
|
|
|
|
this.getTabProp(tab, 'headerClass'),
|
|
|
|
{
|
|
|
|
'p-highlight': this.isTabActive(i),
|
|
|
|
'p-disabled': this.getTabProp(tab, 'disabled')
|
|
|
|
}
|
|
|
|
];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
getTabHeaderIconClass(i) {
|
2022-12-08 11:04:25 +00:00
|
|
|
return ['p-accordion-toggle-icon', this.isTabActive(i) ? this.collapseIcon : this.expandIcon];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
getTabContentClass(tab) {
|
|
|
|
return ['p-toggleable-content', this.getTabProp(tab, 'contentClass')];
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
tabs() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return this.$slots.default().reduce((tabs, child) => {
|
|
|
|
if (this.isAccordionTab(child)) {
|
|
|
|
tabs.push(child);
|
|
|
|
} else if (child.children && child.children instanceof Array) {
|
|
|
|
child.children.forEach((nestedChild) => {
|
|
|
|
if (this.isAccordionTab(nestedChild)) {
|
|
|
|
tabs.push(nestedChild);
|
|
|
|
}
|
|
|
|
});
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
|
|
|
return tabs;
|
|
|
|
}, []);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
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>
|
|
|
|
|
|
|
|
<style>
|
2022-09-14 11:26:01 +00:00
|
|
|
.p-accordion-header-action {
|
2022-09-06 12:03:37 +00:00
|
|
|
cursor: pointer;
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
user-select: none;
|
|
|
|
position: relative;
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
.p-accordion-header-action:focus {
|
2022-09-06 12:03:37 +00:00
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
.p-accordion-header-text {
|
|
|
|
line-height: 1;
|
|
|
|
}
|
|
|
|
</style>
|