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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IgxFocusTrapDirective } from './focus-trap.directive';

import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { UIInteractions } from '../../test-utils/ui-interactions.spec';
import { IgxTimePickerComponent } from '../../time-picker/time-picker.component';

describe('igxFocusTrap', () => {
beforeEach(waitForAsync(() => {
Expand Down Expand Up @@ -161,6 +162,39 @@ describe('igxFocusTrap', () => {
fix.detectChanges();
expect(document.activeElement).toEqual(button.nativeElement);
}));

it('should focus only visible focusable elements on Tab key pressed', () => {
const fix = TestBed.createComponent(TrapFocusTestComponent);
fix.detectChanges();

fix.componentInstance.showTimePicker = true;
fix.detectChanges();

const focusTrap = fix.debugElement.query(By.directive(IgxFocusTrapDirective));
const buttons = fix.debugElement.queryAll(By.css('button'));
const inputs = fix.debugElement.queryAll(By.css('input'));
const timePickerInput = fix.debugElement.query(By.css('.igx-input-group__input'));

UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap);
fix.detectChanges();
expect(document.activeElement).toEqual(inputs[0].nativeElement);

UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap);
fix.detectChanges();
expect(document.activeElement).toEqual(inputs[1].nativeElement);

UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap);
fix.detectChanges();
expect(document.activeElement).toEqual(timePickerInput.nativeElement);

UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap);
fix.detectChanges();
expect(document.activeElement).toEqual(buttons[buttons.length - 1].nativeElement);

UIInteractions.triggerEventHandlerKeyDown('Tab', focusTrap);
fix.detectChanges();
expect(document.activeElement).toEqual(inputs[0].nativeElement);
});
});


Expand All @@ -177,14 +211,19 @@ describe('igxFocusTrap', () => {
<input type="password" placeholder="Enter Password" name="psw">
}
<br>
@if (showTimePicker) {
<igx-time-picker> </igx-time-picker>
}
<br>
@if (showButton) {
<button>SIGN IN</button>
}
</div>`,
imports: [IgxFocusTrapDirective]
imports: [IgxFocusTrapDirective, IgxTimePickerComponent]
})
class TrapFocusTestComponent {
public showInput = true;
public showButton = true;
public focusTrap = true;
public showTimePicker = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class IgxFocusTrapDirective implements AfterViewInit, OnDestroy {
private getFocusableElements(element: Element) {
return Array.from(element.querySelectorAll(
'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
)).filter(el => !el.hasAttribute('disabled') && !el.getAttribute('aria-hidden'));
)).filter(el => !el.hasAttribute('disabled') && !el.closest('[aria-hidden="true"]'));
}

private getFocusedElement(): HTMLElement | null {
Expand Down
Loading