diff --git a/demo/demo.component.ts b/demo/demo.component.ts index 54236d5..e965a62 100644 --- a/demo/demo.component.ts +++ b/demo/demo.component.ts @@ -1,4 +1,4 @@ -import { Component, ElementRef, ViewChild } from '@angular/core'; +import { Component, ElementRef, ViewChild, OnInit } from '@angular/core'; import { FormBuilder, FormGroup } from '@angular/forms'; import { CurrencyMaskInputMode } from '../src/currency-mask.config'; @@ -49,7 +49,7 @@ import { CurrencyMaskInputMode } from '../src/currency-mask.config'; ` }) -export class DemoComponent { +export class DemoComponent implements OnInit { @ViewChild('valueInput', { static: true }) valueInput: ElementRef; @@ -58,7 +58,7 @@ export class DemoComponent { prefix: 'R$ ', thousands: '.', decimal: ',', - allowNegative: false, + allowNegative: true, nullable: true, max: 250_000_000, inputMode: CurrencyMaskInputMode.FINANCIAL, @@ -69,7 +69,7 @@ export class DemoComponent { ngOnInit() { this.form = this.formBuilder.group({ - value: null, + value: 0, inputMode: this.ngxCurrencyOptions.inputMode, }); @@ -77,7 +77,7 @@ export class DemoComponent { this.ngxCurrencyOptions.inputMode = val; // Clear and focus the value input when the input mode is changed.container - this.form.get('value').setValue(null); + this.form.get('value').setValue(0); this.valueInput.nativeElement.focus(); }); diff --git a/src/input.service.ts b/src/input.service.ts index 1ab138b..47d3f8e 100644 --- a/src/input.service.ts +++ b/src/input.service.ts @@ -138,8 +138,8 @@ export class InputService { } } - let isZero = newValue == 0; - let operator = (isNegative && allowNegative && !isZero) ? "-" : ""; + // let isZero = newValue == 0; + let operator = (isNegative && allowNegative /*&& !isZero */) ? "-" : ""; return operator + prefix + newRawValue + suffix; } @@ -187,9 +187,9 @@ export class InputService { } changeToNegative(): void { - if (this.options.allowNegative && this.rawValue != "" && this.rawValue.charAt(0) != "-" && this.value != 0) { + if (this.options.allowNegative /*&& this.rawValue != ""*/ && this.rawValue.charAt(0) != "-" /*&& this.value != 0*/) { // Apply the mask to ensure the min and max values are enforced. - this.rawValue = this.applyMask(false, "-" + this.rawValue); + this.rawValue = this.applyMask(false, "-" + (this.rawValue ? this.rawValue : '0')); } } @@ -213,7 +213,7 @@ export class InputService { selectionEnd = Math.min(suffixStart, Math.max(selectionEnd, prefix.length)); selectionStart = Math.min(suffixStart, Math.max(selectionStart, prefix.length)); - // Check if selection was entirely in the prefix or suffix. + // Check if selection was entirely in the prefix or suffix. if (selectionStart === selectionEnd && this.inputSelection.selectionStart !== this.inputSelection.selectionEnd) { this.updateFieldValue(selectionStart); @@ -226,7 +226,7 @@ export class InputService { } let shiftSelection = 0; - let insertChars = ''; + let insertChars = ''; if (selectionEnd === selectionStart) { if (keyCode == 8) { if (selectionStart <= prefix.length) {