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

Autofocus corrects the second time that the modal is open #2945

Closed
wants to merge 2 commits 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
7 changes: 7 additions & 0 deletions misc/test-lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ beforeEach(function() {
var element = angular.element(this.actual);
return element.hasClass('ng-hide') ||
element.css('display') == 'none';
},
toHaveFocus: function () {
this.message = function () {
return 'Expected \'' + angular.mock.dump(this.actual) + '\' to have focus';
};

return document.activeElement === this.actual[0];
}
});
});
6 changes: 5 additions & 1 deletion src/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
scope.size = attrs.size;

$timeout(function () {
var inputsWithAutofocus = element[0].querySelectorAll('[autofocus]');

// trigger CSS transitions
scope.animate = true;

Expand All @@ -103,7 +105,9 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.transition'])
* the onscreen keyboard. Fixed by updated the focusing logic to only autofocus
* the modal element if the modal does not contain an autofocus element.
*/
if (!element[0].querySelectorAll('[autofocus]').length) {
if (inputsWithAutofocus.length) {
inputsWithAutofocus[0].focus();
} else {
element[0].focus();
}
});
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 @@ -238,6 +238,22 @@ describe('$modal', function () {
expect(modal.opened).toBeRejectedWith(false);
});

it('should focus on the element that has autofocus attribute when the modal is open/reopen', function () {
function openAndCloseModalWithAutofocusElement() {
var modal = open({template: '<div><input type="text" id="auto-focus-element" autofocus></div>'});

waitForBackdropAnimation();

expect(angular.element('#auto-focus-element')).toHaveFocus();

close(modal, 'closed ok');

expect(modal.result).toBeResolvedWith('closed ok');
}

openAndCloseModalWithAutofocusElement();
openAndCloseModalWithAutofocusElement();
});
});

describe('default options can be changed in a provider', function () {
Expand Down