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

Commit

Permalink
fix(input): add check for input value on blur
Browse files Browse the repository at this point in the history
Closes #1201.
  • Loading branch information
ajoslin committed Jan 15, 2015
1 parent b432277 commit ec53d1a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
41 changes: 17 additions & 24 deletions src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
function postLink(scope, element, attr, ctrls) {

var containerCtrl = ctrls[0];
var ngModelCtrl = ctrls[1];
var ngModelCtrl = ctrls[1] || $mdUtil.fakeNgModel();

if ( !containerCtrl ) return;
if (containerCtrl.input) {
Expand All @@ -187,38 +187,31 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
setupTextarea();
}

var isEmpty = ngModelCtrl ?
ngModelCtrl.$isEmpty :
function() { return ('' + element.val()).length === 0; };

// When the input value changes, check if it "has" a value, and
// set the appropriate class on the input group
if (ngModelCtrl) {
scope.$watch(function() {
return ngModelCtrl.$dirty && ngModelCtrl.$invalid;
}, containerCtrl.setInvalid);

ngModelCtrl.$formatters.push(checkHasValue);
ngModelCtrl.$parsers.push(checkHasValue);
} else {
checkHasValue();
function ngModelPipelineCheckValue(arg) {
containerCtrl.setHasValue(!ngModelCtrl.$isEmpty(arg));
return arg;
}
element.on('input', checkHasValue);

function checkHasValue(value) {
containerCtrl.setHasValue(
!isEmpty(value) ||
(element[0].validity || {}).badInput // allow badInput to count as having a value.
);
return value;
function inputCheckValue() {
// An input's value counts if its length > 0,
// or if the input's validity state says it has bad input (eg string in a number input)
containerCtrl.setHasValue(element.val().length > 0 || (element[0].validity||{}).badInput);
}

scope.$watch(function() {
return ngModelCtrl.$dirty && ngModelCtrl.$invalid;
}, containerCtrl.setInvalid);

ngModelCtrl.$parsers.push(ngModelPipelineCheckValue);
ngModelCtrl.$formatters.push(ngModelPipelineCheckValue);

element
.on('input', inputCheckValue)
.on('focus', function(ev) {
containerCtrl.setFocused(true);
})
.on('blur', function(ev) {
containerCtrl.setFocused(false);
inputCheckValue();
});

scope.$on('$destroy', function() {
Expand Down
8 changes: 5 additions & 3 deletions src/components/input/input.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
describe('md-input-container directive', function() {
beforeEach(module('material.components.input'));

function setup(attrs) {
function setup(attrs, isForm) {
var container;
inject(function($rootScope, $compile) {
container = $compile('<md-input-container><input ' +(attrs||'')+ '><label></label></md-input-container>')($rootScope);
container = $compile((isForm ? '<form>' : '') +
'<md-input-container><input ' +(attrs||'')+ '><label></label></md-input-container>' +
(isForm ? '<form>' : ''))($rootScope);
$rootScope.$apply();
});
return container;
Expand Down Expand Up @@ -56,7 +58,7 @@ describe('md-input-container directive', function() {
expect(el.find('label').attr('for')).toBe(el.find('input').attr('id'));
}));

ddescribe('md-maxlength', function() {
describe('md-maxlength', function() {
function getCharCounter(el) {
return angular.element(el[0].querySelector('.md-char-counter'));
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/util/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ angular.module('material.core')

fakeNgModel: function() {
return {
$fake: true,
$setViewValue: function(value) {
this.$viewValue = value;
this.$render(value);
this.$viewChangeListeners.forEach(function(cb) { cb(); });
},
$isEmpty: function(value) {
return (''+value).length === 0;
},
$parsers: [],
$formatters: [],
$viewChangeListeners: [],
Expand Down

0 comments on commit ec53d1a

Please sign in to comment.