Skip to content

Commit d49d8ce

Browse files
Ensure Tab keyboard functionality after #37146 (#37200)
* fix: keyboard functionality * test: add tests * Add some focus spies in 2 other unit tests Co-authored-by: Julien Déramond <juderamond@gmail.com>
1 parent 0a5f6e0 commit d49d8ce

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

js/src/tab.js

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Tab extends BaseComponent {
161161
const nextActiveElement = getNextActiveElement(this._getChildren().filter(element => !isDisabled(element)), event.target, isNext, true)
162162

163163
if (nextActiveElement) {
164+
nextActiveElement.focus({ preventScroll: true })
164165
Tab.getOrCreateInstance(nextActiveElement).show()
165166
}
166167
}

js/tests/unit/tab.spec.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ describe('Tab', () => {
526526
const spyShow1 = spyOn(tab1, 'show').and.callThrough()
527527
const spyShow2 = spyOn(tab2, 'show').and.callThrough()
528528
const spyShow3 = spyOn(tab3, 'show').and.callThrough()
529+
const spyFocus1 = spyOn(tabEl1, 'focus').and.callThrough()
530+
const spyFocus2 = spyOn(tabEl2, 'focus').and.callThrough()
531+
const spyFocus3 = spyOn(tabEl3, 'focus').and.callThrough()
529532

530533
const spyStop = spyOn(Event.prototype, 'stopPropagation').and.callThrough()
531534
const spyPrevent = spyOn(Event.prototype, 'preventDefault').and.callThrough()
@@ -535,15 +538,18 @@ describe('Tab', () => {
535538

536539
tabEl1.dispatchEvent(keydown)
537540
expect(spyShow2).toHaveBeenCalled()
541+
expect(spyFocus2).toHaveBeenCalled()
538542

539543
keydown = createEvent('keydown')
540544
keydown.key = 'ArrowDown'
541545

542546
tabEl2.dispatchEvent(keydown)
543547
expect(spyShow3).toHaveBeenCalled()
548+
expect(spyFocus3).toHaveBeenCalled()
544549

545550
tabEl3.dispatchEvent(keydown)
546551
expect(spyShow1).toHaveBeenCalled()
552+
expect(spyFocus1).toHaveBeenCalled()
547553

548554
expect(spyStop).toHaveBeenCalledTimes(3)
549555
expect(spyPrevent).toHaveBeenCalledTimes(3)
@@ -557,12 +563,14 @@ describe('Tab', () => {
557563
'</div>'
558564
].join('')
559565

560-
const tabEl = fixtureEl.querySelector('#tab1')
566+
const tabEl1 = fixtureEl.querySelector('#tab1')
561567
const tabEl2 = fixtureEl.querySelector('#tab2')
562-
const tab = new Tab(tabEl)
568+
const tab1 = new Tab(tabEl1)
563569
const tab2 = new Tab(tabEl2)
564-
const spyShow1 = spyOn(tab, 'show').and.callThrough()
570+
const spyShow1 = spyOn(tab1, 'show').and.callThrough()
565571
const spyShow2 = spyOn(tab2, 'show').and.callThrough()
572+
const spyFocus1 = spyOn(tabEl1, 'focus').and.callThrough()
573+
const spyFocus2 = spyOn(tabEl2, 'focus').and.callThrough()
566574

567575
const spyStop = spyOn(Event.prototype, 'stopPropagation').and.callThrough()
568576
const spyPrevent = spyOn(Event.prototype, 'preventDefault').and.callThrough()
@@ -572,12 +580,14 @@ describe('Tab', () => {
572580

573581
tabEl2.dispatchEvent(keydown)
574582
expect(spyShow1).toHaveBeenCalled()
583+
expect(spyFocus1).toHaveBeenCalled()
575584

576585
keydown = createEvent('keydown')
577586
keydown.key = 'ArrowUp'
578587

579-
tabEl.dispatchEvent(keydown)
588+
tabEl1.dispatchEvent(keydown)
580589
expect(spyShow2).toHaveBeenCalled()
590+
expect(spyFocus2).toHaveBeenCalled()
581591

582592
expect(spyStop).toHaveBeenCalledTimes(2)
583593
expect(spyPrevent).toHaveBeenCalledTimes(2)
@@ -605,6 +615,10 @@ describe('Tab', () => {
605615
const spy2 = spyOn(tab2, 'show').and.callThrough()
606616
const spy3 = spyOn(tab3, 'show').and.callThrough()
607617
const spy4 = spyOn(tab4, 'show').and.callThrough()
618+
const spyFocus1 = spyOn(tabEl, 'focus').and.callThrough()
619+
const spyFocus2 = spyOn(tabEl2, 'focus').and.callThrough()
620+
const spyFocus3 = spyOn(tabEl3, 'focus').and.callThrough()
621+
const spyFocus4 = spyOn(tabEl4, 'focus').and.callThrough()
608622

609623
const keydown = createEvent('keydown')
610624
keydown.key = 'ArrowRight'
@@ -614,6 +628,10 @@ describe('Tab', () => {
614628
expect(spy2).not.toHaveBeenCalled()
615629
expect(spy3).not.toHaveBeenCalled()
616630
expect(spy4).toHaveBeenCalledTimes(1)
631+
expect(spyFocus1).not.toHaveBeenCalled()
632+
expect(spyFocus2).not.toHaveBeenCalled()
633+
expect(spyFocus3).not.toHaveBeenCalled()
634+
expect(spyFocus4).toHaveBeenCalledTimes(1)
617635
})
618636

619637
it('if keydown event is left arrow and next element is disabled', () => {
@@ -638,6 +656,10 @@ describe('Tab', () => {
638656
const spy2 = spyOn(tab2, 'show').and.callThrough()
639657
const spy3 = spyOn(tab3, 'show').and.callThrough()
640658
const spy4 = spyOn(tab4, 'show').and.callThrough()
659+
const spyFocus1 = spyOn(tabEl, 'focus').and.callThrough()
660+
const spyFocus2 = spyOn(tabEl2, 'focus').and.callThrough()
661+
const spyFocus3 = spyOn(tabEl3, 'focus').and.callThrough()
662+
const spyFocus4 = spyOn(tabEl4, 'focus').and.callThrough()
641663

642664
const keydown = createEvent('keydown')
643665
keydown.key = 'ArrowLeft'
@@ -647,6 +669,10 @@ describe('Tab', () => {
647669
expect(spy3).not.toHaveBeenCalled()
648670
expect(spy2).not.toHaveBeenCalled()
649671
expect(spy1).toHaveBeenCalledTimes(1)
672+
expect(spyFocus4).not.toHaveBeenCalled()
673+
expect(spyFocus3).not.toHaveBeenCalled()
674+
expect(spyFocus2).not.toHaveBeenCalled()
675+
expect(spyFocus1).toHaveBeenCalledTimes(1)
650676
})
651677
})
652678

0 commit comments

Comments
 (0)