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

[Angular] Pattern Input Mask - An invalid value is not always cleared #8519

Merged
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
4 changes: 2 additions & 2 deletions packages/survey-angular-ui/src/questions/text.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</div>
<div *ngIf="model.isReadOnlyRenderDiv()" #contentElement>{{ value }}</div>
<ng-template #input>
<input [style]="model.inputStyle" [class]="model.getControlClass()" [attr.list]="model.dataListId"
<input #inputElement [style]="model.inputStyle" [class]="model.getControlClass()" [attr.list]="model.dataListId"
(change)="model.onChange($event)" [value]="value" [disabled]="model.isDisabledAttr" [type]="model.inputType" [id]="model.inputId" [attr.placeholder]="model.renderedPlaceholder || ''"
(keyup)="model.onKeyUp($event)" (keydown)="model.onKeyDown($event)" (blur)="model.onBlur($event)" (focus)="model.onFocus($event)" (compositionupdate)="model.onCompositionUpdate($event)"
(keyup)="model.onKeyUp($event)" (keydown)="model.onKeyDown($event)" (blur)="blur($event)" (focus)="model.onFocus($event)" (compositionupdate)="model.onCompositionUpdate($event)"
[attr.size] = "model.renderedInputSize" [attr.maxlength]= "model.getMaxLength()" [attr.min]="model.renderedMin" [readonly]="model.isReadOnlyAttr"
[attr.max]="model.renderedMax" [attr.step]="model.renderedStep" [attr.max]="model.renderedMax" [attr.aria-required]="model.a11y_input_ariaRequired"
[attr.aria-label]="model.a11y_input_ariaLabel" [attr.aria-labelledby]="model.a11y_input_ariaLabelledBy" [attr.aria-describedby]="model.a11y_input_ariaDescribedBy"[attr.aria-invalid]="model.a11y_input_ariaInvalid" [attr.aria-errormessage]="model.a11y_input_ariaErrormessage" [attr.autocomplete]="model.autocomplete" #contentElement/>
Expand Down
20 changes: 18 additions & 2 deletions packages/survey-angular-ui/src/questions/text.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from "@angular/core";
import { Component, ElementRef, ViewChild } from "@angular/core";
import { QuestionAngular } from "../question";
import { QuestionTextModel } from "survey-core";
import { Helpers, QuestionTextModel } from "survey-core";
import { AngularComponentFactory } from "../component-factory";

@Component({
Expand All @@ -9,9 +9,25 @@ import { AngularComponentFactory } from "../component-factory";
styleUrls: ["./text.component.scss"]
})
export class TextQuestionComponent extends QuestionAngular<QuestionTextModel> {
@ViewChild("inputElement") inputElementRef!: ElementRef<HTMLDivElement>;

get value(): string {
return this.model.inputValue ?? "";
}

blur(event: any): void {
this.model.onBlur(event);
this.updateInputDomElement();
}

updateInputDomElement(): void {
if (!!this.inputElementRef?.nativeElement) {
const control: any = this.inputElementRef.nativeElement;
if (!Helpers.isTwoValueEquals(this.value, control.value, false, true, false)) {
control.value = this.value;
}
}
}
}

AngularComponentFactory.Instance.registerComponent("text-question", TextQuestionComponent);
37 changes: 37 additions & 0 deletions testCafe/questions/input_mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,41 @@ frameworks.forEach((framework) => {
.expect(getCursor()).eql(6);
});

test("An invalid value is not always cleared", async (t) => {
await initSurvey(framework, {
focusFirstQuestionAutomatic: true,
pages: [
{
name: "page1",
elements: [
{
type: "text",
name: "question1",
maskType: "pattern",
maskSettings: {
pattern: "99999",
},
},
],
},
],
});
const emptyValue = "_____";

await t
.expect(Selector("input").value).eql(emptyValue)

.pressKey("1 2 3 4")
.expect(Selector("input").value).eql("1234_")

.pressKey("tab")
.expect(Selector("input").value).eql(emptyValue)

.click("input")
.pressKey("1 2 3")
.expect(Selector("input").value).eql("123__")

.pressKey("tab")
.expect(Selector("input").value).eql(emptyValue);
});
});