Skip to content

Commit

Permalink
resolve #8231 When using the numeric input mask, it's impossible to r…
Browse files Browse the repository at this point in the history
…eset a field's value (#8250)

Co-authored-by: OlgaLarina <olga.larina.dev@gmail.com>
  • Loading branch information
OlgaLarina and OlgaLarina committed May 8, 2024
1 parent 5637bb0 commit 1feeffa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/mask/mask_numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ export class InputMaskNumeric extends InputMaskBase {
return result;
}

private numericalCompositionIsEmpty(number: INumericalComposition): boolean {
return !number.integralPart && !number.fractionalPart;
}

public displayNumber(parsedNumber: INumericalComposition, insertThousandsSeparator = true, matchWholeMask: boolean = false): string {
let displayIntegralPart = parsedNumber.integralPart;
if(insertThousandsSeparator && !!displayIntegralPart) {
Expand Down Expand Up @@ -224,8 +228,9 @@ export class InputMaskNumeric extends InputMaskBase {
return displayText;
}

private getNumberUnmaskedValue(str: string): number {
private getNumberUnmaskedValue(str: string): number | undefined {
const parsedNumber = this.parseNumber(str);
if (this.numericalCompositionIsEmpty(parsedNumber)) return undefined;
return this.convertNumber(parsedNumber);
}

Expand Down
15 changes: 14 additions & 1 deletion tests/mask/mask_number_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,29 @@ QUnit.test("get numeric masked not allow negative value by formated text", funct

QUnit.test("get numeric unmasked valid text", function(assert) {
const maskInstance = new InputMaskNumeric();
assert.ok(maskInstance.getUnmaskedValue("") === undefined);
assert.ok(maskInstance.getUnmaskedValue("0") === 0);
assert.ok(maskInstance.getUnmaskedValue("123") === 123);
assert.ok(maskInstance.getUnmaskedValue("123,456") === 123456);
assert.ok(maskInstance.getUnmaskedValue("123,456.78") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123,456.789") === 123456.78);
assert.ok(maskInstance.getUnmaskedValue("123,456,789,101.12") === 123456789101.12);
});

QUnit.test("get numeric unmasked valid text custom settings", function(assert) {
QUnit.test("get numeric unmasked invalid text", function (assert) {
const maskInstance = new InputMaskNumeric();
assert.ok(maskInstance.getUnmaskedValue(" ") === undefined);
assert.ok(maskInstance.getUnmaskedValue(".") === undefined);
assert.ok(maskInstance.getUnmaskedValue(",") === undefined);
assert.ok(maskInstance.getUnmaskedValue("-") === undefined);
assert.ok(maskInstance.getUnmaskedValue("@") === undefined);
assert.ok(maskInstance.getUnmaskedValue("a") === undefined);
});

QUnit.test("get numeric unmasked valid text custom settings", function (assert) {
const maskInstance = new InputMaskNumeric();
maskInstance.setData({ "decimalSeparator": ",", "thousandsSeparator": "." });
assert.ok(maskInstance.getUnmaskedValue("0") === 0);
assert.ok(maskInstance.getUnmaskedValue("123") === 123);
assert.ok(maskInstance.getUnmaskedValue("123.456") === 123456);
assert.ok(maskInstance.getUnmaskedValue("123.456,78") === 123456.78);
Expand Down

0 comments on commit 1feeffa

Please sign in to comment.