Skip to content

Commit 7a6fb23

Browse files
committed
fix(cdk/text-field): Long multiline textfield focus issue
When we have long multiline textfield and put cursor on almost an end (For eg. on 3rd last line) the scroll moves up to the top of textarea. This fix will cache the `scrollTop` in `resizeToFitContent` method Fixes #20255
1 parent 5a98cc6 commit 7a6fb23

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/cdk/text-field/autosize.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
4747

4848
/** Keep track of the previous textarea value to avoid resizing when the value hasn't changed. */
4949
private _previousValue?: string;
50+
private _currentScrollTop: number;
5051
private _initialHeight: string | undefined;
5152
private readonly _destroyed = new Subject<void>();
5253
private _listenerCleanups: (() => void)[] | undefined;
@@ -303,6 +304,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
303304

304305
this._cacheTextareaLineHeight();
305306
this._cacheTextareaPlaceholderHeight();
307+
this._currentScrollTop = this._textareaElement.scrollTop;
306308

307309
// If we haven't determined the line-height yet, we know we're still hidden and there's no point
308310
// in checking the height of the textarea.
@@ -367,6 +369,7 @@ export class CdkTextareaAutosize implements AfterViewInit, DoCheck, OnDestroy {
367369
// it to receive focus on IE and Edge.
368370
if (!this._destroyed.isStopped && this._hasFocus) {
369371
textarea.setSelectionRange(selectionStart, selectionEnd);
372+
textarea.scrollTop = this._currentScrollTop;
370373
}
371374
}
372375
}

0 commit comments

Comments
 (0)