Skip to content
Open
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
6 changes: 4 additions & 2 deletions js/src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ class Carousel extends BaseComponent {

const activeIndicator = SelectorEngine.findOne(SELECTOR_ACTIVE, this._indicatorsElement)

activeIndicator.classList.remove(CLASS_NAME_ACTIVE)
activeIndicator.removeAttribute('aria-current')
if (activeIndicator) {
activeIndicator.classList.remove(CLASS_NAME_ACTIVE)
activeIndicator.removeAttribute('aria-current')
}

const newActiveIndicator = SelectorEngine.findOne(`[data-bs-slide-to="${index}"]`, this._indicatorsElement)

Expand Down
29 changes: 29 additions & 0 deletions js/tests/unit/carousel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,35 @@ describe('Carousel', () => {
})
})

it('should handle indicators without an active indicator on initialization', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="myCarousel" class="carousel slide">',
' <div class="carousel-indicators">',
' <button type="button" data-bs-target="myCarousel" data-bs-slide-to="0" aria-label="Slide 1"></button>',
' <button type="button" id="secondIndicator" data-bs-target="myCarousel" data-bs-slide-to="1" aria-label="Slide 2"></button>',
' </div>',
' <div class="carousel-inner">',
' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 2</div>',
' </div>',
'</div>'
].join('')

const carouselEl = fixtureEl.querySelector('#myCarousel')
const secondIndicator = fixtureEl.querySelector('#secondIndicator')
const carousel = new Carousel(carouselEl)

carouselEl.addEventListener('slid.bs.carousel', () => {
expect(secondIndicator).toHaveClass('active')
expect(secondIndicator.getAttribute('aria-current')).toEqual('true')
resolve()
})

carousel.next()
})
})

it('should call next()/prev() instance methods when clicking the respective direction buttons', () => {
fixtureEl.innerHTML = [
'<div id="carousel" class="carousel slide">',
Expand Down