Skip to content

Commit

Permalink
#8057 Input mask does not start from the first symbol when focus is s…
Browse files Browse the repository at this point in the history
…et at the end

Fixes #8057
  • Loading branch information
novikov82 committed Apr 5, 2024
1 parent 4ac4d90 commit 7599faa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/mask/input_element_adapter.ts
Expand Up @@ -25,6 +25,13 @@ export class InputElementAdapter {
}
}

clickHandler = (event: any) => {
if (this.inputElement.value == this.inputMaskInstance.getMaskedValue("")) {
this.inputElement.setSelectionRange(0, 0);
event.preventDefault();
}
};

beforeInputHandler = (event: any) => {
const args = this.createArgs(event);
const result = this.inputMaskInstance.processInput(args);
Expand Down Expand Up @@ -60,11 +67,13 @@ export class InputElementAdapter {
public addInputEventListener(): void {
if (!!this.inputElement) {
this.inputElement.addEventListener("beforeinput", this.beforeInputHandler);
this.inputElement.addEventListener("click", this.clickHandler);
}
}
public removeInputEventListener(): void {
if (!!this.inputElement) {
this.inputElement.removeEventListener("beforeinput", this.beforeInputHandler);
this.inputElement.removeEventListener("click", this.clickHandler);
}
}
public dispose(): void {
Expand Down
37 changes: 37 additions & 0 deletions testCafe/questions/input_mask.ts
Expand Up @@ -29,4 +29,41 @@ frameworks.forEach((framework) => {
name: "1234",
});
});

test("Cursor position on click", async (t) => {
await initSurvey(framework, {
focusFirstQuestionAutomatic: true,
questions: [
{
name: "name",
type: "text",
maskType: "pattern",
maskSettings: {
pattern: "+99-99"
}
},
{
name: "name1",
type: "text",
defaultValue: "1234",
maskType: "pattern",
maskSettings: {
pattern: "+99-99"
}
}]
});

var getCursor =
ClientFunction(() => {
return (document.activeElement as HTMLInputElement).selectionStart;
});

await t
.click(Selector(".sv_q_text_root").nth(0))
.expect(getCursor()).eql(0);
await t
.click(Selector(".sv_q_text_root").nth(1))
.expect(getCursor()).eql(6);
});

});

0 comments on commit 7599faa

Please sign in to comment.