Skip to content

Commit 8bd6cf9

Browse files
crisbetoyifange
authored andcommitted
fix(expansion): unable to toggle disabled panel via methods (angular#18181)
In our expansion panel readme we state that a disabled panel can still be toggled and this is partially true, because it works only for toggling through the `expanded` property, but not via the `open`, `close` and `toggle` methods since they're inherited from the CDK `AccordionItem`. These changes override the methods so that we can allow them to work on disabled panels. We can't make this change safely for the CDK accordion item, because it doesn't have a panel header which means that we have to assume that all calls to the methods are user-generated. Fixes angular#18171.
1 parent 2baff6e commit 8bd6cf9

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/material/expansion/expansion-panel-header.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
148148

149149
/** Toggles the expanded state of the panel. */
150150
_toggle(): void {
151-
this.panel.toggle();
151+
if (!this.disabled) {
152+
this.panel.toggle();
153+
}
152154
}
153155

154156
/** Gets whether the panel is expanded. */

src/material/expansion/expansion-panel.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ export class MatExpansionPanel extends CdkAccordionItem implements AfterContentI
192192
return this.expanded ? 'expanded' : 'collapsed';
193193
}
194194

195+
/** Toggles the expanded state of the expansion panel. */
196+
toggle(): void {
197+
this.expanded = !this.expanded;
198+
}
199+
200+
/** Sets the expanded state of the expansion panel to false. */
201+
close(): void {
202+
this.expanded = false;
203+
}
204+
205+
/** Sets the expanded state of the expansion panel to true. */
206+
open(): void {
207+
this.expanded = true;
208+
}
209+
195210
ngAfterContentInit() {
196211
if (this._lazyContent) {
197212
// Render the content as soon as the panel becomes open.

src/material/expansion/expansion.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,52 @@ describe('MatExpansionPanel', () => {
403403
expect(header.classList).toContain('mat-expanded');
404404
});
405405

406+
it('should be able to toggle a disabled expansion panel programmatically via the ' +
407+
'open/close methods', () => {
408+
const panelInstance = fixture.componentInstance.panel;
409+
410+
expect(panelInstance.expanded).toBe(false);
411+
expect(header.classList).not.toContain('mat-expanded');
412+
413+
fixture.componentInstance.disabled = true;
414+
fixture.detectChanges();
415+
416+
panelInstance.open();
417+
fixture.detectChanges();
418+
419+
expect(panelInstance.expanded).toBe(true);
420+
expect(header.classList).toContain('mat-expanded');
421+
422+
panelInstance.close();
423+
fixture.detectChanges();
424+
425+
expect(panelInstance.expanded).toBe(false);
426+
expect(header.classList).not.toContain('mat-expanded');
427+
});
428+
429+
it('should be able to toggle a disabled expansion panel programmatically via the ' +
430+
'toggle method', () => {
431+
const panelInstance = fixture.componentInstance.panel;
432+
433+
expect(panelInstance.expanded).toBe(false);
434+
expect(header.classList).not.toContain('mat-expanded');
435+
436+
fixture.componentInstance.disabled = true;
437+
fixture.detectChanges();
438+
439+
panelInstance.toggle();
440+
fixture.detectChanges();
441+
442+
expect(panelInstance.expanded).toBe(true);
443+
expect(header.classList).toContain('mat-expanded');
444+
445+
panelInstance.toggle();
446+
fixture.detectChanges();
447+
448+
expect(panelInstance.expanded).toBe(false);
449+
expect(header.classList).not.toContain('mat-expanded');
450+
});
451+
406452
});
407453
});
408454

tools/public_api_guard/material/expansion.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ export declare class MatExpansionPanel extends CdkAccordionItem implements After
6161
_containsFocus(): boolean;
6262
_getExpandedState(): MatExpansionPanelState;
6363
_hasSpacing(): boolean;
64+
close(): void;
6465
ngAfterContentInit(): void;
6566
ngOnChanges(changes: SimpleChanges): void;
6667
ngOnDestroy(): void;
68+
open(): void;
69+
toggle(): void;
6770
static ngAcceptInputType_disabled: BooleanInput;
6871
static ngAcceptInputType_expanded: BooleanInput;
6972
static ngAcceptInputType_hideToggle: BooleanInput;

0 commit comments

Comments
 (0)