Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option to allow a negative sign to be entered before a number… #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Available options:

* `align` - Text alignment in input. (default: `right`)
* `allowNegative` - If `true` can input negative values. (default: `true`)
* `allowNegativeZero` - If `true` can input a negative sign before numbers are entered. (default: `false`)
* `decimal` - Separator of decimals (default: `'.'`)
* `precision` - Number of decimal places (default: `2`)
* `prefix` - Money prefix (default: `'$ '`)
Expand Down
1 change: 1 addition & 0 deletions src/currency-mask.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface CurrencyMaskConfig {

align: string;
allowNegative: boolean;
allowNegativeZero: boolean;
allowZero: boolean;
decimal: string;
precision: number;
Expand Down
1 change: 1 addition & 0 deletions src/currency-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class CurrencyMaskDirective implements AfterViewInit, ControlValueAccesso
optionsTemplate = {
align: "right",
allowNegative: true,
allowNegativeZero: false,
allowZero: true,
decimal: ".",
precision: 2,
Expand Down
6 changes: 4 additions & 2 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class InputService {
}

applyMask(isNumber: boolean, rawValue: string): string {
let { allowNegative, decimal, precision, prefix, suffix, thousands } = this.options;
let { allowNegative, allowNegativeZero, decimal, precision, prefix, suffix, thousands } = this.options;
rawValue = isNumber ? new Number(rawValue).toFixed(precision) : rawValue;
let onlyNumbers = rawValue.replace(/[^0-9]/g, "");

Expand All @@ -43,7 +43,7 @@ export class InputService {
}

let isZero = parseInt(integerPart) == 0 && (parseInt(decimalPart) == 0 || decimalPart == "");
let operator = (rawValue.indexOf("-") > -1 && allowNegative && !isZero) ? "-" : "";
let operator = (rawValue.indexOf("-") > -1 && allowNegative && (!isZero || allowNegativeZero)) ? "-" : "";
return operator + prefix + newRawValue + suffix;
}

Expand All @@ -64,6 +64,8 @@ export class InputService {
changeToNegative(): void {
if (this.options.allowNegative && this.rawValue != "" && this.rawValue.charAt(0) != "-" && this.value != 0) {
this.rawValue = "-" + this.rawValue;
} else if(this.options.allowNegativeZero && (this.rawValue == "" || this.value == 0)) {
this.rawValue = this.applyMask(false, "-0");
}
}

Expand Down