From 864e50e5b3f37421114202219edff67edbc07251 Mon Sep 17 00:00:00 2001 From: Maksim Ivanov Date: Thu, 1 Aug 2024 11:17:39 +0300 Subject: [PATCH] fix(kit): reuse cursor position after showing password (#8188) --- .../input-password/input-password.component.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/projects/kit/components/input-password/input-password.component.ts b/projects/kit/components/input-password/input-password.component.ts index 8b8d2efb6e2d..00e5dfea14ea 100644 --- a/projects/kit/components/input-password/input-password.component.ts +++ b/projects/kit/components/input-password/input-password.component.ts @@ -31,8 +31,8 @@ import { } from '@taiga-ui/core'; import {TUI_PASSWORD_TEXTS} from '@taiga-ui/kit/tokens'; import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus'; -import {combineLatest, EMPTY, Observable} from 'rxjs'; -import {map, startWith} from 'rxjs/operators'; +import {combineLatest, EMPTY, Observable, timer} from 'rxjs'; +import {map, startWith, takeUntil} from 'rxjs/operators'; import { TUI_INPUT_PASSWORD_OPTIONS, @@ -128,9 +128,23 @@ export class TuiInputPasswordComponent togglePasswordVisibility(): void { this.isPasswordHidden = !this.isPasswordHidden; + + this.reuseCursorPosition(); } protected getFallbackValue(): string { return ''; } + + private reuseCursorPosition(): void { + const {selectionStart: start = 0, selectionEnd: end = 0} = + this.textfield?.nativeFocusableElement ?? {}; + + timer(0) + .pipe(takeUntil(this.destroy$)) + .subscribe( + () => + this.textfield?.nativeFocusableElement?.setSelectionRange(start, end), + ); + } }