Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Accordion): toggle correct element when keyboard press #805

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 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,19 @@ 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) {
// pass the HTMLElement to close method to save the current click button for enter/space press, see #626
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.