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

Commit

Permalink
fix(mdInput): Fix md-maxlength when used with ng-messages.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
topherfangio authored and ThomasBurleson committed Sep 23, 2015
1 parent 64fb803 commit 968aa23
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/input/demoErrors/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h1 class="md-toolbar-tools">
<md-input-container>
<label>Hourly Rate (USD)</label>
<input required type="number" step="any" name="rate" ng-model="project.rate" min="800"
max="4999" ng-pattern="/^1234$/">
max="4999" ng-pattern="/^1234$/" md-maxlength="20" />

<div ng-messages="projectForm.rate.$error" multiple>
<div ng-message="required">
Expand Down
4 changes: 3 additions & 1 deletion src/components/input/input-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ md-input-container.md-THEME_NAME-theme {
ng-message, data-ng-message, x-ng-message,
[ng-message], [data-ng-message], [x-ng-message],
[ng-message-exp], [data-ng-message-exp], [x-ng-message-exp] {
color: '{{warn-500}}';
:not(.md-char-counter) {
color: '{{warn-500}}';
}
}

&:not(.md-input-invalid) {
Expand Down
20 changes: 19 additions & 1 deletion src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,25 @@ function mdMaxlengthDirective($animate) {
// Stop model from trimming. This makes it so whitespace
// over the maxlength still counts as invalid.
attr.$set('ngTrim', 'false');
input.after(charCountEl);

var ngMessagesSelectors = [
'ng-messages',
'data-ng-messages',
'x-ng-messages',
'[ng-messages]',
'[data-ng-messages]',
'[x-ng-messages]'
];

var ngMessages = containerCtrl.element[0].querySelector(ngMessagesSelectors.join(','));

// If we have an ngMessages container, put the counter at the top; otherwise, put it after the
// input so it will be positioned properly in the SCSS
if (ngMessages) {
angular.element(ngMessages).prepend(charCountEl);
} else {
input.after(charCountEl);
}

ngModelCtrl.$formatters.push(renderCharCount);
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
Expand Down
10 changes: 8 additions & 2 deletions src/components/input/input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ md-input-container {
}

.md-char-counter {
position: absolute;
right: 0;
position: relative;
text-align: right;
order: 3;
}

Expand All @@ -176,6 +176,12 @@ md-input-container {
position: relative;
order: 4;
min-height: $input-error-height;

.md-char-counter {
position: absolute;
top: 0;
right: 0;
}
}

ng-message, data-ng-message, x-ng-message,
Expand Down

0 comments on commit 968aa23

Please sign in to comment.