Skip to content

Commit

Permalink
work for #4842
Browse files Browse the repository at this point in the history
  • Loading branch information
OlgaLarina committed Dec 26, 2023
1 parent e76ce94 commit bafaa3c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/mask/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export class InputMaskBase {
this.addInputEventListener();
}

protected getMaskedValue(mask: any, option?: any): string {
protected getMaskedValue(mask: any): string {
return this.input.value;
}
protected processMaskedValue(mask: any, option?: any): IMaskedValue {
protected processMaskedValue(mask: any): IMaskedValue {
return { text: this.input.value, cursorPosition: this.input.selectionStart };
}
applyValue(mask: any) {
Expand Down
4 changes: 2 additions & 2 deletions src/mask/mask_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export function processValueWithPattern(str: string, pattern: string, prevСurso
}

export class InputMaskPattern extends InputMaskBase {
protected getMaskedValue(mask: string, option?: any): string {
protected getMaskedValue(mask: string): string {
return getMaskedValueByPattern(getUnmaskedValueByPattern(this.input.value, mask, false), mask);
}
protected processMaskedValue(mask: string): IMaskedValue {
return processValueWithPattern(this.input.value, mask, this._prevSelectionStart, this.input.selectionStart);
}

public updateMaskedString(mask: string, option?: any): void {
public updateMaskedString(mask: string): void {
if(!!this.input) {
const result = this.processMaskedValue(mask);
this.input.value = result.text;
Expand Down
13 changes: 7 additions & 6 deletions src/mask/number_mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function getNumberUnmaskedValue(str: string, mask: string, option?: INumb
const thousandsSeparator = option?.thousands || settings.numberOptions.thousands;
const precision = option?.precision || settings.numberOptions.precision;

const number = parseNumber(str.replace(thousandsSeparator, ""), decimalSeparator);
const number = parseNumber(str.split(thousandsSeparator).join(""), decimalSeparator);
const integralPart = number.integralPart ? number.integralPart : 0;
let fractionalPart = number.fractionalPart ? number.fractionalPart : undefined;
if(fractionalPart) {
Expand All @@ -110,16 +110,17 @@ export function getNumberUnmaskedValue(str: string, mask: string, option?: INumb

export class InputMaskNumber extends InputMaskBase {

constructor(input: HTMLInputElement, mask?: INumberMaskOption) {
super(input, mask);
constructor(input: HTMLInputElement, private options?: INumberMaskOption) {
super(input, "9+");
}

protected getMaskedValue(mask: string, option: INumberMaskOption): string {
return getNumberMaskedValue(getNumberUnmaskedValue(this.input.value, mask, option), mask, option);
protected getMaskedValue(mask: string): string {
return getNumberMaskedValue(getNumberUnmaskedValue(this.input.value, mask, this.options), mask, this.options);
}

protected processMaskedValue(mask: string): IMaskedValue {
// return processValueWithPattern(this.input.value, mask, this._prevSelectionStart, this.input.selectionStart);
return { text: this.input.value, cursorPosition: this.input.selectionStart };
const text = getNumberMaskedValue(getNumberUnmaskedValue(this.input.value, mask, this.options), mask, this.options);
return { text: text, cursorPosition: this.input.selectionStart };
}
}
6 changes: 5 additions & 1 deletion src/question_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,11 @@ export class QuestionTextModel extends QuestionTextBase {
}

@property() mask: string;
@property() maskOptions: any;
private updateMaskInstance() {
if (!this.maskInstance) {
if(this.mask === "decimal") {
this.maskInstance = new InputMaskNumber(this.input);
this.maskInstance = new InputMaskNumber(this.input, this.maskOptions);
} else if(this.mask) {
this.maskInstance = new InputMaskPattern(this.input, this.mask);
}
Expand Down Expand Up @@ -646,6 +647,9 @@ Serializer.addClass(
{
name: "mask"
},
{
name: "maskOptions"
},
{
name: "step:number",
dependsOn: "inputType",
Expand Down
1 change: 1 addition & 0 deletions tests/mask/mask_number_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ QUnit.test("get numeric unmasked valid text", function(assert) {
assert.equal(getNumberUnmaskedValue("123,456", customMask), 123456);
assert.equal(getNumberUnmaskedValue("123,456.78", customMask), 123456.78);
assert.equal(getNumberUnmaskedValue("123,456.789", customMask), 123456.78);
assert.equal(getNumberUnmaskedValue("123,456,789,1011.12", customMask), 1234567891011.12);
});

0 comments on commit bafaa3c

Please sign in to comment.