2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2024-02-12 13:02:20 +00:00
|
|
|
<div :class="cx('root')" v-bind="ptmi('root')">
|
2023-05-24 01:03:54 +00:00
|
|
|
<div v-for="(tab, i) of tabs" :key="getKey(tab, i)" :class="cx('tab.root', { tab, index: i })" v-bind="getTabPT(tab, 'root', i)" data-pc-name="accordiontab" :data-pc-index="i" :data-p-active="isTabActive(i)">
|
2023-05-23 23:40:14 +00:00
|
|
|
<div
|
|
|
|
:style="getTabProp(tab, 'headerStyle')"
|
|
|
|
:class="[cx('tab.header', { tab, index: i }), getTabProp(tab, 'headerClass')]"
|
|
|
|
v-bind="{ ...getTabProp(tab, 'headerProps'), ...getTabPT(tab, 'header', i) }"
|
|
|
|
:data-p-highlight="isTabActive(i)"
|
|
|
|
:data-p-disabled="getTabProp(tab, 'disabled')"
|
|
|
|
>
|
2022-09-14 11:26:01 +00:00
|
|
|
<a
|
|
|
|
:id="getTabHeaderActionId(i)"
|
2023-05-23 23:40:14 +00:00
|
|
|
:class="cx('tab.headerAction')"
|
2022-09-14 11:26:01 +00:00
|
|
|
: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)"
|
2023-05-23 21:10:36 +00:00
|
|
|
v-bind="{ ...getTabProp(tab, 'headeractionprops'), ...getTabPT(tab, 'headeraction', i) }"
|
2022-09-14 11:26:01 +00:00
|
|
|
>
|
2023-11-09 10:33:08 +00:00
|
|
|
<component v-if="tab.children && tab.children.headericon" :is="tab.children.headericon" :isTabActive="isTabActive(i)" :active="isTabActive(i)" :index="i"></component>
|
2023-10-24 16:08:31 +00:00
|
|
|
<component
|
|
|
|
v-else-if="isTabActive(i)"
|
|
|
|
:is="$slots.collapseicon ? $slots.collapseicon : collapseIcon ? 'span' : 'ChevronDownIcon'"
|
|
|
|
:class="[cx('tab.headerIcon'), collapseIcon]"
|
|
|
|
aria-hidden="true"
|
|
|
|
v-bind="getTabPT(tab, 'headericon', i)"
|
|
|
|
/>
|
|
|
|
<component v-else :is="$slots.expandicon ? $slots.expandicon : expandIcon ? 'span' : 'ChevronRightIcon'" :class="[cx('tab.headerIcon'), expandIcon]" aria-hidden="true" v-bind="getTabPT(tab, 'headericon', i)" />
|
2023-05-24 00:59:42 +00:00
|
|
|
<span v-if="tab.props && tab.props.header" :class="cx('tab.headerTitle')" v-bind="getTabPT(tab, 'headertitle', i)">{{ tab.props.header }}</span>
|
2022-09-14 11:26:01 +00:00
|
|
|
<component v-if="tab.children && tab.children.header" :is="tab.children.header"></component>
|
2022-09-06 12:03:37 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
2023-08-02 11:59:30 +00:00
|
|
|
<transition name="p-toggleable-content" v-bind="getTabPT(tab, 'transition', i)">
|
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')"
|
2023-05-23 23:40:14 +00:00
|
|
|
:class="[cx('tab.toggleableContent'), getTabProp(tab, 'contentClass')]"
|
2022-09-14 11:26:01 +00:00
|
|
|
role="region"
|
|
|
|
:aria-labelledby="getTabHeaderActionId(i)"
|
2023-05-23 21:10:36 +00:00
|
|
|
v-bind="{ ...getTabProp(tab, 'contentProps'), ...getTabPT(tab, 'toggleablecontent', i) }"
|
2022-09-14 11:26:01 +00:00
|
|
|
>
|
2023-05-24 00:59:42 +00:00
|
|
|
<div :class="cx('tab.content')" v-bind="getTabPT(tab, 'content', i)">
|
2022-09-06 12:03:37 +00:00
|
|
|
<component :is="tab"></component>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</transition>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-04-18 12:53:43 +00:00
|
|
|
import ChevronDownIcon from 'primevue/icons/chevrondown';
|
|
|
|
import ChevronRightIcon from 'primevue/icons/chevronright';
|
2022-09-14 11:26:01 +00:00
|
|
|
import Ripple from 'primevue/ripple';
|
2023-12-22 11:17:54 +00:00
|
|
|
import { DomHandler, UniqueComponentId } from 'primevue/utils';
|
2023-07-13 11:12:07 +00:00
|
|
|
import { mergeProps } from 'vue';
|
2023-05-23 23:40:14 +00:00
|
|
|
import BaseAccordion from './BaseAccordion.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Accordion',
|
2023-05-23 23:40:14 +00:00
|
|
|
extends: BaseAccordion,
|
2024-02-12 13:02:20 +00:00
|
|
|
inheritAttrs: false,
|
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
|
|
|
data() {
|
|
|
|
return {
|
2023-01-19 04:01:03 +00:00
|
|
|
id: this.$attrs.id,
|
2023-12-22 11:17:54 +00:00
|
|
|
d_activeIndex: this.activeIndex
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2024-03-27 02:37:32 +00:00
|
|
|
'$attrs.id': {
|
|
|
|
immediate: true,
|
|
|
|
handler: function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
|
|
|
},
|
2023-01-19 04:01:03 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
activeIndex(newValue) {
|
|
|
|
this.d_activeIndex = newValue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2023-12-22 11:17:54 +00:00
|
|
|
isAccordionTab(child) {
|
|
|
|
return child.type.name === 'AccordionTab';
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
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`;
|
|
|
|
},
|
2023-05-23 21:10:36 +00:00
|
|
|
getTabPT(tab, key, index) {
|
2023-05-29 13:38:47 +00:00
|
|
|
const count = this.tabs.length;
|
2023-05-23 21:10:36 +00:00
|
|
|
const tabMetaData = {
|
2023-06-01 14:37:52 +00:00
|
|
|
props: tab.props || {},
|
2023-03-22 12:11:03 +00:00
|
|
|
parent: {
|
2023-12-05 11:06:32 +00:00
|
|
|
instance: this,
|
2023-03-22 12:11:03 +00:00
|
|
|
props: this.$props,
|
|
|
|
state: this.$data
|
2023-05-23 21:10:36 +00:00
|
|
|
},
|
|
|
|
context: {
|
2023-05-29 13:38:47 +00:00
|
|
|
index,
|
|
|
|
count,
|
|
|
|
first: index === 0,
|
2023-05-29 15:38:17 +00:00
|
|
|
last: index === count - 1,
|
|
|
|
active: this.isTabActive(index)
|
2023-03-22 12:11:03 +00:00
|
|
|
}
|
2023-05-23 21:10:36 +00:00
|
|
|
};
|
|
|
|
|
2023-07-14 09:04:32 +00:00
|
|
|
return mergeProps(this.ptm(`tab.${key}`, { tab: tabMetaData }), this.ptm(`accordiontab.${key}`, { accordiontab: tabMetaData }), this.ptm(`accordiontab.${key}`, tabMetaData), this.ptmo(this.getTabProp(tab, 'pt'), key, tabMetaData));
|
2023-03-22 12:11:03 +00:00
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
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':
|
2023-12-20 10:45:43 +00:00
|
|
|
case 'NumpadEnter':
|
2022-09-14 11:26:01 +00:00
|
|
|
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;
|
2023-05-23 23:40:14 +00:00
|
|
|
const headerElement = DomHandler.findSingle(nextTabElement, '[data-pc-section="header"]');
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-05-23 23:40:14 +00:00
|
|
|
return headerElement ? (DomHandler.getAttribute(headerElement, 'data-p-disabled') ? this.findNextHeaderAction(headerElement.parentElement) : DomHandler.findSingle(headerElement, '[data-pc-section="headeraction"]')) : null;
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
|
|
|
findPrevHeaderAction(tabElement, selfCheck = false) {
|
|
|
|
const prevTabElement = selfCheck ? tabElement : tabElement.previousElementSibling;
|
2023-05-23 23:40:14 +00:00
|
|
|
const headerElement = DomHandler.findSingle(prevTabElement, '[data-pc-section="header"]');
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-05-23 23:40:14 +00:00
|
|
|
return headerElement ? (DomHandler.getAttribute(headerElement, 'data-p-disabled') ? this.findPrevHeaderAction(headerElement.parentElement) : DomHandler.findSingle(headerElement, '[data-pc-section="headeraction"]')) : null;
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
|
|
|
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) {
|
2023-05-23 23:40:14 +00:00
|
|
|
const index = parseInt(element.parentElement.parentElement.dataset.pcIndex, 10);
|
2022-09-14 11:26:01 +00:00
|
|
|
const tab = this.tabs[index];
|
|
|
|
|
|
|
|
this.changeActiveIndex(event, tab, index);
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
tabs() {
|
2023-12-22 11:17:54 +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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return tabs;
|
|
|
|
}, []);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
2023-04-03 08:27:59 +00:00
|
|
|
components: {
|
|
|
|
ChevronDownIcon,
|
|
|
|
ChevronRightIcon
|
|
|
|
},
|
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>
|