Skip to content

Instantly share code, notes, and snippets.

@sadewole
Last active January 25, 2022 09:01
Show Gist options
  • Save sadewole/b160e9cac0c91c0f7258b5036c1c3882 to your computer and use it in GitHub Desktop.
Save sadewole/b160e9cac0c91c0f7258b5036c1c3882 to your computer and use it in GitHub Desktop.
AccordionItem.vue - script: watchExpand
<script lang="ts">
... // content hidden
export default defineComponent({
name: "AccordionItem",
... // content hidden
setup(props: Props) {
const id = ref(useId());
const isExpanded = ref(props.defaultIsOpen || false);
const allowMultiple = inject("allowMultiple") as boolean;
const indices = inject("indices") as Indices;
const expandedIndex = inject("expandedIndex") as ExpandedIndex;
const watchExpand = () => {
if (allowMultiple) {
let newIndices;
if (isExpanded.value) {
newIndices = [...indices.value, id.value];
} else {
newIndices = indices.value.filter(
(itemIndex) => itemIndex !== id.value
);
}
expandedIndex.value = newIndices;
} else if (isExpanded.value) {
expandedIndex.value = id.value;
}
};
const toggleContent = () => {
isExpanded.value = !isExpanded.value;
};
onMounted(watchExpand);
onUpdated(watchExpand);
},
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment