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

Commit

Permalink
fix(textfield): improved logic to determine in md-input has a value
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBurleson committed Oct 21, 2014
1 parent 1ffeddc commit 5c407b5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/components/textField/textField.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ function mdInputGroupDirective() {
$element.toggleClass('md-input-focused', !!isFocused);
};
this.setHasValue = function(hasValue) {
$element.toggleClass('md-input-has-value', !!hasValue);
$element.toggleClass('md-input-has-value', hasValue );
};
}]
};

}

/*
Expand Down Expand Up @@ -147,15 +148,13 @@ function mdInputDirective($mdUtil) {
if (ngModelCtrl) {
//Add a $formatter so we don't use up the render function
ngModelCtrl.$formatters.push(function(value) {
inputGroupCtrl.setHasValue(angular.isDefined(value) && value!==null );
inputGroupCtrl.setHasValue( isNotEmpty(value) );
return value;
});
}

element.on('input', function() {
var value = element.val();

inputGroupCtrl.setHasValue(angular.isDefined(value) && value!==null);
inputGroupCtrl.setHasValue( isNotEmpty() );
});

// When the input focuses, add the focused class to the group
Expand All @@ -165,12 +164,20 @@ function mdInputDirective($mdUtil) {
// When the input blurs, remove the focused class from the group
element.on('blur', function(e) {
inputGroupCtrl.setFocused(false);
inputGroupCtrl.setHasValue( isNotEmpty() );
});

scope.$on('$destroy', function() {
inputGroupCtrl.setFocused(false);
inputGroupCtrl.setHasValue(false);
});


function isNotEmpty(value) {
value = angular.isUndefined(value) ? element.val() : value;
return (angular.isDefined(value) && (value!==null) &&
(value.toString().trim() != ""));
}
}
};
}
Expand Down

0 comments on commit 5c407b5

Please sign in to comment.