Skip to content

Commit

Permalink
feat(ui-br-phone-number): preserve the original model type
Browse files Browse the repository at this point in the history
  • Loading branch information
assisrafael committed Jun 7, 2015
1 parent 20517ea commit 340ee6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/br/phone/br-phone.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function BrPhoneNumberDirective() {
phoneMask9D = new StringMask('(00) 00000-0000');

function removeNonDigits(value) {
return value.replace(/[^0-9]/g, '');
return value.toString().replace(/[^0-9]/g, '');
}

function applyPhoneMask(value) {
Expand Down Expand Up @@ -49,11 +49,12 @@ function BrPhoneNumberDirective() {
ctrl.$render();
}

return actualValue;
return angular.isNumber(ctrl.$modelValue) ? parseInt(actualValue) : actualValue;
}

function validator(value) {
var valid = ctrl.$isEmpty(value) || value.length === 10 || value.length === 11;
var valueLength = value && value.toString().length;
var valid = ctrl.$isEmpty(value) || valueLength === 10 || valueLength === 11;
ctrl.$setValidity('brPhoneNumber', valid);
return value;
}
Expand Down
13 changes: 13 additions & 0 deletions src/br/phone/br-phone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ describe('ui-br-phone-number', function() {
input.val('12345678901').triggerHandler('input');
expect(model.$error.brPhoneNumber).toBe(false);
});

it('should use the type of the model value (if initialized)', function() {
var input = TestUtil.compile('<input ng-model="model" ui-br-phone-number>', {
model: 1234567890
});

var model = input.controller('ngModel');
expect(model.$viewValue).toBe('(12) 3456-7890');
expect(model.$modelValue).toBe(1234567890);
input.val('12345678901').triggerHandler('input');
expect(model.$viewValue).toBe('(12) 34567-8901');
expect(model.$modelValue).toBe(12345678901);
});
});

0 comments on commit 340ee6d

Please sign in to comment.