TextField with multiline and autoAdjustHeight unexpectedly scrolls when line added or deleted
Environment Information
-
Package version(s): Detected in 7.84.1. Still repros against 7.157.0
-
Browser and OS versions: Edge, Chrome, IE (doesn't seem relevant)
Please provide a reproduction of the bug in a codepen:
https://codepen.io/amygl/pen/MWjNVMe https://fabricweb.z5.web.core.windows.net/oufr/7.157.0/#/examples/textfield
Actual behavior:
- Text field with multiline and autoAdjustHeight.
- When adding a new line, or deleting a line from text field, it scrolls unexpectedly

Expected behavior:
- Shouldn't scroll
Priorities and help requested:
Are you willing to submit a PR to fix? Yes The root cause is in TextField.base.tsx. When the height is set to "", the scroll position on outer container goes to top. And then when it is set to the scroll height, the scroll position on outer container goes to the cursor position.
private _adjustInputHeight(): void {
if (this._textElement.current && this.props.autoAdjustHeight && this.props.multiline) {
const textField = this._textElement.current;
textField.style.height = '';
textField.style.height = textField.scrollHeight + 'px';
}
}
2 different proposed fixes:
- Add on
onAdjustInputHeightcallback. Consumers who hit this issue can save the container's scrollTop inonChangecallback and then reset it inonAdjustInputHeight - Add optional prop that holds
scrollableContainerRefthen update to:
private _adjustInputHeight(): void {
if (this._textElement.current && this.props.autoAdjustHeight && this.props.multiline) {
const containerScrollTop = this.props.scrollableContainerRef?.scrollTop;
const textField = this._textElement.current;
textField.style.height = '';
textField.style.height = textField.scrollHeight + 'px';
if (this.props.scrollableContainerRef){
this.props.scrollableContainerRef.scrollTop = containerScrollTop;
}
}
}
Requested priority: Normal
Products/sites affected: Microsoft Planner
@amygl - Thank you for the feedback. I had a look at this one against our latest version https://developer.microsoft.com/en-us/fluentui#/controls/web/textfield and the issue looks resolved.
@gouttierre, I just tried again and the text still unexpectedly jumps when inserting a newline or deleting a line. https://developer.microsoft.com/en-us/fluentui#/controls/web/textfield
This is on the multi-line text field with auto-adjusting height.
Gentle ping that this issue needs attention.
@amygl - thanks for confirming.
@tomi-msft - Would you be able to confirm if this is a regression, or if this behavior is an issue?
The onAdjustInputHeight approach seems less risky than having TextField directly mess with a parent's DOM. I'm not thrilled about adding even more API surface to TextField, but this does seem like a legitimate use case and at least with an initial search I can't find any other way to handle it (since setting the height to '' is necessary to make the field shrink when text is deleted).
This is not something the team is likely to get to any time soon, but I'd be happy to review a PR if anyone else is interested in implementing it.
@ecraig12345 @amygl I'd like to work on the PR if that's okay with you
@heptapod42 Sure that's fine. As mentioned above I think the onAdjustInputHeight approach is better, and it looks like you'd need to call it after the height has been adjusted.
Working example for using the onAdjustHeight callback to fix the container scroll movement in CodeSandbox https://codesandbox.io/s/textfield-onadjustheight-example-g3kvp?file=/src/index.tsx
@ecraig12345 I've raised a PR for this issue. Would you be able to review it?
Because this issue has not had activity for over 180 days, we're automatically closing it for house-keeping purposes.
Still require assistance? Please, create a new issue with up-to date details.