primevue-mirror/components/lib/accordion/Accordion.vue

254 lines
10 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<div :class="cx('root')" v-bind="ptm('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
>
<component v-if="tab.children && tab.children.headericon" :is="tab.children.headericon" :isTabActive="isTabActive(i)" :index="i"></component>
<component v-else-if="isTabActive(i)" :is="collapseIcon ? 'span' : 'ChevronDownIcon'" :class="[cx('tab.headerIcon'), collapseIcon]" aria-hidden="true" v-bind="getTabPT(tab, 'headericon', i)" />
<component v-else :is="expandIcon ? 'span' : 'ChevronRightIcon'" :class="[cx('tab.headerIcon'), expandIcon]" aria-hidden="true" v-bind="getTabPT(tab, 'headericon', i)" />
<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>
<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')"
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
>
<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>
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';
2022-12-08 11:04:25 +00:00
import { DomHandler, UniqueComponentId } from 'primevue/utils';
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,
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 {
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: {
'$attrs.id': function (newValue) {
this.id = newValue || UniqueComponentId();
},
2022-09-06 12:03:37 +00:00
activeIndex(newValue) {
this.d_activeIndex = newValue;
}
},
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`;
},
2023-05-23 21:10:36 +00:00
getTabPT(tab, key, index) {
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 || {},
parent: {
props: this.$props,
state: this.$data
2023-05-23 21:10:36 +00:00
},
context: {
index,
count,
first: index === 0,
last: index === count - 1,
active: this.isTabActive(index)
}
2023-05-23 21:10:36 +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));
},
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':
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() {
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
},
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>