[Tabs] Hidden tabs don't render expansion panels correctly
Bug, feature request, or proposal:
Bug.
What is the expected behavior?
A <md-expansion-panel [expanded]="false"> should not be expanded until explicitally opened.
When inside a hidden tab, .mat-expansion-panel-content should have height : 0px
What is the current behavior?
Panel is collapsed but panetl's content is visible. Problem occurs only if the accordion is in a hidden tab of a tab-group.
What are the steps to reproduce?
http://plnkr.co/edit/q6kXPH?p=preview
<md-tab label="Working">
<md-accordion>
<md-expansion-panel [expanded]="false">
<md-expansion-panel-header>
You should see me
</md-expansion-panel-header>
<p>Yous should not see me</p>
</md-expansion-panel>
</md-accordion>
</md-tab>
<md-tab label="Not working">
<md-accordion>
<md-expansion-panel [expanded]="false">
<md-expansion-panel-header>
You should see me
</md-expansion-panel-header>
<p>Yous should not see me</p>
</md-expansion-panel>
</md-accordion>
</md-tab>
</md-tab-group>
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Material2 master Angular 4.2.3
It seems to be working correctly here: http://plnkr.co/edit/q6kXPH?p=preview
correct. It's also working in another page of mine. Currently trying to isolate the pb.
Ok, i edit the issue : the problem occurs only if the element is in a hidden tab. Thanks @julianobrasil
OK. Updated the plunker to reproduce it: http://plnkr.co/edit/q6kXPH?p=preview
Put it in the issue.
This actually happens to anything that contains an animation when set inside a hidden md-tab.
This seems to be caused by angular 4.2 changes. I use Teradata Covalent expansion panels in my app which have the same problem since the new angular version: https://gitter.im/Teradata/covalent?at=593e58f06549436c7d3c8360
:wink: A very unacceptable workaround (but useful if you can't wait for the next release, possibly without this bug) is using a *ngIf and just show the accordion/expansion panel when it's needed (if it's needed when the page is loaded, put a variable in a setTimeout to trigger *ngIf in about 4 to 5 seconds after the tab-group has been rendered, or use the tab change event). In this case the expansion is shown in its expected state (collapsed or expanded).
Hey! that bug is really annoying, is there any hope to see it's fix?) Maybe applying @julianobrasil solution with *ngIf=isActive on tabs content?
@lebeker, I've been using something like that in a project: http://plnkr.co/edit/YAi6Xb?p=preview
I tried isActive (*ngIf="mdTab.isActive" where mdTab is a reference template variable), but it causes a "changed after checked" error.
Hello! @julianobrasil, following @lebeker advice that's working for me as a workaround:
<md-tab-group [(selectedIndex)]="selectedTabIndex">
<md-tab label="First">
<div>...</div>
</md-tab>
<md-tab label="Second">
<div *ngIf="selectedTabIndex == 1">...</div>
<md-tab>
<md-tab label="Third">
<div *ngIf="selectedTabIndex == 2">...</div>
<md-tab>
</md-tab-group>
@lula worked for me, thanks!
@lula Can you show your component.ts file?
@michalmw there's nothing to show in my component.ts concerning with my workaround. Variable selectedTabIndex is directly used in the view template. Angular should take care of instantiating it. Should you need to start with another tab than the first, then you can also add selectedTabIndex to component.ts and set the tab index you prefer. Let me know if this is what you wanted to know or you needed something else :)
Just a little tweak of @lula's solution - you don't need to actually store the selectedIndex. Instead you could just reference the md-tab-group:
<md-tab-group #myTabGroup>
<md-tab label="First">
<div>First</div>
</md-tab>
<md-tab label="Second">
<md-accordion *ngIf="myTabGroup.selectedIndex == 1">
<md-expansion-panel *ngFor="let i of [1,2,3];">
<md-expansion-panel-header>
<md-panel-title>Test {{ i }}</md-panel-title>
</md-expansion-panel-header>
<p>Testing content {{ i }}</p>
</md-expansion-panel>
</md-accordion>
</md-tab>
<md-tab label="Third">
<div>Third</div>
</md-tab>
</md-tab-group>
Or use css ? It worked for me ! :)
div.mat-expansion-panel-content:not(.mat-expanded) {
height: 0px !important;
visibility: hidden !important;
}
.mat-expansion-panel-header {
height: 48px;
}
The CSS solution by @bastienlemaitre is not working for me in the content of hidden tabs.
Tried the solution of mdTab.isActive based on @julianobrasil suggestion with ngIf but resulting in
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.
I have to generate tabs dynamically, so storing selected index won't work. Here is what I am trying.
<mat-tab-group #tabGroup>
<mat-tab #currTab *ngFor="let label of labels">
<ng-template mat-tab-label>
<span>{{label}}</span>
</ng-template>
<div *ngIf="currTab.isActive" >
<mat-expansion-panel [expanded]="false" *ngFor="let panel of panels">
<mat-expansion-panel-header>
<mat-panel-title>{{panel.title}}</mat-panel-title>
<mat-panel-description>{{panel.desc}}</mat-panel-description>
</mat-expansion-panel-header>
<div>{{panel.content}}</div>
</mat-expansion-panel>
</div>
</mat-tab>
</mat-tab-group>
Is there a better way to operate on this one?
@rayabhagis, try @lula solution above (https://github.com/angular/material2/issues/5269#issuecomment-326692430). It's simple and dosen't throw the "changed after checked" error.
Thanks, I updated my code to follow similar approach by comparing index of *ngFor on tabs with tab-group's selectedIndex and there is no "change after checked" error.
@rayabhagis, did you use this into your component ?
encapsulation: ViewEncapsulation.None
Updated CSS:
div.mat-expansion-panel-content:not(.mat-expanded) {
height: 0px;
visibility: hidden;
}
.mat-expansion-panel-header:not(.mat-expanded) {
height: 48px;
}
.mat-expansion-panel-header {
height: 64px;
.mat-content {
font-weight: 100;
font-size: 18px;
}
&.mat-expanded {
.mat-expansion-indicator {
transform: rotate(180deg);
}
}
}
Apparently the problem is on trigger animation. The trigger isn't call when component is inside a false *ngIf. I don't know if this is a problem in Material or Angular.