Skip to content

Commit

Permalink
fix(Accordion): toggle correct element when keyboard press (#805)
Browse files Browse the repository at this point in the history
Co-authored-by: Haytham Salama <haythamasalama@gmail.com>
  • Loading branch information
Sma11X and Haythamasalama committed Oct 12, 2023
1 parent 94cabca commit 96296c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 40 deletions.
34 changes: 16 additions & 18 deletions src/runtime/components/elements/Accordion.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<template>
<div :class="ui.wrapper">
<HDisclosure v-for="(item, index) in items" v-slot="{ open, close }" :key="index" :default-open="defaultOpen || item.defaultOpen">
<HDisclosureButton :ref="() => buttonRefs[index] = close" as="template" :disabled="item.disabled">
<HDisclosureButton
:ref="() => buttonRefs[index] = { open, close }"
as="template"
:disabled="item.disabled"
@click="closeOthers(index, $event)"
@keydown.enter="closeOthers(index, $event)"
@keydown.space="closeOthers(index, $event)"
>
<slot :item="item" :index="index" :open="open" :close="close">
<UButton v-bind="{ ...omit(ui.default, ['openIcon', 'closeIcon']), ...attrs, ...omit(item, ['slot', 'disabled', 'content', 'defaultOpen']) }">
<template #trailing>
Expand All @@ -18,8 +25,6 @@
</slot>
</HDisclosureButton>

<StateEmitter :open="open" @open="closeOthers(index)" />

<Transition
v-bind="ui.transition"
@enter="onEnter"
Expand Down Expand Up @@ -47,7 +52,6 @@ import UIcon from '../elements/Icon.vue'
import UButton from '../elements/Button.vue'
import { useUI } from '../../composables/useUI'
import { mergeConfig, omit } from '../../utils'
import StateEmitter from '../../utils/StateEmitter'
import type { AccordionItem, Strategy } from '../../types'
// @ts-expect-error
import appConfig from '#build/app.config'
Expand All @@ -63,8 +67,7 @@ export default defineComponent({
HDisclosureButton,
HDisclosurePanel,
UIcon,
UButton,
StateEmitter
UButton
},
inheritAttrs: false,
props: {
Expand Down Expand Up @@ -102,23 +105,18 @@ export default defineComponent({
const uiButton = computed<Partial<typeof configButton>>(() => configButton)
const buttonRefs = ref<Function[]>([])
const buttonRefs = ref<{ open: boolean, close: (e: EventTarget) => {} }[]>([])
function closeOthers (currentIndex: number) {
function closeOthers (currentIndex: number, e: Event) {
if (!props.items[currentIndex].closeOthers && props.multiple) {
return
}
const totalItems = buttonRefs.value.length
const order = Array.from({ length: totalItems }, (_, i) => (currentIndex + i) % totalItems)
.filter(index => index !== currentIndex)
.reverse()
for (const index of order) {
const close = buttonRefs.value[index]
close()
}
buttonRefs.value.forEach((button) => {
if (button.open) {
button.close(e.target)
}
})
}
function onEnter (el: HTMLElement, done) {
Expand Down
22 changes: 0 additions & 22 deletions src/runtime/utils/StateEmitter.ts

This file was deleted.

1 comment on commit 96296c3

@vercel
Copy link

@vercel vercel bot commented on 96296c3 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-git-dev-nuxt-js.vercel.app
ui-nuxt-js.vercel.app
ui.nuxt.com

Please sign in to comment.