Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#10099 adds keyboard handling to close confirm_modal on ESC #10455

Merged
Merged
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
42 changes: 36 additions & 6 deletions src/ui/public/modals/__tests__/confirm_modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ describe('ui/modals/confirm_modal', function () {
showClose: true
};

function resetSpyCounts() {
beforeEach(() => {
confirmCallback.reset();
closeCallback.reset();
cancelCallback.reset();
}
});

it('onClose', function () {
resetSpyCounts();
confirmModal('hi', confirmModalOptions);
$rootScope.$digest();
findByDataTestSubj('confirmModalCloseButton').click();
Expand All @@ -143,7 +142,6 @@ describe('ui/modals/confirm_modal', function () {
});

it('onCancel', function () {
resetSpyCounts();
confirmModal('hi', confirmModalOptions);
$rootScope.$digest();
findByDataTestSubj('confirmModalCancelButton').click();
Expand All @@ -154,7 +152,6 @@ describe('ui/modals/confirm_modal', function () {
});

it('onConfirm', function () {
resetSpyCounts();
confirmModal('hi', confirmModalOptions);
$rootScope.$digest();
findByDataTestSubj('confirmModalConfirmButton').click();
Expand All @@ -166,7 +163,6 @@ describe('ui/modals/confirm_modal', function () {


it('onClose defaults to onCancel if not specified', function () {
resetSpyCounts();
const confirmModalOptions = {
confirmButtonText: 'bye',
onConfirm: confirmCallback,
Expand All @@ -182,5 +178,39 @@ describe('ui/modals/confirm_modal', function () {
expect(confirmCallback.called).to.be(false);
expect(cancelCallback.called).to.be(true);
});

it('onKeyDown detects ESC and calls onCancel', function () {
const confirmModalOptions = {
confirmButtonText: 'bye',
onConfirm: confirmCallback,
onCancel: cancelCallback,
title: 'hi'
};

confirmModal('hi', confirmModalOptions);

const e = angular.element.Event('keydown'); // eslint-disable-line new-cap
e.keyCode = 27;
angular.element(document.body).trigger(e);

expect(cancelCallback.called).to.be(true);
});

it('onKeyDown ignores keys other than ESC', function () {
const confirmModalOptions = {
confirmButtonText: 'bye',
onConfirm: confirmCallback,
onCancel: cancelCallback,
title: 'hi'
};

confirmModal('hi', confirmModalOptions);

const e = angular.element.Event('keydown'); // eslint-disable-line new-cap
e.keyCode = 50;
angular.element(document.body).trigger(e);

expect(cancelCallback.called).to.be(false);
});
});
});
1 change: 0 additions & 1 deletion src/ui/public/modals/confirm_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<div class="kuiModalHeader__title" data-test-subj="confirmModalTitleText">
{{title}}
</div>

<div
ng-if="showClose"
class="kuiModalHeaderCloseButton kuiIcon fa-times"
Expand Down
9 changes: 9 additions & 0 deletions src/ui/public/modals/confirm_modal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import angular from 'angular';
import { noop } from 'lodash';
import uiModules from 'ui/modules';
import template from './confirm_modal.html';
Expand Down Expand Up @@ -73,11 +74,19 @@ module.factory('confirmModal', function ($rootScope, $compile) {

const modalInstance = $compile(template)(confirmScope);
modalPopover = new ModalOverlay(modalInstance);

angular.element(document.body).on('keydown', (event) => {
if(event.keyCode === 27) {
confirmScope.onCancel();
}
});

modalInstance.find('[data-test-subj=confirmModalConfirmButton]').focus();

function destroy() {
modalPopover.destroy();
modalPopover = undefined;
angular.element(document.body).off('keydown');
confirmScope.$destroy();
}
};
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/modals/modal_overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class ModalOverlay {
constructor(modalElement) {
this.overlayElement = angular.element(modalOverlayTemplate);
this.overlayElement.append(modalElement);

angular.element(document.body).append(this.overlayElement);
}

Expand Down