Skip to content

Instantly share code, notes, and snippets.

@sadewole
Last active January 25, 2022 09:00
Show Gist options
  • Save sadewole/d975992ddfcc8e8cc9981b084da0493c to your computer and use it in GitHub Desktop.
Save sadewole/d975992ddfcc8e8cc9981b084da0493c to your computer and use it in GitHub Desktop.
AccordionItem.vue - Script: getExpandCondition
<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;
... // content hidden
const getExpandCondition = (
arrIndex: string | Array<string>,
itemIndex: string
) => {
if (Array.isArray(arrIndex)) {
return arrIndex.includes(itemIndex);
}
return arrIndex === itemIndex;
};
watch(
() => expandedIndex.value,
() => {
isExpanded.value = getExpandCondition(expandedIndex.value, id.value);
}
);
onMounted(watchExpand);
onUpdated(watchExpand);
return { isExpanded, toggleContent };
},
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment