Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 0917523

Browse files
committed
fix(select): ngModel.$touched trigger after menu close
closes #5256
1 parent cba5fa7 commit 0917523

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/components/select/select.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
147147
return function postLink(scope, element, attr, ctrls) {
148148
var isDisabled;
149149

150+
var firstOpen = true;
151+
150152
var containerCtrl = ctrls[0];
151153
var mdSelectCtrl = ctrls[1];
152154
var ngModelCtrl = ctrls[2];
@@ -200,6 +202,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
200202

201203
attr.$observe('placeholder', ngModelCtrl.$render);
202204

205+
203206
mdSelectCtrl.setLabelText = function(text) {
204207
mdSelectCtrl.setIsPlaceholder(!text);
205208
// Use placeholder attribute, otherwise fallback to the md-input-container label
@@ -314,6 +317,7 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
314317
element.on('keydown', handleKeypress);
315318
}
316319

320+
317321
var ariaAttrs = {
318322
role: 'combobox',
319323
'aria-expanded': 'false'
@@ -387,6 +391,10 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
387391

388392
function openSelect() {
389393
selectScope.isOpen = true;
394+
if (firstOpen) {
395+
element.on('blur', setUntouched);
396+
firstOpen = false;
397+
}
390398

391399
$mdSelect.show({
392400
scope: selectScope,
@@ -396,9 +404,15 @@ function SelectDirective($mdSelect, $mdUtil, $mdTheming, $mdAria, $compile, $par
396404
target: element[0],
397405
hasBackdrop: true,
398406
loadingAsync: attr.mdOnOpen ? scope.$eval(attr.mdOnOpen) || true : false
399-
}).then(function() {
407+
}).finally(function() {
400408
selectScope.isOpen = false;
409+
ngModelCtrl.$setTouched();
401410
});
411+
412+
function setUntouched() {
413+
ngModelCtrl.$setUntouched();
414+
element.off('blur', setUntouched);
415+
}
402416
}
403417
};
404418
}

src/components/select/select.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ describe('<md-select>', function() {
5959
expect(called).toBe(true);
6060
}));
6161

62+
it('should set touched only after closing', inject(function($compile, $rootScope) {
63+
var form = $compile('<form name="myForm">' +
64+
'<md-select name="select" ng-model="val">' +
65+
'<md-option>1</md-option>' +
66+
'</md-select>' +
67+
'</form>')($rootScope);
68+
var select = form.find('md-select');
69+
openSelect(select);
70+
expect($rootScope.myForm.select.$touched).toBe(false);
71+
closeSelect();
72+
expect($rootScope.myForm.select.$touched).toBe(true);
73+
}));
74+
6275
it('closes the menu during scope.$destroy()', inject(function($document, $rootScope, $timeout) {
6376
var container = angular.element("<div></div>");
6477
var scope = $rootScope.$new();
@@ -815,9 +828,17 @@ describe('<md-select>', function() {
815828
try {
816829
el.triggerHandler('click');
817830
waitForSelectOpen();
831+
el.triggerHandler('blur');
818832
} catch(e) { }
819833
}
820834

835+
function closeSelect() {
836+
inject(function($document) {
837+
$document.find('md-backdrop').triggerHandler('click');
838+
});
839+
waitForSelectClose();
840+
}
841+
821842

822843
function pressKey(el, code) {
823844
el.triggerHandler({

test/angular-material-mocks.js

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ angular.module('ngMaterial-mock', [
4747
$timeout.flush();
4848
this.flushOutstandingAnimations();
4949
$timeout.flush();
50+
this.flushOutstandingAnimations();
51+
$timeout.flush();
5052
}
5153
};
5254
}]);

0 commit comments

Comments
 (0)