Permalink
Browse files

feat(dialog options): allow esc to close dialog without lock: false

Adding ability to enable esc without `lock:false`.

Referenced: #238
  • Loading branch information...
devanp92 authored and PWKad committed Dec 13, 2016
1 parent 50de877 commit c0baaf52739286dfbdc79651a01ebf3233e866ea
Showing with 45 additions and 2 deletions.
  1. +2 −1 src/dialog-options.js
  2. +1 −1 src/dialog-renderer.js
  3. +42 −0 test/unit/dialog-renderer.spec.js
View
@@ -4,5 +4,6 @@ export let dialogOptions = {
startingZIndex: 1000,
ignoreTransitions: false,
rejectOnCancel: false,
- yieldController: false
+ yieldController: false,
+ enableEscClose: true
};
View
@@ -47,7 +47,7 @@ export class DialogRenderer {
_escapeKeyEventHandler = (e) => {
if (e.keyCode === 27) {
let top = this._dialogControllers[this._dialogControllers.length - 1];
- if (top && top.settings.lock !== true) {
+ if (top && (top.settings.lock !== true && !top.settings.enableEscClose)) {
top.cancel();
}
}
@@ -115,6 +115,48 @@ describe('the Dialog Renderer', () => {
});
});
+ it('does close the top dialog when enableEscClose is true', function (done) {
+ const settings = { enableEscClose: true, lock: false };
+ const expectedEndCount = 2;
+ const first = createDialogController(settings);
+ const last = createDialogController(settings);
+
+ spyOn(first, 'cancel');
+ spyOn(last, 'cancel');
+
+ this.showDialogs([first, last]).then(() => {
+ expect(this.catchWasCalled).toBe(false);
+ if (this.catchWasCalled) { return done(); }
+ expect(last.renderer._dialogControllers.length).toBe(expectedEndCount);
+ last.renderer._escapeKeyEventHandler({ keyCode: 27 });
+ expect(first.cancel).not.toHaveBeenCalled();
+ expect(last.cancel).not.toHaveBeenCalled();
+ expect(last.renderer._dialogControllers.length).toBe(expectedEndCount);
+ done();
+ });
+ });
+
+ it('does not close the top dialog when enableEscClose is false and lock is true', function (done) {
+ const settings = { enableEscClose: false, lock: true };
+ const expectedEndCount = 2;
+ const first = createDialogController(settings);
+ const last = createDialogController(settings);
+
+ spyOn(first, 'cancel');
+ spyOn(last, 'cancel');
+
+ this.showDialogs([first, last]).then(() => {
+ expect(this.catchWasCalled).toBe(false);
+ if (this.catchWasCalled) { return done(); }
+ expect(last.renderer._dialogControllers.length).toBe(expectedEndCount);
+ last.renderer._escapeKeyEventHandler({ keyCode: 27 });
+ expect(first.cancel).not.toHaveBeenCalled();
+ expect(last.cancel).not.toHaveBeenCalled();
+ expect(last.renderer._dialogControllers.length).toBe(expectedEndCount);
+ done();
+ });
+ });
+
it('does add the "ai-dialog-open" class on first open dialog', function (done) {
const body = DOM.querySelectorAll('body')[0];
spyOn(body.classList, 'add').and.callThrough();

0 comments on commit c0baaf5

Please sign in to comment.