Skip to content

Commit

Permalink
feat(uiNumberMask): allow minus sign as first character for negative …
Browse files Browse the repository at this point in the history
…numbers

* Allow minus sign a first character for negative numbers (fixes #147)

* Allow minus sign as first character for negative numbers

* Adding unit test to check if the field is cleared when it only contains the minus sign
  • Loading branch information
brunobastosg authored and assisrafael committed May 21, 2016
1 parent af759b4 commit ef449d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/global/number/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function NumberMaskDirective($locale, $parse, PreFormatters, NumberMasks) {
var isNegative = (value[0] === '-'),
needsToInvertSign = (value.slice(-1) === '-');

//only apply the minus sign if it is negative or(exclusive)
//only apply the minus sign if it is negative or(exclusive) or the first character
//needs to be negative and the number is different from zero
if (needsToInvertSign ^ isNegative && !!actualNumber) {
if ((needsToInvertSign ^ isNegative) || value === '-') {
actualNumber *= -1;
formatedValue = '-' + formatedValue;
formatedValue = '-' + ((actualNumber !== 0) ? formatedValue : '');
}
}

Expand All @@ -61,6 +61,15 @@ function NumberMaskDirective($locale, $parse, PreFormatters, NumberMasks) {
return prefix + viewMask.apply(valueToFormat);
}

function clearViewValueIfMinusSign() {
if (ctrl.$viewValue === '-') {
ctrl.$setViewValue('');
ctrl.$render();
}
}

element.on('blur', clearViewValueIfMinusSign);

ctrl.$formatters.push(formatter);
ctrl.$parsers.push(parser);

Expand Down
40 changes: 37 additions & 3 deletions src/global/number/number.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('ui.utils.masks.number', function() {
expect(input.getAttribute('value')).toEqual('12.341,89');
});

it('should not allow -0 parsing it to 0', function() {
it('should not allow -0 parsing it to - (just the minus sign)', function() {
var input = element(by.model('numberWith2Decimals'));

input.clear();
Expand All @@ -118,7 +118,8 @@ describe('ui.utils.masks.number', function() {
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('-0,01');
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('0,00');
expect(input.getAttribute('value')).toEqual('-');
input.sendKeys(protractor.Key.BACK_SPACE);
input.sendKeys(1);
expect(input.getAttribute('value')).toEqual('0,01');
input.sendKeys('-');
Expand All @@ -136,7 +137,7 @@ describe('ui.utils.masks.number', function() {
input.sendKeys(protractor.Key.BACK_SPACE);
input.sendKeys(protractor.Key.BACK_SPACE);
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('0,00');
expect(input.getAttribute('value')).toEqual('-');
input.sendKeys('-');
expect(input.getAttribute('value')).toEqual('0,00');
});
Expand Down Expand Up @@ -305,5 +306,38 @@ describe('ui.utils.masks.number', function() {
expect(input.getAttribute('value')).toEqual('123');
expect(value.getText()).toEqual('123');
});

it('should accept minus sign as first character when the ui-negative attribute is present', function() {
var input = element(by.model('numberWith2Decimals'));

input.sendKeys('-');
expect(input.getAttribute('value')).toEqual('-');
input.sendKeys('1');
expect(input.getAttribute('value')).toEqual('-0,01');
input.sendKeys('2');
expect(input.getAttribute('value')).toEqual('-0,12');
input.sendKeys('3');
expect(input.getAttribute('value')).toEqual('-1,23');
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('-0,12');
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('-0,01');
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('-');
input.sendKeys(protractor.Key.BACK_SPACE);
expect(input.getAttribute('value')).toEqual('');
});

it('should clear field when it contains only minus sign', function() {
var input = element(by.model('numberWith2Decimals'));

input.sendKeys('-');
input.sendKeys(protractor.Key.TAB);
expect(input.getAttribute('value')).toEqual('');
input.click();
input.sendKeys('-123');
input.sendKeys(protractor.Key.TAB);
expect(input.getAttribute('value')).toEqual('-1,23');
});
});
});
12 changes: 12 additions & 0 deletions src/global/number/number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,16 @@ describe('ui-number-mask', function() {
expect(model.$viewValue).toBe('-1,234.56');
expect(model.$modelValue).toBe(-1234.56);
});

it('should clear field on blur if it contains only minus sign', function() {
var input = TestUtil.compile('<input ng-model="model" ui-number-mask ui-negative-number>');
var model = input.controller('ngModel');

input.val('-').triggerHandler('input');
expect(model.$viewValue).toBe('-');
expect(model.$modelValue).toBe(0);
input.triggerHandler('blur');
expect(model.$viewValue).toBe('');
expect(model.$modelValue).toBe(null);
});
});

0 comments on commit ef449d8

Please sign in to comment.