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

Commit e6176fb

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(input): ngMessages height changing.
When using `ng-messages` inside of an input container, the height of the messages will change when appearing, which pushes other elements out of the way. This was a regression from the previous behavior which allowed space for a one-line message before pushing other content out of the way. This was caused by some changes to the icons inside of an input. Fix by finding an alternate method to address the icon issue. Fixes #7072. Closes #7146
1 parent 70489c2 commit e6176fb

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/components/input/demoErrors/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@
5757
doubt event Bill Clinton could afford that.
5858
</div>
5959
</div>
60-
<p>
61-
Make sure to include <a href="https://docs.angularjs.org/api/ngMessages" target="_blank">ngMessages</a> module when using ng-message markup.
62-
</p>
6360
</md-input-container>
61+
62+
<p>
63+
Make sure to include <a href="https://docs.angularjs.org/api/ngMessages" target="_blank">ngMessages</a> module when using ng-message markup.
64+
</p>
6465
</form>
6566
</md-content>
6667

src/components/input/input.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ angular.module('material.components.input', [
6565
*/
6666
function mdInputContainerDirective($mdTheming, $parse) {
6767

68-
var INPUT_TAGS = ['INPUT', 'TEXTAREA', 'MD-SELECT'];
68+
var INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT', 'MD-SELECT'];
69+
70+
var LEFT_SELECTORS = INPUT_TAGS.reduce(function(selectors, isel) {
71+
return selectors.concat(['md-icon ~ ' + isel, '.md-icon ~ ' + isel]);
72+
}, []).join(",");
73+
74+
var RIGHT_SELECTORS = INPUT_TAGS.reduce(function(selectors, isel) {
75+
return selectors.concat([isel + ' ~ md-icon', isel + ' ~ .md-icon']);
76+
}, []).join(",");
6977

7078
return {
7179
restrict: 'E',
@@ -76,21 +84,12 @@ function mdInputContainerDirective($mdTheming, $parse) {
7684
function postLink(scope, element) {
7785
$mdTheming(element);
7886

79-
var iconElements = element.find('md-icon');
80-
var icons = iconElements.length ? iconElements : element[0].getElementsByClassName('md-icon');
81-
82-
// Incase there's one icon we want to identify where the icon is (right or left) and apply the related class
83-
if (icons.length == 1) {
84-
var next = icons[0].nextElementSibling;
85-
var previous = icons[0].previousElementSibling;
87+
// Check for both a left & right icon
88+
var leftIcon = element[0].querySelector(LEFT_SELECTORS);
89+
var rightIcon = element[0].querySelector(RIGHT_SELECTORS);
8690

87-
element.addClass(next && INPUT_TAGS.indexOf(next.tagName) != -1 ? 'md-icon-left' :
88-
previous && INPUT_TAGS.indexOf(previous.tagName) != -1 ? 'md-icon-right' : '');
89-
}
90-
// In case there are two icons we apply both icon classes
91-
else if (icons.length == 2) {
92-
element.addClass('md-icon-left md-icon-right');
93-
}
91+
if (leftIcon) { element.addClass('md-icon-left'); }
92+
if (rightIcon) { element.addClass('md-icon-right'); }
9493
}
9594

9695
function ContainerCtrl($scope, $element, $attrs, $animate) {
@@ -276,8 +275,7 @@ function inputTextareaDirective($mdUtil, $window, $mdAria) {
276275

277276
// Add an error spacer div after our input to provide space for the char counter and any ng-messages
278277
var errorsSpacer = angular.element('<div class="md-errors-spacer">');
279-
// element.after appending the div before the icon (if exist) which cause a problem with calculating which class to apply
280-
element.parent().append(errorsSpacer);
278+
element.after(errorsSpacer);
281279

282280
if (!containerCtrl.label) {
283281
$mdAria.expect(element, 'aria-label', element.attr('placeholder'));

0 commit comments

Comments
 (0)