Skip to content

Commit ea2f29d

Browse files
Jessica Xujesscxu
andauthored
feat(material/expansion): allow focus origin to be optional input in focus method (#20912)
Co-authored-by: Jessica Xu <[email protected]>
1 parent 16576e4 commit ea2f29d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ export class MatExpansionPanelHeader implements AfterViewInit, OnDestroy, Focusa
190190
* @param origin Origin of the action that triggered the focus.
191191
* @docs-private
192192
*/
193-
focus(origin: FocusOrigin = 'program', options?: FocusOptions) {
194-
this._focusMonitor.focusVia(this._element, origin, options);
193+
focus(origin?: FocusOrigin, options?: FocusOptions) {
194+
if (origin) {
195+
this._focusMonitor.focusVia(this._element, origin, options);
196+
} else {
197+
this._element.nativeElement.focus(options);
198+
}
195199
}
196200

197201
ngAfterViewInit() {

src/material/expansion/expansion.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ describe('MatExpansionPanel', () => {
223223
expect(document.activeElement).toBe(header, 'Expected header to be focused.');
224224
}));
225225

226+
it('should not change focus origin if origin not specified', fakeAsync(() => {
227+
const fixture = TestBed.createComponent(PanelWithContent);
228+
fixture.componentInstance.expanded = true;
229+
fixture.detectChanges();
230+
tick(250);
231+
232+
const header = fixture.debugElement.query(By.css('mat-expansion-panel-header'))!;
233+
const headerInstance = header.componentInstance;
234+
235+
headerInstance.focus('mouse');
236+
headerInstance.focus();
237+
fixture.detectChanges();
238+
tick(250);
239+
240+
expect(header.nativeElement.classList).toContain('cdk-focused');
241+
expect(header.nativeElement.classList).toContain('cdk-mouse-focused');
242+
}));
243+
226244
it('should not override the panel margin if it is not inside an accordion', fakeAsync(() => {
227245
const fixture = TestBed.createComponent(PanelWithCustomMargin);
228246
fixture.detectChanges();

0 commit comments

Comments
 (0)