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')">
|
2024-04-24 22:42:28 +00:00
|
|
|
<template v-if="hasAccordionTab">
|
|
|
|
<AccordionPanel v-for="(tab, i) of tabs" :key="getKey(tab, i)" :value="`${i}`" :pt="{ root: getTabPT(tab, 'root', i) }" :disabled="getTabProp(tab, 'disabled')">
|
|
|
|
<AccordionHeader :class="getTabProp(tab, 'headerClass')" :pt="getHeaderPT(tab, i)">
|
|
|
|
<component v-if="tab.children && tab.children.headericon" :is="tab.children.headericon" :isTabActive="isItemActive(`${i}`)" :active="isItemActive(`${i}`)" :index="i"></component>
|
|
|
|
<span v-if="tab.props && tab.props.header" v-bind="getTabPT(tab, 'headertitle', i)">{{ tab.props.header }}</span>
|
|
|
|
<template #toggleicon="slotProps">
|
|
|
|
<component
|
|
|
|
v-if="slotProps.active"
|
|
|
|
:is="$slots.collapseicon ? $slots.collapseicon : collapseIcon ? 'span' : 'ChevronDownIcon'"
|
|
|
|
:class="[collapseIcon, slotProps.class]"
|
|
|
|
aria-hidden="true"
|
|
|
|
v-bind="getTabPT(tab, 'headericon', i)"
|
|
|
|
/>
|
|
|
|
<component v-else :is="$slots.expandicon ? $slots.expandicon : expandIcon ? 'span' : 'ChevronUpIcon'" :class="[expandIcon, slotProps.class]" aria-hidden="true" v-bind="getTabPT(tab, 'headericon', i)" />
|
|
|
|
</template>
|
2022-09-14 11:26:01 +00:00
|
|
|
<component v-if="tab.children && tab.children.header" :is="tab.children.header"></component>
|
2024-04-24 22:42:28 +00:00
|
|
|
</AccordionHeader>
|
|
|
|
<AccordionContent :pt="getContentPT(tab, i)">
|
|
|
|
<component :is="tab"></component>
|
|
|
|
</AccordionContent>
|
|
|
|
</AccordionPanel>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<slot v-else></slot>
|
2022-09-06 12:03:37 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-04-24 22:42:28 +00:00
|
|
|
import AccordionContent from 'primevue/accordioncontent';
|
|
|
|
import AccordionHeader from 'primevue/accordionheader';
|
|
|
|
import AccordionPanel from 'primevue/accordionpanel';
|
2023-04-18 12:53:43 +00:00
|
|
|
import ChevronRightIcon from 'primevue/icons/chevronright';
|
2024-04-24 22:42:28 +00:00
|
|
|
import ChevronUpIcon from 'primevue/icons/chevronup';
|
|
|
|
import { 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,
|
2024-04-24 22:42:28 +00:00
|
|
|
emits: ['update:value', '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,
|
2024-04-24 22:42:28 +00:00
|
|
|
d_value: this.value
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2024-04-16 09:35:02 +00:00
|
|
|
'$attrs.id': function (newValue) {
|
|
|
|
this.id = newValue || UniqueComponentId();
|
2023-01-19 04:01:03 +00:00
|
|
|
},
|
2024-04-24 22:42:28 +00:00
|
|
|
value(newValue) {
|
|
|
|
this.d_value = newValue;
|
|
|
|
},
|
|
|
|
activeIndex: {
|
|
|
|
immediate: true,
|
|
|
|
handler(newValue) {
|
|
|
|
if (this.hasAccordionTab) {
|
|
|
|
this.d_value = this.multiple ? newValue?.map(String) : newValue?.toString();
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
2024-04-16 09:35:02 +00:00
|
|
|
mounted() {
|
|
|
|
this.id = this.id || UniqueComponentId();
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
methods: {
|
2024-04-24 22:42:28 +00:00
|
|
|
isItemActive(value) {
|
|
|
|
return this.multiple ? this.d_value?.includes(value) : this.d_value === value;
|
|
|
|
},
|
|
|
|
updateValue(newValue) {
|
|
|
|
const active = this.isItemActive(newValue);
|
|
|
|
|
|
|
|
if (this.multiple) {
|
|
|
|
if (active) {
|
|
|
|
this.d_value = this.d_value.filter((v) => v !== newValue);
|
|
|
|
} else {
|
|
|
|
if (this.d_value) this.d_value.push(newValue);
|
|
|
|
else this.d_value = [newValue];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.d_value = active ? null : newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$emit('update:value', this.d_value);
|
|
|
|
|
|
|
|
// @deprecated since v4.
|
|
|
|
this.$emit('update:activeIndex', this.multiple ? this.d_value?.map(Number) : Number(this.d_value));
|
|
|
|
this.$emit(active ? 'tab-close' : 'tab-open', { originalEvent: undefined, index: Number(newValue) });
|
|
|
|
},
|
|
|
|
// @deprecated since v4. Use new structure instead.
|
2023-12-22 11:17:54 +00:00
|
|
|
isAccordionTab(child) {
|
|
|
|
return child.type.name === 'AccordionTab';
|
|
|
|
},
|
2022-09-14 11:26:01 +00:00
|
|
|
getTabProp(tab, name) {
|
|
|
|
return tab.props ? tab.props[name] : undefined;
|
|
|
|
},
|
|
|
|
getKey(tab, index) {
|
|
|
|
return this.getTabProp(tab, 'header') || index;
|
|
|
|
},
|
2024-04-24 22:42:28 +00:00
|
|
|
getHeaderPT(tab, index) {
|
|
|
|
return {
|
|
|
|
root: mergeProps({ onClick: (event) => this.onTabClick(event, index) }, this.getTabProp(tab, 'headerProps'), this.getTabPT(tab, 'header', index)),
|
|
|
|
toggleicon: mergeProps(this.getTabProp(tab, 'headeractionprops'), this.getTabPT(tab, 'headeraction', index))
|
|
|
|
};
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
2024-04-24 22:42:28 +00:00
|
|
|
getContentPT(tab, index) {
|
|
|
|
return {
|
|
|
|
root: mergeProps(this.getTabProp(tab, 'contentProps'), this.getTabPT(tab, 'toggleablecontent', index)),
|
|
|
|
transition: this.getTabPT(tab, 'transition', index),
|
|
|
|
content: this.getTabPT(tab, 'content', index)
|
|
|
|
};
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
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,
|
2024-04-24 22:42:28 +00:00
|
|
|
active: this.isItemActive(`${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
|
|
|
},
|
2024-04-24 22:42:28 +00:00
|
|
|
onTabClick(event, index) {
|
2022-09-14 11:26:01 +00:00
|
|
|
this.$emit('tab-click', { originalEvent: event, index });
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2024-04-24 22:42:28 +00:00
|
|
|
// @deprecated since v4.
|
2022-09-06 12:03:37 +00:00
|
|
|
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;
|
|
|
|
}, []);
|
2024-04-24 22:42:28 +00:00
|
|
|
},
|
|
|
|
hasAccordionTab() {
|
|
|
|
return this.tabs.length;
|
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: {
|
2024-04-24 22:42:28 +00:00
|
|
|
AccordionPanel,
|
|
|
|
AccordionHeader,
|
|
|
|
AccordionContent,
|
|
|
|
ChevronUpIcon,
|
2023-04-03 08:27:59 +00:00
|
|
|
ChevronRightIcon
|
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>
|