Skip to content

fix(expansion-panel): toggle not being updated when set programmatically #5650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/expansion/accordion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export class AccordionItem implements OnDestroy {
@Output() destroyed = new EventEmitter<void>();
/** The unique MdAccordionChild id. */
readonly id = `cdk-accordion-child-${nextId++}`;

/** Whether the MdAccordionChild is expanded. */
@Input() get expanded(): boolean { return this._expanded; }
@Input()
get expanded(): boolean { return this._expanded; }
set expanded(expanded: boolean) {
// Only emit events and update the internal value if the value changes.
if (this._expanded !== expanded) {
Expand Down
30 changes: 28 additions & 2 deletions src/lib/expansion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
Host,
ViewEncapsulation,
ChangeDetectionStrategy,
ChangeDetectorRef,
OnDestroy,
} from '@angular/core';
import {
trigger,
Expand All @@ -22,6 +24,9 @@ import {
} from '@angular/animations';
import {SPACE, ENTER} from '../core/keyboard/keycodes';
import {MdExpansionPanel, EXPANSION_PANEL_ANIMATION_TIMING} from './expansion-panel';
import {filter} from '../core/rxjs/index';
import {merge} from 'rxjs/observable/merge';
import {Subscription} from 'rxjs/Subscription';


/**
Expand Down Expand Up @@ -62,8 +67,22 @@ import {MdExpansionPanel, EXPANSION_PANEL_ANIMATION_TIMING} from './expansion-pa
]),
],
})
export class MdExpansionPanelHeader {
constructor(@Host() public panel: MdExpansionPanel) {}
export class MdExpansionPanelHeader implements OnDestroy {
private _parentChangeSubscription: Subscription | null = null;

constructor(
@Host() public panel: MdExpansionPanel,
private _changeDetectorRef: ChangeDetectorRef) {

// Since the toggle state depends on an @Input on the panel, we
// need to subscribe and trigger change detection manually.
this._parentChangeSubscription = merge(
panel.opened,
panel.closed,
filter.call(panel._inputChanges, changes => !!changes.hideToggle)
)
.subscribe(() => this._changeDetectorRef.markForCheck());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add a unit test for this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can give it a try, but I feel like the manual change detection might cause it not to fail as expected.

}

/** Toggles the expanded state of the panel. */
_toggle(): void {
Expand Down Expand Up @@ -103,6 +122,13 @@ export class MdExpansionPanelHeader {
return;
}
}

ngOnDestroy() {
if (this._parentChangeSubscription) {
this._parentChangeSubscription.unsubscribe();
this._parentChangeSubscription = null;
}
}
}

/**
Expand Down
17 changes: 16 additions & 1 deletion src/lib/expansion/expansion-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
forwardRef,
ChangeDetectionStrategy,
ChangeDetectorRef,
SimpleChanges,
OnChanges,
OnDestroy,
} from '@angular/core';
import {
trigger,
Expand All @@ -27,6 +30,7 @@ import {
import {MdAccordion, MdAccordionDisplayMode} from './accordion';
import {AccordionItem} from './accordion-item';
import {UniqueSelectionDispatcher} from '../core';
import {Subject} from 'rxjs/Subject';


/** MdExpansionPanel's states. */
Expand Down Expand Up @@ -72,10 +76,13 @@ export const EXPANSION_PANEL_ANIMATION_TIMING = '225ms cubic-bezier(0.4,0.0,0.2,
]),
],
})
export class MdExpansionPanel extends AccordionItem {
export class MdExpansionPanel extends AccordionItem implements OnChanges, OnDestroy {
/** Whether the toggle indicator should be hidden. */
@Input() hideToggle: boolean = false;

/** Stream that emits for changes in `@Input` properties. */
_inputChanges = new Subject<SimpleChanges>();

constructor(@Optional() @Host() accordion: MdAccordion,
_changeDetectorRef: ChangeDetectorRef,
_uniqueSelectionDispatcher: UniqueSelectionDispatcher) {
Expand Down Expand Up @@ -104,6 +111,14 @@ export class MdExpansionPanel extends AccordionItem {
_getExpandedState(): MdExpansionPanelState {
return this.expanded ? 'expanded' : 'collapsed';
}

ngOnChanges(changes: SimpleChanges) {
this._inputChanges.next(changes);
}

ngOnDestroy() {
this._inputChanges.complete();
}
}

@Directive({
Expand Down
36 changes: 36 additions & 0 deletions src/lib/expansion/expansion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,47 @@ describe('MdExpansionPanel', () => {
expect(styles.marginLeft).toBe('37px');
expect(styles.marginRight).toBe('37px');
}));

it('should be able to hide the toggle', () => {
const fixture = TestBed.createComponent(PanelWithContent);
const header = fixture.debugElement.query(By.css('.mat-expansion-panel-header')).nativeElement;

fixture.detectChanges();

expect(header.querySelector('.mat-expansion-indicator'))
.toBeTruthy('Expected indicator to be shown.');

fixture.componentInstance.hideToggle = true;
fixture.detectChanges();

expect(header.querySelector('.mat-expansion-indicator'))
.toBeFalsy('Expected indicator to be hidden.');
});

it('should update the indicator rotation when the expanded state is toggled programmatically',
fakeAsync(() => {
const fixture = TestBed.createComponent(PanelWithContent);

fixture.detectChanges();
tick(250);

const arrow = fixture.debugElement.query(By.css('.mat-expansion-indicator')).nativeElement;

expect(arrow.style.transform).toBe('rotate(0deg)', 'Expected no rotation.');

fixture.componentInstance.expanded = true;
fixture.detectChanges();
tick(250);

expect(arrow.style.transform).toBe('rotate(180deg)', 'Expected 180 degree rotation.');
}));
});


@Component({
template: `
<md-expansion-panel [expanded]="expanded"
[hideToggle]="hideToggle"
(opened)="openCallback()"
(closed)="closeCallback()">
<md-expansion-panel-header>Panel Title</md-expansion-panel-header>
Expand All @@ -118,6 +153,7 @@ describe('MdExpansionPanel', () => {
})
class PanelWithContent {
expanded: boolean = false;
hideToggle: boolean = false;
openCallback = jasmine.createSpy('openCallback');
closeCallback = jasmine.createSpy('closeCallback');
}
Expand Down