fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

TextField with multiline and autoAdjustHeight unexpectedly scrolls when line added or deleted

Open amygl opened this issue 5 years ago • 10 comments

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 textFieldScrollOnAdjustHeight

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:

  1. Add on onAdjustInputHeight callback. Consumers who hit this issue can save the container's scrollTop in onChange callback and then reset it in onAdjustInputHeight
  2. Add optional prop that holds scrollableContainerRef then 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 avatar Jan 27 '21 00:01 amygl

@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 avatar Mar 22 '21 12:03 gouttierre

@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.

amygl avatar Mar 22 '21 21:03 amygl

Gentle ping that this issue needs attention.

msft-fluent-ui-bot avatar Apr 12 '21 12:04 msft-fluent-ui-bot

@amygl - thanks for confirming.

@tomi-msft - Would you be able to confirm if this is a regression, or if this behavior is an issue?

gouttierre avatar Apr 12 '21 14:04 gouttierre

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 avatar May 25 '21 01:05 ecraig12345

@ecraig12345 @amygl I'd like to work on the PR if that's okay with you

heptapod42 avatar Jul 27 '21 07:07 heptapod42

@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.

ecraig12345 avatar Jul 28 '21 01:07 ecraig12345

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

heptapod42 avatar Jul 28 '21 12:07 heptapod42

@ecraig12345 I've raised a PR for this issue. Would you be able to review it?

heptapod42 avatar Aug 02 '21 04:08 heptapod42

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.

msft-fluent-ui-bot avatar Sep 27 '22 04:09 msft-fluent-ui-bot