Skip to content

Commit 95ef66c

Browse files
committed
fix(material/button): anchor not handling disabledInteractive correctly (#29938)
Fixes that the anchor-based `MatButton` wasn't setting `aria-disabled` when `disabledInteractive` is enabled. (cherry picked from commit 6b3a371)
1 parent ea976f9 commit 95ef66c

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/material/button/button-base.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
215215

216216
/** Shared host configuration for buttons using the `<a>` tag. */
217217
export const MAT_ANCHOR_HOST = {
218+
// Note that this is basically a noop on anchors,
219+
// but it appears that some internal apps depend on it.
218220
'[attr.disabled]': '_getDisabledAttribute()',
219221
'[class.mat-mdc-button-disabled]': 'disabled',
220222
'[class.mat-mdc-button-disabled-interactive]': 'disabledInteractive',
@@ -224,7 +226,7 @@ export const MAT_ANCHOR_HOST = {
224226
// consistency with the `mat-button` applied on native buttons where even
225227
// though they have an index, they're not tabbable.
226228
'[attr.tabindex]': 'disabled && !disabledInteractive ? -1 : tabIndex',
227-
'[attr.aria-disabled]': '_getDisabledAttribute()',
229+
'[attr.aria-disabled]': '_getAriaDisabled()',
228230
// MDC automatically applies the primary theme color to the button, but we want to support
229231
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
230232
// select and style this "theme".
@@ -267,6 +269,9 @@ export class MatAnchorBase extends MatButtonBase implements OnInit, OnDestroy {
267269
};
268270

269271
protected override _getAriaDisabled() {
270-
return this.ariaDisabled == null ? this.disabled : this.ariaDisabled;
272+
if (this.ariaDisabled != null) {
273+
return this.ariaDisabled;
274+
}
275+
return this.disabled || null;
271276
}
272277
}

src/material/button/button.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,15 @@ describe('MatButton', () => {
316316
describe('interactive disabled buttons', () => {
317317
let fixture: ComponentFixture<TestApp>;
318318
let button: HTMLButtonElement;
319+
let anchor: HTMLAnchorElement;
319320

320321
beforeEach(() => {
321322
fixture = TestBed.createComponent(TestApp);
322323
fixture.componentInstance.isDisabled = true;
323324
fixture.changeDetectorRef.markForCheck();
324325
fixture.detectChanges();
325-
button = fixture.debugElement.query(By.css('button'))!.nativeElement;
326+
button = fixture.nativeElement.querySelector('button');
327+
anchor = fixture.nativeElement.querySelector('a');
326328
});
327329

328330
it('should set a class when allowing disabled interactivity', () => {
@@ -354,6 +356,29 @@ describe('MatButton', () => {
354356

355357
expect(button.hasAttribute('disabled')).toBe(false);
356358
});
359+
360+
it('should set aria-disabled on anchor when disabledInteractive is enabled', () => {
361+
fixture.componentInstance.isDisabled = false;
362+
fixture.changeDetectorRef.markForCheck();
363+
fixture.detectChanges();
364+
expect(anchor.hasAttribute('aria-disabled')).toBe(false);
365+
expect(anchor.hasAttribute('disabled')).toBe(false);
366+
expect(anchor.classList).not.toContain('mat-mdc-button-disabled-interactive');
367+
368+
fixture.componentInstance.isDisabled = true;
369+
fixture.changeDetectorRef.markForCheck();
370+
fixture.detectChanges();
371+
expect(anchor.getAttribute('aria-disabled')).toBe('true');
372+
expect(anchor.hasAttribute('disabled')).toBe(true);
373+
expect(anchor.classList).not.toContain('mat-mdc-button-disabled-interactive');
374+
375+
fixture.componentInstance.disabledInteractive = true;
376+
fixture.changeDetectorRef.markForCheck();
377+
fixture.detectChanges();
378+
expect(anchor.getAttribute('aria-disabled')).toBe('true');
379+
expect(anchor.hasAttribute('disabled')).toBe(false);
380+
expect(anchor.classList).toContain('mat-mdc-button-disabled-interactive');
381+
});
357382
});
358383
});
359384

0 commit comments

Comments
 (0)