Skip to content

Commit

Permalink
feat(Accordion): emit open event with index (#1559)
Browse files Browse the repository at this point in the history
  • Loading branch information
noook committed Mar 25, 2024
1 parent c3ac4ba commit 224ec3c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/runtime/components/elements/Accordion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
</template>

<script lang="ts">
import { ref, computed, toRef, defineComponent } from 'vue'
import { ref, computed, toRef, defineComponent, watch } from 'vue'
import type { PropType } from 'vue'
import { Disclosure as HDisclosure, DisclosureButton as HDisclosureButton, DisclosurePanel as HDisclosurePanel, provideUseId } from '@headlessui/vue'
import UIcon from '../elements/Icon.vue'
Expand Down Expand Up @@ -108,12 +108,25 @@ export default defineComponent({
default: () => ({})
}
},
setup (props) {
emits: ['open'],
setup (props, { emit }) {
const { ui, attrs } = useUI('accordion', toRef(props, 'ui'), config, toRef(props, 'class'))
const uiButton = computed<typeof configButton>(() => configButton)
const buttonRefs = ref<{ open: boolean, close: (e: EventTarget) => {} }[]>([])
const openedStates = computed(() => buttonRefs.value.map(({ open }) => open))
watch(openedStates, (newValue, oldValue) => {
for (const index in newValue) {
const isOpenBefore = oldValue[index]
const isOpenAfter = newValue[index]
if (!isOpenBefore && isOpenAfter) {
emit('open', index)
}
}
}, { immediate: true })
function closeOthers (currentIndex: number, e: Event) {
if (!props.items[currentIndex].closeOthers && props.multiple) {
Expand Down

0 comments on commit 224ec3c

Please sign in to comment.