Skip to content

Commit

Permalink
Merge pull request #99 from iamkevla/master
Browse files Browse the repository at this point in the history
fix: allow negative at start of entry
  • Loading branch information
nbfontana committed Nov 30, 2022
2 parents 175b6e8 + d898311 commit d095e89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 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: 7 additions & 5 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,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 @@ -192,9 +192,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 @@ -218,7 +218,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 @@ -232,8 +232,10 @@ export class InputService {

let shiftSelection = 0;
let insertChars = '';

const isCursorInDecimals = decimalIndex < selectionEnd;
const isCursorImmediatelyAfterDecimalPoint = decimalIndex + 1 === selectionEnd;

if (selectionEnd === selectionStart) {
if (keyCode == 8) {
if (selectionStart <= prefix.length) {
Expand Down

0 comments on commit d095e89

Please sign in to comment.