Since the latest ngx-bootstrap(v2.4) does not have the fix, I created a directive to trap tab key focus in the modal box itself.
Angular Version I used is, Angular 5
My directive is below.
Directive
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appSdbModalFocus]'
})
export class SdbModalFocusDirective {
KEYCODE_TAB: number = 9;
constructor(
private hostElement: ElementRef
) {}
ngOnInit() {}
@HostListener("keydown", ["$event"])
onKeyDown(e: KeyboardEvent): any {
if (e.key === 'Tab' || e.keyCode === this.KEYCODE_TAB) {
let focusableEls = this.hostElement.nativeElement;
let modalContent = $(focusableEls).find('a, :input, [tabindex]');
var firstFocusableEl = modalContent.first()[0];
var lastFocusableEl = modalContent.last()[0];
if (e.shiftKey) /* shift + tab */ {
this.log(firstFocusableEl, lastFocusableEl, document);
if (document.activeElement === firstFocusableEl) {
lastFocusableEl.focus();
e.preventDefault();
}
} else /* tab */ {
this.log(firstFocusableEl, lastFocusableEl, document);
if (document.activeElement === lastFocusableEl) {
firstFocusableEl.focus();
e.preventDefault();
}
}
}
}
}
HTML
<form appSdbModalFocus>
...
</form>
Created this directive with the help of trap-focus-in-an-element