Skip to content

Commit a98c886

Browse files
committedOct 30, 2024·
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 cd6d55c commit a98c886

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
@@ -222,6 +222,8 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
222222

223223
/** Shared host configuration for buttons using the `<a>` tag. */
224224
export const MAT_ANCHOR_HOST = {
225+
// Note that this is basically a noop on anchors,
226+
// but it appears that some internal apps depend on it.
225227
'[attr.disabled]': '_getDisabledAttribute()',
226228
'[class.mat-mdc-button-disabled]': 'disabled',
227229
'[class.mat-mdc-button-disabled-interactive]': 'disabledInteractive',
@@ -231,7 +233,7 @@ export const MAT_ANCHOR_HOST = {
231233
// consistency with the `mat-button` applied on native buttons where even
232234
// though they have an index, they're not tabbable.
233235
'[attr.tabindex]': 'disabled && !disabledInteractive ? -1 : tabIndex',
234-
'[attr.aria-disabled]': '_getDisabledAttribute()',
236+
'[attr.aria-disabled]': '_getAriaDisabled()',
235237
// MDC automatically applies the primary theme color to the button, but we want to support
236238
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
237239
// select and style this "theme".
@@ -278,6 +280,9 @@ export class MatAnchorBase extends MatButtonBase implements OnInit, OnDestroy {
278280
};
279281

280282
protected override _getAriaDisabled() {
281-
return this.ariaDisabled == null ? this.disabled : this.ariaDisabled;
283+
if (this.ariaDisabled != null) {
284+
return this.ariaDisabled;
285+
}
286+
return this.disabled || null;
282287
}
283288
}

‎src/material/button/button.spec.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,15 @@ describe('MatButton', () => {
405405
describe('interactive disabled buttons', () => {
406406
let fixture: ComponentFixture<TestApp>;
407407
let button: HTMLButtonElement;
408+
let anchor: HTMLAnchorElement;
408409

409410
beforeEach(() => {
410411
fixture = TestBed.createComponent(TestApp);
411412
fixture.componentInstance.isDisabled = true;
412413
fixture.changeDetectorRef.markForCheck();
413414
fixture.detectChanges();
414-
button = fixture.debugElement.query(By.css('button'))!.nativeElement;
415+
button = fixture.nativeElement.querySelector('button');
416+
anchor = fixture.nativeElement.querySelector('a');
415417
});
416418

417419
it('should set a class when allowing disabled interactivity', () => {
@@ -443,6 +445,29 @@ describe('MatButton', () => {
443445

444446
expect(button.hasAttribute('disabled')).toBe(false);
445447
});
448+
449+
it('should set aria-disabled on anchor when disabledInteractive is enabled', () => {
450+
fixture.componentInstance.isDisabled = false;
451+
fixture.changeDetectorRef.markForCheck();
452+
fixture.detectChanges();
453+
expect(anchor.hasAttribute('aria-disabled')).toBe(false);
454+
expect(anchor.hasAttribute('disabled')).toBe(false);
455+
expect(anchor.classList).not.toContain('mat-mdc-button-disabled-interactive');
456+
457+
fixture.componentInstance.isDisabled = true;
458+
fixture.changeDetectorRef.markForCheck();
459+
fixture.detectChanges();
460+
expect(anchor.getAttribute('aria-disabled')).toBe('true');
461+
expect(anchor.hasAttribute('disabled')).toBe(true);
462+
expect(anchor.classList).not.toContain('mat-mdc-button-disabled-interactive');
463+
464+
fixture.componentInstance.disabledInteractive = true;
465+
fixture.changeDetectorRef.markForCheck();
466+
fixture.detectChanges();
467+
expect(anchor.getAttribute('aria-disabled')).toBe('true');
468+
expect(anchor.hasAttribute('disabled')).toBe(false);
469+
expect(anchor.classList).toContain('mat-mdc-button-disabled-interactive');
470+
});
446471
});
447472
});
448473

0 commit comments

Comments
 (0)
Please sign in to comment.