Skip to content
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
8 changes: 6 additions & 2 deletions src/material/expansion/expansion-panel-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,12 @@ export class MatExpansionPanelHeader implements AfterViewInit, OnDestroy, Focusa
* @param origin Origin of the action that triggered the focus.
* @docs-private
*/
focus(origin: FocusOrigin = 'program', options?: FocusOptions) {
this._focusMonitor.focusVia(this._element, origin, options);
focus(origin?: FocusOrigin, options?: FocusOptions) {
if (origin) {
this._focusMonitor.focusVia(this._element, origin, options);
} else {
this._element.nativeElement.focus(options);
}
}

ngAfterViewInit() {
Expand Down
18 changes: 18 additions & 0 deletions src/material/expansion/expansion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,24 @@ describe('MatExpansionPanel', () => {
expect(document.activeElement).toBe(header, 'Expected header to be focused.');
}));

it('should not change focus origin if origin not specified', fakeAsync(() => {
const fixture = TestBed.createComponent(PanelWithContent);
fixture.componentInstance.expanded = true;
fixture.detectChanges();
tick(250);

const header = fixture.debugElement.query(By.css('mat-expansion-panel-header'))!;
const headerInstance = header.componentInstance;

headerInstance.focus('mouse');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This isn't the best test in my opinion. I attempted to focus the button with origin mouse but this requires making this button a mat-button in the component template below.

    const button = fixture.debugElement.query(By.css('button'))!;
    const buttonInstance = button.componentInstance as MatButton;

    buttonInstance.focus('mouse');

This requires importing MatButton as well, which I was having trouble doing and was not sure if we should avoid importing other modules. If this is a better, however, can change it to this approach (assuming it works).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the way you have it here is fine

headerInstance.focus();
fixture.detectChanges();
tick(250);

expect(header.nativeElement.classList).toContain('cdk-focused');
expect(header.nativeElement.classList).toContain('cdk-mouse-focused');
}));

it('should not override the panel margin if it is not inside an accordion', fakeAsync(() => {
const fixture = TestBed.createComponent(PanelWithCustomMargin);
fixture.detectChanges();
Expand Down