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

Commit 968aa23

Browse files
topherfangioThomasBurleson
authored andcommitted
fix(mdInput): Fix md-maxlength when used with ng-messages.
The recently added support for input elements to have multiple messages caused a rendering issue for the `md-maxlength` character counter. Update code/CSS to properly position the character counter when used with and without ng-messages. Fixes #4783. POSSIBLE BREAKING CHANGE - The `<div class="md-char-counter">` that is automatically added to the input is now added after the input if no messages are found, but inside if they are. This may cause some styling issues if users provide custom CSS. Users may need to update their CSS to take the extra HTML into account. Example rendered HTML: **Without `ng-messages`:** ```html <label>Label is here</label> <input /> <div class="md-char-counter">10/20</div> ``` **With `ng-messages`:** ```html <label>Label is here</label> <input /> <div ng-messages="..."> <div class="md-char-counter">10/20</div> <div ng-message="requried">This is required</div> </div> ``` Closes #4786.
1 parent 64fb803 commit 968aa23

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/components/input/demoErrors/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ <h1 class="md-toolbar-tools">
4040
<md-input-container>
4141
<label>Hourly Rate (USD)</label>
4242
<input required type="number" step="any" name="rate" ng-model="project.rate" min="800"
43-
max="4999" ng-pattern="/^1234$/">
43+
max="4999" ng-pattern="/^1234$/" md-maxlength="20" />
4444

4545
<div ng-messages="projectForm.rate.$error" multiple>
4646
<div ng-message="required">

src/components/input/input-theme.scss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ md-input-container.md-THEME_NAME-theme {
2020
ng-message, data-ng-message, x-ng-message,
2121
[ng-message], [data-ng-message], [x-ng-message],
2222
[ng-message-exp], [data-ng-message-exp], [x-ng-message-exp] {
23-
color: '{{warn-500}}';
23+
:not(.md-char-counter) {
24+
color: '{{warn-500}}';
25+
}
2426
}
2527

2628
&:not(.md-input-invalid) {

src/components/input/input.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,25 @@ function mdMaxlengthDirective($animate) {
362362
// Stop model from trimming. This makes it so whitespace
363363
// over the maxlength still counts as invalid.
364364
attr.$set('ngTrim', 'false');
365-
input.after(charCountEl);
365+
366+
var ngMessagesSelectors = [
367+
'ng-messages',
368+
'data-ng-messages',
369+
'x-ng-messages',
370+
'[ng-messages]',
371+
'[data-ng-messages]',
372+
'[x-ng-messages]'
373+
];
374+
375+
var ngMessages = containerCtrl.element[0].querySelector(ngMessagesSelectors.join(','));
376+
377+
// If we have an ngMessages container, put the counter at the top; otherwise, put it after the
378+
// input so it will be positioned properly in the SCSS
379+
if (ngMessages) {
380+
angular.element(ngMessages).prepend(charCountEl);
381+
} else {
382+
input.after(charCountEl);
383+
}
366384

367385
ngModelCtrl.$formatters.push(renderCharCount);
368386
ngModelCtrl.$viewChangeListeners.push(renderCharCount);

src/components/input/input.scss

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ md-input-container {
166166
}
167167

168168
.md-char-counter {
169-
position: absolute;
170-
right: 0;
169+
position: relative;
170+
text-align: right;
171171
order: 3;
172172
}
173173

@@ -176,6 +176,12 @@ md-input-container {
176176
position: relative;
177177
order: 4;
178178
min-height: $input-error-height;
179+
180+
.md-char-counter {
181+
position: absolute;
182+
top: 0;
183+
right: 0;
184+
}
179185
}
180186

181187
ng-message, data-ng-message, x-ng-message,

0 commit comments

Comments
 (0)