Skip to content

Commit

Permalink
fix(core): don't execute setSelectionRange if element is not focused (
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored Jan 23, 2024
1 parent c7d9859 commit 92f288b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 24 deletions.
9 changes: 7 additions & 2 deletions projects/core/src/lib/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,13 @@ export class Maskito extends MaskHistory {
}

private updateSelectionRange([from, to]: SelectionRange): void {
if (this.element.selectionStart !== from || this.element.selectionEnd !== to) {
this.element.setSelectionRange?.(from, to);
const {element} = this;

if (
element.matches(':focus') &&
(element.selectionStart !== from || element.selectionEnd !== to)
) {
element.setSelectionRange?.(from, to);
}
}

Expand Down
5 changes: 4 additions & 1 deletion projects/core/src/lib/utils/dom/update-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export function maskitoUpdateElement(
const [from, to] = valueOrElementState.selection;

element.value = valueOrElementState.value;
element.setSelectionRange?.(from, to);

if (element.matches(':focus')) {
element.setSelectionRange?.(from, to);
}
}

element.dispatchEvent(
Expand Down
4 changes: 2 additions & 2 deletions projects/kit/src/lib/plugins/caret-guard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MaskitoPlugin} from '@maskito/core';

import {clamp, getFocused} from '../utils';
import {clamp} from '../utils';

export function maskitoCaretGuard(
guard: (
Expand All @@ -17,7 +17,7 @@ export function maskitoCaretGuard(
};

const listener = (): void => {
if (getFocused(document) !== element) {
if (!element.matches(':focus')) {
return;
}

Expand Down
18 changes: 0 additions & 18 deletions projects/kit/src/lib/utils/get-focused.ts

This file was deleted.

1 change: 0 additions & 1 deletion projects/kit/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export * from './date/validate-date-string';
export * from './escape-reg-exp';
export * from './extract-affixes';
export * from './find-common-beginning-substr';
export * from './get-focused';
export * from './identity';
export * from './is-empty';
export * from './pad-with-zeroes-until-valid';

0 comments on commit 92f288b

Please sign in to comment.