Last active
January 25, 2022 09:01
-
-
Save sadewole/b160e9cac0c91c0f7258b5036c1c3882 to your computer and use it in GitHub Desktop.
AccordionItem.vue - script: watchExpand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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