From 370733b332029f58158bc4a272be8ed6fb9fac28 Mon Sep 17 00:00:00 2001 From: Remco Date: Sun, 22 Jun 2014 14:30:26 +0200 Subject: [PATCH] feat(modal): added support for autofocus fields on open --- src/modal/modal.js | 2 +- src/modal/test/modal.spec.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index 317f36737d..a66ba551b5 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -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) { diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index d3424c06ed..2127a8858b 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -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 () { @@ -183,6 +187,18 @@ describe('$modal', function () { expect($document).not.toHaveBackdrop(); }); + it('should focus on autofocus element', function () { + var modal = open({template: '
Content
'}); + $timeout.flush(); + expect(isFocused($document.find('#test877'))).toBe(true); + }); + + it('should not focus on non-autofocus element', function () { + var modal = open({template: '
Content
'}); + $timeout.flush(); + expect(isFocused($document.find('#test877'))).toBe(false); + }); + it('should support closing on ESC', function () { var modal = open({template: '
Content
'});