components icon indicating copy to clipboard operation
components copied to clipboard

MdTextareaAutosize: value not rendered with display:none style at initialization of component

Open mitch-berg opened this issue 9 years ago • 9 comments

Bug, feature request, or proposal:

Bug: When the resizing functionality is used on a textarea within an element whose style contains 'display:none' at initialization the value of the textarea is not rendered to the user. When a user enters the field and begins to provide input the existing data value is then rendered in full.

What is the expected behavior?

Pre-populated data values are displayed to the user when the component becomes visible even if the wrapping container is set to 'display:none' at initialization.

What is the current behavior?

When the container is toggled visible to the user the data value is not displayed. The height of the component appears to be set to 0px by MdTextareaAutosize. Once a user begins to enter more data it triggers another resize and the field populates with the data and resizes.

What are the steps to reproduce?

Plunker template: https://plnkr.co/edit/d6s5aOGvS1ZGLNQR7hSJ?p=preview

This example has two wrapper 'div' elements whose display property is manipulated by a button. One is set to 'display:none' at load and demonstrates the problem of data not being visible when you toggle the 'div' that contains the textarea to be visible.

The second 'div' just demonstrates that the data is present on load as expected when the wrapping 'div' does not have 'display:none' set.

What is the use-case or motivation for changing an existing behavior?

Support pre-populated data in components which require resize on load but may not be visible to users at initialization.

Is there anything else we should know?

Material 1 did some state checking and had a watcher out there that resized when the component became visible based on criteria.

https://github.com/angular/material/blob/v1.1.1/src/components/input/input.js#L604

mitch-berg avatar Mar 01 '17 01:03 mitch-berg

Unsurprisingly, this also affects MdTextareaAutosize text areas inside of mdTabs.

While not ideal, a quick fix to overwrite the height: 0 could be:

@ViewChild(MdTextareaAutosize, {read: ViewContainerRef})
myTextArea: ViewContainerRef;

ngAfterViewInit() {
    this.myTextArea.element.nativeElement.style.height = 'auto';
}

rscottfree avatar Mar 06 '17 21:03 rscottfree

I tried this solution but the problem is that when the initial value is multi line, then the default scroll bars appear for textarea.

programmersharp avatar Apr 27 '17 13:04 programmersharp

+1 :+1: This problem occurs especially when mdTextareaAutosize is present.

gedclack avatar Jun 05 '17 06:06 gedclack

@programmersharp I had the same issue regarding displaying the initial value which is multi-line, did you solve this issue?

jsitu avatar Jul 18 '17 18:07 jsitu

@jsitu no, I didn't

programmersharp avatar Jul 19 '17 21:07 programmersharp

@programmersharp, I found a workaround for the issue:

this.yourTextArea.nativeElement.style.height 
= `${this.yourTextArea.nativeElement.scrollHeight}px`;

I have it declared within the ngOnInit lifecycle hook. Kind of hacky but it does the job.

jsitu avatar Jul 19 '17 23:07 jsitu

One suggestion might be using *ngIf rather than display: none.

amcdnl avatar Aug 23 '17 23:08 amcdnl

Closing this since I don't think there's a viable way for the directive to know about visibility changes like this.

jelbourn avatar Apr 28 '20 17:04 jelbourn

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.