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

Commit b432277

Browse files
committed
fix(input): fix error when md-maxlength attribute changes
1 parent 1cabb62 commit b432277

2 files changed

Lines changed: 62 additions & 9 deletions

File tree

src/components/input/input.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,9 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
164164
return {
165165
restrict: 'E',
166166
require: ['^?mdInputContainer', '?ngModel'],
167-
compile: compile,
167+
link: postLink
168168
};
169169

170-
function compile(element) {
171-
element.addClass('md-input');
172-
return postLink;
173-
}
174170
function postLink(scope, element, attr, ctrls) {
175171

176172
var containerCtrl = ctrls[0];
@@ -182,6 +178,7 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
182178
}
183179
containerCtrl.input = element;
184180

181+
element.addClass('md-input');
185182
if (!element.attr('id')) {
186183
element.attr('id', 'input_' + $mdUtil.nextUid());
187184
}
@@ -272,7 +269,7 @@ function inputTextareaDirective($mdUtil, $window, $compile, $animate) {
272269
}
273270
}
274271

275-
function mdMaxlengthDirective() {
272+
function mdMaxlengthDirective($animate) {
276273
return {
277274
restrict: 'A',
278275
require: ['ngModel', '^mdInputContainer'],
@@ -292,7 +289,9 @@ function mdMaxlengthDirective() {
292289

293290
ngModelCtrl.$formatters.push(renderCharCount);
294291
ngModelCtrl.$viewChangeListeners.push(renderCharCount);
295-
element.on('input keydown', renderCharCount);
292+
element.on('input keydown', function() {
293+
renderCharCount(); //make sure it's called with no args
294+
});
296295

297296
scope.$watch(attr.mdMaxlength, function(value) {
298297
maxlength = value;
@@ -311,11 +310,11 @@ function mdMaxlengthDirective() {
311310
if (!angular.isNumber(maxlength) || maxlength < 0) {
312311
return true;
313312
}
314-
return ( element.val() || modelValue || viewValue || '' ).length <= maxlength;
313+
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
315314
};
316315

317316
function renderCharCount(value) {
318-
charCountEl.text( element.val().length + '/' + maxlength );
317+
charCountEl.text( ( element.val() || value || '' ).length + '/' + maxlength );
319318
return value;
320319
}
321320
}

src/components/input/input.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,58 @@ describe('md-input-container directive', function() {
5555
expect(el.find('input').attr('id')).toBeTruthy();
5656
expect(el.find('label').attr('for')).toBe(el.find('input').attr('id'));
5757
}));
58+
59+
ddescribe('md-maxlength', function() {
60+
function getCharCounter(el) {
61+
return angular.element(el[0].querySelector('.md-char-counter'));
62+
}
63+
64+
it('should work with a constant', inject(function($rootScope, $compile) {
65+
var el = $compile('<form name="form">' +
66+
'<md-input-container>' +
67+
'<input md-maxlength="5" ng-model="foo" name="foo">' +
68+
'</md-input-container>' +
69+
'</form>')($rootScope);
70+
$rootScope.$apply();
71+
expect($rootScope.form.foo.$error['md-maxlength']).toBeFalsy();
72+
expect(getCharCounter(el).text()).toBe('0/5');
73+
74+
$rootScope.$apply('foo = "abcde"');
75+
expect($rootScope.form.foo.$error['md-maxlength']).toBeFalsy();
76+
expect(getCharCounter(el).text()).toBe('5/5');
77+
78+
$rootScope.$apply('foo = "abcdef"');
79+
el.find('input').triggerHandler('input');
80+
expect($rootScope.form.foo.$error['md-maxlength']).toBe(true);
81+
expect(getCharCounter(el).text()).toBe('6/5');
82+
83+
$rootScope.$apply('foo = "abc"');
84+
el.find('input').triggerHandler('input');
85+
expect($rootScope.form.foo.$error['md-maxlength']).toBeFalsy();
86+
expect(getCharCounter(el).text()).toBe('3/5');
87+
}));
88+
89+
it('should add and remove maxlength element & error with expression', inject(function($rootScope, $compile) {
90+
var el = $compile('<form name="form">' +
91+
'<md-input-container>' +
92+
'<input md-maxlength="max" ng-model="foo" name="foo">' +
93+
'</md-input-container>' +
94+
'</form>')($rootScope);
95+
96+
$rootScope.$apply();
97+
expect($rootScope.form.foo.$error['md-maxlength']).toBeFalsy();
98+
expect(getCharCounter(el).length).toBe(0);
99+
100+
$rootScope.$apply('max = 5');
101+
$rootScope.$apply('foo = "abcdef"');
102+
expect($rootScope.form.foo.$error['md-maxlength']).toBeTruthy();
103+
expect(getCharCounter(el).length).toBe(1);
104+
expect(getCharCounter(el).text()).toBe('6/5');
105+
106+
$rootScope.$apply('max = -1');
107+
$rootScope.$apply('foo = "abcdefg"');
108+
expect($rootScope.form.foo.$error['md-maxlength']).toBeFalsy();
109+
expect(getCharCounter(el).length).toBe(0);
110+
}));
111+
});
58112
});

0 commit comments

Comments
 (0)