Skip to content

Minor fixes and improvements in the CAccordion and CNavGroup component #296

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

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { CCollapse } from '../collapse/CCollapse'
const CAccordionBody = defineComponent({
name: 'CAccordionBody',
setup(_, { slots }) {
const id = inject('id')
const visible = inject('visible') as Ref<boolean>
return () =>
h(
CCollapse,
{ class: 'accordion-collapse', visible: visible.value },
{ class: 'accordion-collapse', id, visible: visible.value },
{
default: () => h('div', { class: ['accordion-body'] }, slots.default && slots.default()),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineComponent, h, inject, Ref } from 'vue'
const CAccordionButton = defineComponent({
name: 'CAccordionButton',
setup(_, { slots }) {
const id = inject('id') as string
const toggleVisibility = inject('toggleVisibility') as () => void
const visible = inject('visible') as Ref<boolean>

Expand All @@ -11,7 +12,8 @@ const CAccordionButton = defineComponent({
'button',
{
type: 'button',
'aria-expanded': !visible.value,
'aria-control': id,
'aria-expanded': visible.value,
class: ['accordion-button', { ['collapsed']: !visible.value }],
onClick: () => toggleVisibility(),
},
Expand Down
14 changes: 11 additions & 3 deletions packages/coreui-vue/src/components/accordion/CAccordionItem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { defineComponent, h, inject, provide, ref, watch, Ref } from 'vue'
import { defineComponent, h, inject, provide, ref, watch, Ref, useId } from 'vue'

const CAccordionItem = defineComponent({
name: 'CAccordionItem',
props: {
/**
* The id global attribute defines an identifier (ID) that must be unique in the whole document.
*/
id: String,
/**
* The item key.
*/
Expand All @@ -13,16 +17,20 @@ const CAccordionItem = defineComponent({
const alwaysOpen = inject('alwaysOpen') as boolean
const setActiveItemKey = inject('setActiveItemKey') as (key: number | string) => void

const itemKey = ref(props.itemKey ?? Math.random().toString(36).slice(2, 11))
const id = props.id ?? useId()
const itemKey = ref(props.itemKey ?? id)
const visible = ref(Boolean(activeItemKey.value === itemKey.value))

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

const toggleVisibility = () => {
visible.value = !visible.value
!alwaysOpen && visible && setActiveItemKey(itemKey.value)
if (!alwaysOpen && visible) {
setActiveItemKey(itemKey.value)
}
}

provide('id', id)
provide('visible', visible)
provide('toggleVisibility', toggleVisibility)

Expand Down
9 changes: 7 additions & 2 deletions packages/coreui-vue/src/components/nav/CNavGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const CNavGroup = defineComponent({

onMounted(() => {
visible.value = props.visible
props.visible && navGroupRef.value.classList.add('show')
if (props.visible) {
navGroupRef.value.classList.add('show')
}

emit('visible-change', visible.value)
})

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

const handleTogglerClick = () => {
const handleTogglerClick = (event: Event) => {
event.preventDefault()
visible.value = !visible.value
emit('visible-change', visible.value)
}
Expand Down Expand Up @@ -111,6 +115,7 @@ const CNavGroup = defineComponent({
'a',
{
class: ['nav-link', 'nav-group-toggle'],
href: '#',
onClick: handleTogglerClick,
},
slots.togglerContent && slots.togglerContent(),
Expand Down