Ngx-Modal让你在自己的标签之外 - Angular 2

时间:2017-06-28 12:10:16

标签: angular focus tabbing ngx-modal

Plunker Example

  

我在Ngx-Modal代码中放置了一个包装器,因此我可以将一些代码应用于所有ngx-modals。我也碰巧注意到,当键盘标签允许你在Modal之外时,是否有人知道如何解决这个问题?我在那里放了一个keydown事件来试图限制状态......

//our root app component
import {Component, ContentChild, NgModule, HostListener, ElementRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {ModalModule} from "ngx-modal";
import { Modal } from "ngx-modal";

@Component({
  selector: 'ext-ngx-modal',
  template: `<ng-content></ng-content>`,
})
export class NgxModalComponent {
  @ContentChild(Modal) modal: Modal;

  openExt():void {
    this.modal.open();
  }

  @HostListener('document:keydown', ['$event'])
  onkeydown(ev: KeyboardEvent) {
     this.restrictModalFocus(ev);
  }

  restrictModalFocus(event: any): void {
    if (!this.modal.isOpened) {                        
        console.log("Modal is NOT open: " + this.modal);
        return
    }

    console.log("Modal is open");

    if (event.keyCode == '9' || event.which == '9') {
        let focusStillInModal = this.modal.modalRoot.nativeElement.contains(event.target);
        console.log("focusStillInModal: " + focusStillInModal);
    }
  }  
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}} I am </h2>
      <div class="row container-fluid">
        <button (click)="myExtNgxModal.openExt()"> open my modal</button>
        <ext-ngx-modal #myExtNgxModal>
          <modal #myModal>
              <modal-header>
                  <h1>Tab issue</h1>
              </modal-header>
              <modal-content>
                  tabbing...lets you out of modal!<br/><br/>
                  Content could be dynamic unknown text...<br/>
                  <input type="Text" value="Some field"><br/>
                  <input type="Text" value="Some field"><br/>
                  <input type="Text" value="Some field"><br/>
              </modal-content>
          </modal>
        </ext-ngx-modal>
      </div>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = 'Angular2'
  }
}

@NgModule({
  imports: [ BrowserModule, ModalModule ],
  declarations: [ App, NgxModalComponent ],
  exports: [ NgxModalComponent ],
  bootstrap: [ App ]
})
export class AppModule {}

0 个答案:

没有答案