From 340ee6d742db1c70ad96da1c6aa0b35dc94f38ea Mon Sep 17 00:00:00 2001 From: Igor Rafael Date: Sun, 7 Jun 2015 18:04:19 +0200 Subject: [PATCH] feat(ui-br-phone-number): preserve the original model type --- src/br/phone/br-phone.js | 7 ++++--- src/br/phone/br-phone.test.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/br/phone/br-phone.js b/src/br/phone/br-phone.js index 97f29fff..bc98611c 100644 --- a/src/br/phone/br-phone.js +++ b/src/br/phone/br-phone.js @@ -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) { @@ -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; } diff --git a/src/br/phone/br-phone.test.js b/src/br/phone/br-phone.test.js index f0921a2c..2629d65b 100644 --- a/src/br/phone/br-phone.test.js +++ b/src/br/phone/br-phone.test.js @@ -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('', { + 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); + }); });