feat(accordion): composant MalioAccordion + AccordionItem (mode multiple) [#MUI-37]
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,88 @@
|
|||||||
|
import {describe, expect, it} from 'vitest'
|
||||||
|
import {mount} from '@vue/test-utils'
|
||||||
|
import {nextTick} from 'vue'
|
||||||
|
import Accordion from './Accordion.vue'
|
||||||
|
import AccordionItem from './AccordionItem.vue'
|
||||||
|
|
||||||
|
const TWO_ITEMS = `
|
||||||
|
<MalioAccordionItem title="Prix" value="prix"><p>Contenu prix</p></MalioAccordionItem>
|
||||||
|
<MalioAccordionItem title="Catégorie" value="cat"><p>Contenu catégorie</p></MalioAccordionItem>
|
||||||
|
`
|
||||||
|
|
||||||
|
function mountAccordion(props: Record<string, unknown> = {}, slot: string = TWO_ITEMS, attachTo?: HTMLElement) {
|
||||||
|
return mount(Accordion, {
|
||||||
|
props,
|
||||||
|
slots: {default: slot},
|
||||||
|
attachTo,
|
||||||
|
global: {components: {MalioAccordionItem: AccordionItem}},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('MalioAccordion — rendu & mode multiple', () => {
|
||||||
|
it('renders each item header with its title', () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
expect(headers).toHaveLength(2)
|
||||||
|
expect(headers[0].text()).toContain('Prix')
|
||||||
|
expect(headers[1].text()).toContain('Catégorie')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders the slot content of each panel', () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
expect(wrapper.html()).toContain('Contenu prix')
|
||||||
|
expect(wrapper.html()).toContain('Contenu catégorie')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('all panels are collapsed by default', () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
expect(headers[0].attributes('aria-expanded')).toBe('false')
|
||||||
|
expect(headers[1].attributes('aria-expanded')).toBe('false')
|
||||||
|
const regions = wrapper.findAll('[role="region"]')
|
||||||
|
expect(regions[0].classes()).toContain('grid-rows-[0fr]')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('opens a panel on header click (multiple mode is default)', async () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
await headers[0].trigger('click')
|
||||||
|
expect(headers[0].attributes('aria-expanded')).toBe('true')
|
||||||
|
const regions = wrapper.findAll('[role="region"]')
|
||||||
|
expect(regions[0].classes()).toContain('grid-rows-[1fr]')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps multiple panels open simultaneously in multiple mode', async () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
await headers[0].trigger('click')
|
||||||
|
await headers[1].trigger('click')
|
||||||
|
expect(headers[0].attributes('aria-expanded')).toBe('true')
|
||||||
|
expect(headers[1].attributes('aria-expanded')).toBe('true')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('closes an open panel when its header is clicked again', async () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
await headers[0].trigger('click')
|
||||||
|
await headers[0].trigger('click')
|
||||||
|
expect(headers[0].attributes('aria-expanded')).toBe('false')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('wires aria-controls / aria-labelledby / role=region correctly', () => {
|
||||||
|
const wrapper = mountAccordion({id: 'acc'})
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
const regions = wrapper.findAll('[role="region"]')
|
||||||
|
expect(headers[0].attributes('id')).toBe('acc-header-prix')
|
||||||
|
expect(headers[0].attributes('aria-controls')).toBe('acc-panel-prix')
|
||||||
|
expect(regions[0].attributes('id')).toBe('acc-panel-prix')
|
||||||
|
expect(regions[0].attributes('aria-labelledby')).toBe('acc-header-prix')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('emits update:modelValue with an array in multiple mode', async () => {
|
||||||
|
const wrapper = mountAccordion()
|
||||||
|
const headers = wrapper.findAll('button[aria-expanded]')
|
||||||
|
await headers[0].trigger('click')
|
||||||
|
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual([['prix']])
|
||||||
|
await nextTick()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<template>
|
||||||
|
<div v-bind="$attrs" :class="rootClass">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import {computed, provide, ref, useId} from 'vue'
|
||||||
|
import {twMerge} from 'tailwind-merge'
|
||||||
|
import {accordionContextKey, type AccordionItemRegistration} from './context'
|
||||||
|
|
||||||
|
defineOptions({name: 'MalioAccordion', inheritAttrs: false})
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<{
|
||||||
|
mode?: 'single' | 'multiple'
|
||||||
|
modelValue?: string | string[]
|
||||||
|
id?: string
|
||||||
|
groupClass?: string
|
||||||
|
}>(), {
|
||||||
|
mode: 'multiple',
|
||||||
|
modelValue: undefined,
|
||||||
|
id: '',
|
||||||
|
groupClass: '',
|
||||||
|
})
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
(e: 'update:modelValue', value: string | string[]): void
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const generatedId = useId()
|
||||||
|
const baseId = computed(() => props.id || `malio-accordion-${generatedId}`)
|
||||||
|
const mode = computed(() => props.mode)
|
||||||
|
|
||||||
|
const isControlled = computed(() => props.modelValue !== undefined)
|
||||||
|
const localOpen = ref<string[]>([])
|
||||||
|
|
||||||
|
const items = ref<AccordionItemRegistration[]>([])
|
||||||
|
|
||||||
|
const openKeys = computed<string[]>(() => {
|
||||||
|
if (isControlled.value) {
|
||||||
|
const v = props.modelValue
|
||||||
|
if (props.mode === 'single') return v ? [v as string] : []
|
||||||
|
if (Array.isArray(v)) return v
|
||||||
|
return v ? [v as string] : []
|
||||||
|
}
|
||||||
|
return localOpen.value
|
||||||
|
})
|
||||||
|
|
||||||
|
function isOpen(value: string) {
|
||||||
|
return openKeys.value.includes(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggle(value: string) {
|
||||||
|
const current = openKeys.value
|
||||||
|
let next: string[]
|
||||||
|
if (props.mode === 'single') {
|
||||||
|
next = current.includes(value) ? [] : [value]
|
||||||
|
} else {
|
||||||
|
next = current.includes(value)
|
||||||
|
? current.filter(v => v !== value)
|
||||||
|
: [...current, value]
|
||||||
|
}
|
||||||
|
if (!isControlled.value) {
|
||||||
|
localOpen.value = next
|
||||||
|
}
|
||||||
|
emit('update:modelValue', props.mode === 'single' ? (next[0] ?? '') : next)
|
||||||
|
}
|
||||||
|
|
||||||
|
function register(item: AccordionItemRegistration, defaultOpen: boolean) {
|
||||||
|
items.value.push(item)
|
||||||
|
if (defaultOpen && !isControlled.value) {
|
||||||
|
if (props.mode === 'single') {
|
||||||
|
if (localOpen.value.length === 0) localOpen.value = [item.value]
|
||||||
|
} else if (!localOpen.value.includes(item.value)) {
|
||||||
|
localOpen.value.push(item.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unregister(value: string) {
|
||||||
|
items.value = items.value.filter(i => i.value !== value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function focusSibling(value: string, offset: 1 | -1) {
|
||||||
|
const enabled = items.value.filter(i => !i.isDisabled())
|
||||||
|
const idx = enabled.findIndex(i => i.value === value)
|
||||||
|
if (idx === -1) return
|
||||||
|
const next = enabled[(idx + offset + enabled.length) % enabled.length]
|
||||||
|
next?.getHeaderEl()?.focus()
|
||||||
|
}
|
||||||
|
|
||||||
|
const rootClass = computed(() =>
|
||||||
|
twMerge('divide-y divide-m-border overflow-hidden rounded-malio border border-m-border', props.groupClass),
|
||||||
|
)
|
||||||
|
|
||||||
|
provide(accordionContextKey, {
|
||||||
|
mode,
|
||||||
|
baseId,
|
||||||
|
isOpen,
|
||||||
|
toggle,
|
||||||
|
register,
|
||||||
|
unregister,
|
||||||
|
focusSibling,
|
||||||
|
})
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<h3 class="m-0">
|
||||||
|
<button
|
||||||
|
:id="headerId"
|
||||||
|
ref="headerRef"
|
||||||
|
type="button"
|
||||||
|
:class="headerClasses"
|
||||||
|
:aria-expanded="open"
|
||||||
|
:aria-controls="panelId"
|
||||||
|
:disabled="disabled"
|
||||||
|
:aria-disabled="disabled || undefined"
|
||||||
|
@click="onToggle"
|
||||||
|
@keydown.down.prevent="ctx.focusSibling(value, 1)"
|
||||||
|
@keydown.up.prevent="ctx.focusSibling(value, -1)"
|
||||||
|
>
|
||||||
|
<span>{{ title }}</span>
|
||||||
|
<IconifyIcon
|
||||||
|
icon="mdi:chevron-down"
|
||||||
|
:width="24"
|
||||||
|
class="shrink-0 transition-transform duration-200"
|
||||||
|
:class="open ? 'rotate-180' : ''"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</h3>
|
||||||
|
<div
|
||||||
|
:id="panelId"
|
||||||
|
role="region"
|
||||||
|
:aria-labelledby="headerId"
|
||||||
|
class="grid transition-[grid-template-rows] duration-200 ease-out"
|
||||||
|
:class="open ? 'grid-rows-[1fr]' : 'grid-rows-[0fr]'"
|
||||||
|
>
|
||||||
|
<div class="overflow-hidden" :inert="!open || undefined">
|
||||||
|
<div :class="panelInnerClass">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import {computed, inject, onBeforeUnmount, onMounted, ref, useId} from 'vue'
|
||||||
|
import {Icon as IconifyIcon} from '@iconify/vue'
|
||||||
|
import {twMerge} from 'tailwind-merge'
|
||||||
|
import {accordionContextKey} from './context'
|
||||||
|
|
||||||
|
defineOptions({name: 'MalioAccordionItem', inheritAttrs: false})
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<{
|
||||||
|
title: string
|
||||||
|
value?: string
|
||||||
|
defaultOpen?: boolean
|
||||||
|
disabled?: boolean
|
||||||
|
headerClass?: string
|
||||||
|
panelClass?: string
|
||||||
|
}>(), {
|
||||||
|
value: '',
|
||||||
|
defaultOpen: false,
|
||||||
|
disabled: false,
|
||||||
|
headerClass: '',
|
||||||
|
panelClass: '',
|
||||||
|
})
|
||||||
|
|
||||||
|
const ctx = inject(accordionContextKey)
|
||||||
|
if (!ctx) {
|
||||||
|
throw new Error('MalioAccordionItem doit être utilisé à l\'intérieur de MalioAccordion')
|
||||||
|
}
|
||||||
|
|
||||||
|
const generatedId = useId()
|
||||||
|
const value = computed(() => props.value || `malio-accordion-item-${generatedId}`)
|
||||||
|
const headerRef = ref<HTMLButtonElement | null>(null)
|
||||||
|
const headerId = computed(() => `${ctx.baseId.value}-header-${value.value}`)
|
||||||
|
const panelId = computed(() => `${ctx.baseId.value}-panel-${value.value}`)
|
||||||
|
const open = computed(() => ctx.isOpen(value.value))
|
||||||
|
|
||||||
|
function onToggle() {
|
||||||
|
if (props.disabled) return
|
||||||
|
ctx.toggle(value.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
const headerClasses = computed(() =>
|
||||||
|
twMerge(
|
||||||
|
'flex w-full items-center justify-between gap-4 px-4 py-3 text-left font-medium text-m-text transition-colors',
|
||||||
|
props.disabled ? 'cursor-not-allowed text-m-muted' : 'cursor-pointer hover:bg-m-surface',
|
||||||
|
props.headerClass,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
const panelInnerClass = computed(() => twMerge('px-4 py-3 text-m-text', props.panelClass))
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
ctx.register(
|
||||||
|
{
|
||||||
|
value: value.value,
|
||||||
|
getHeaderEl: () => headerRef.value,
|
||||||
|
isDisabled: () => props.disabled,
|
||||||
|
},
|
||||||
|
props.defaultOpen,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => ctx.unregister(value.value))
|
||||||
|
</script>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import type {ComputedRef, InjectionKey} from 'vue'
|
||||||
|
|
||||||
|
export interface AccordionItemRegistration {
|
||||||
|
value: string
|
||||||
|
getHeaderEl: () => HTMLElement | null
|
||||||
|
isDisabled: () => boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AccordionContext {
|
||||||
|
mode: ComputedRef<'single' | 'multiple'>
|
||||||
|
baseId: ComputedRef<string>
|
||||||
|
isOpen: (value: string) => boolean
|
||||||
|
toggle: (value: string) => void
|
||||||
|
register: (item: AccordionItemRegistration, defaultOpen: boolean) => void
|
||||||
|
unregister: (value: string) => void
|
||||||
|
focusSibling: (value: string, offset: 1 | -1) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export const accordionContextKey: InjectionKey<AccordionContext> = Symbol('MalioAccordion')
|
||||||
Reference in New Issue
Block a user