Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

feat(modal): added support for autofocus fields on open #2377

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
// trigger CSS transitions
scope.animate = true;
// focus a freshly-opened modal
element[0].focus();
(element.find('*[autofocus]')[0] || element[0]).focus();
});

scope.close = function (evt) {
Expand Down
16 changes: 16 additions & 0 deletions src/modal/test/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ describe('$modal', function () {
$rootScope.$digest();
}

function isFocused(elm) {
return elm[0] === document.activeElement;
}

describe('basic scenarios with default options', function () {

it('should open and dismiss a modal with a minimal set of options', function () {
Expand Down Expand Up @@ -183,6 +187,18 @@ describe('$modal', function () {
expect($document).not.toHaveBackdrop();
});

it('should focus on autofocus element', function () {
var modal = open({template: '<div>Content<input id="test877" autofocus></div>'});
$timeout.flush();
expect(isFocused($document.find('#test877'))).toBe(true);
});

it('should not focus on non-autofocus element', function () {
var modal = open({template: '<div>Content<input id="test877"></div>'});
$timeout.flush();
expect(isFocused($document.find('#test877'))).toBe(false);
});

it('should support closing on ESC', function () {

var modal = open({template: '<div>Content</div>'});
Expand Down