Skip to content

Commit 23ccdc8

Browse files
authored
Merge pull request #296 from mrholek/main
Minor fixes and improvements in the `CAccordion` and `CNavGroup` component
2 parents 342ba88 + 7a65f36 commit 23ccdc8

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

packages/coreui-vue/src/components/accordion/CAccordionBody.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { CCollapse } from '../collapse/CCollapse'
44
const CAccordionBody = defineComponent({
55
name: 'CAccordionBody',
66
setup(_, { slots }) {
7+
const id = inject('id')
78
const visible = inject('visible') as Ref<boolean>
89
return () =>
910
h(
1011
CCollapse,
11-
{ class: 'accordion-collapse', visible: visible.value },
12+
{ class: 'accordion-collapse', id, visible: visible.value },
1213
{
1314
default: () => h('div', { class: ['accordion-body'] }, slots.default && slots.default()),
1415
},

packages/coreui-vue/src/components/accordion/CAccordionButton.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineComponent, h, inject, Ref } from 'vue'
33
const CAccordionButton = defineComponent({
44
name: 'CAccordionButton',
55
setup(_, { slots }) {
6+
const id = inject('id') as string
67
const toggleVisibility = inject('toggleVisibility') as () => void
78
const visible = inject('visible') as Ref<boolean>
89

@@ -11,7 +12,8 @@ const CAccordionButton = defineComponent({
1112
'button',
1213
{
1314
type: 'button',
14-
'aria-expanded': !visible.value,
15+
'aria-control': id,
16+
'aria-expanded': visible.value,
1517
class: ['accordion-button', { ['collapsed']: !visible.value }],
1618
onClick: () => toggleVisibility(),
1719
},

packages/coreui-vue/src/components/accordion/CAccordionItem.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { defineComponent, h, inject, provide, ref, watch, Ref } from 'vue'
1+
import { defineComponent, h, inject, provide, ref, watch, Ref, useId } from 'vue'
22

33
const CAccordionItem = defineComponent({
44
name: 'CAccordionItem',
55
props: {
6+
/**
7+
* The id global attribute defines an identifier (ID) that must be unique in the whole document.
8+
*/
9+
id: String,
610
/**
711
* The item key.
812
*/
@@ -13,16 +17,20 @@ const CAccordionItem = defineComponent({
1317
const alwaysOpen = inject('alwaysOpen') as boolean
1418
const setActiveItemKey = inject('setActiveItemKey') as (key: number | string) => void
1519

16-
const itemKey = ref(props.itemKey ?? Math.random().toString(36).slice(2, 11))
20+
const id = props.id ?? useId()
21+
const itemKey = ref(props.itemKey ?? id)
1722
const visible = ref(Boolean(activeItemKey.value === itemKey.value))
1823

1924
watch(activeItemKey, () => (visible.value = Boolean(activeItemKey.value === itemKey.value)))
2025

2126
const toggleVisibility = () => {
2227
visible.value = !visible.value
23-
!alwaysOpen && visible && setActiveItemKey(itemKey.value)
28+
if (!alwaysOpen && visible) {
29+
setActiveItemKey(itemKey.value)
30+
}
2431
}
2532

33+
provide('id', id)
2634
provide('visible', visible)
2735
provide('toggleVisibility', toggleVisibility)
2836

packages/coreui-vue/src/components/nav/CNavGroup.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const CNavGroup = defineComponent({
4141

4242
onMounted(() => {
4343
visible.value = props.visible
44-
props.visible && navGroupRef.value.classList.add('show')
44+
if (props.visible) {
45+
navGroupRef.value.classList.add('show')
46+
}
47+
4548
emit('visible-change', visible.value)
4649
})
4750

@@ -60,7 +63,8 @@ const CNavGroup = defineComponent({
6063
emit('visible-change', visible.value)
6164
})
6265

63-
const handleTogglerClick = () => {
66+
const handleTogglerClick = (event: Event) => {
67+
event.preventDefault()
6468
visible.value = !visible.value
6569
emit('visible-change', visible.value)
6670
}
@@ -111,6 +115,7 @@ const CNavGroup = defineComponent({
111115
'a',
112116
{
113117
class: ['nav-link', 'nav-group-toggle'],
118+
href: '#',
114119
onClick: handleTogglerClick,
115120
},
116121
slots.togglerContent && slots.togglerContent(),

0 commit comments

Comments
 (0)