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

171 lines
6.8 KiB
Vue
Raw Normal View History

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';
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';
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 {
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();
},
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.
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) {
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: {
instance: this,
props: this.$props,
state: this.$data
2023-05-23 21:10:36 +00:00
},
context: {
index,
count,
first: index === 0,
last: index === count - 1,
2024-04-24 22:42:28 +00:00
active: this.isItemActive(`${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));
},
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() {
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>