Skip to content

Commit

Permalink
fix: allow negative at start of entry
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkevla committed Jun 15, 2020
1 parent 5ce0077 commit dad7b33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions demo/demo.component.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -49,7 +49,7 @@ import { CurrencyMaskInputMode } from '../src/currency-mask.config';
</form>
`
})
export class DemoComponent {
export class DemoComponent implements OnInit {

@ViewChild('valueInput', { static: true }) valueInput: ElementRef;

Expand All @@ -58,7 +58,7 @@ export class DemoComponent {
prefix: 'R$ ',
thousands: '.',
decimal: ',',
allowNegative: false,
allowNegative: true,
nullable: true,
max: 250_000_000,
inputMode: CurrencyMaskInputMode.FINANCIAL,
Expand All @@ -69,15 +69,15 @@ export class DemoComponent {

ngOnInit() {
this.form = this.formBuilder.group({
value: null,
value: 0,
inputMode: this.ngxCurrencyOptions.inputMode,
});

this.form.get('inputMode').valueChanges.subscribe(val => {
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();
});

Expand Down
12 changes: 6 additions & 6 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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'));
}
}

Expand All @@ -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);
Expand All @@ -226,7 +226,7 @@ export class InputService {
}

let shiftSelection = 0;
let insertChars = '';
let insertChars = '';
if (selectionEnd === selectionStart) {
if (keyCode == 8) {
if (selectionStart <= prefix.length) {
Expand Down

0 comments on commit dad7b33

Please sign in to comment.