Skip to content

Commit eba6ee8

Browse files
authored
fix(material/radio): allow aria attributes to be reset (#32613)
Fixes that it wasn't possible to reset the aria-* attributes for the radio button. Fixes #31790.
1 parent 1f34b94 commit eba6ee8

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

goldens/material/radio/index.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export const MAT_RADIO_GROUP_CONTROL_VALUE_ACCESSOR: any;
3030
// @public (undocumented)
3131
export class MatRadioButton implements OnInit, AfterViewInit, DoCheck, OnDestroy {
3232
constructor(...args: unknown[]);
33-
ariaDescribedby: string;
34-
ariaLabel: string;
35-
ariaLabelledby: string;
33+
ariaDescribedby: string | null;
34+
ariaLabel: string | null;
35+
ariaLabelledby: string | null;
3636
readonly change: EventEmitter<MatRadioChange>;
3737
get checked(): boolean;
3838
set checked(value: boolean);

src/material/radio/radio.spec.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,12 @@ describe('MatRadio', () => {
816816
testComponent.ariaLabel = 'Pineapple';
817817
fixture.changeDetectorRef.markForCheck();
818818
fixture.detectChanges();
819-
820819
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Pineapple');
820+
821+
testComponent.ariaLabel = null;
822+
fixture.changeDetectorRef.markForCheck();
823+
fixture.detectChanges();
824+
expect(fruitRadioNativeInputs[0].hasAttribute('aria-label')).toBe(false);
821825
});
822826

823827
it('should add aria-labelledby attribute to the underlying input element if defined', () => {
@@ -834,8 +838,12 @@ describe('MatRadio', () => {
834838
testComponent.ariaLabelledby = 'uvw';
835839
fixture.changeDetectorRef.markForCheck();
836840
fixture.detectChanges();
837-
838841
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('uvw');
842+
843+
testComponent.ariaLabelledby = null;
844+
fixture.changeDetectorRef.markForCheck();
845+
fixture.detectChanges();
846+
expect(fruitRadioNativeInputs[0].hasAttribute('aria-labelledby')).toBe(false);
839847
});
840848

841849
it('should add aria-describedby attribute to the underlying input element if defined', () => {
@@ -852,8 +860,12 @@ describe('MatRadio', () => {
852860
testComponent.ariaDescribedby = 'uvw';
853861
fixture.changeDetectorRef.markForCheck();
854862
fixture.detectChanges();
855-
856863
expect(fruitRadioNativeInputs[0].getAttribute('aria-describedby')).toBe('uvw');
864+
865+
testComponent.ariaDescribedby = null;
866+
fixture.changeDetectorRef.markForCheck();
867+
fixture.detectChanges();
868+
expect(fruitRadioNativeInputs[0].hasAttribute('aria-describedby')).toBe(false);
857869
});
858870

859871
it('should focus on underlying input element when focus() is called', () => {
@@ -1117,9 +1129,9 @@ class RadiosInsidePreCheckedRadioGroup {}
11171129
imports: [MatRadioModule, FormsModule, ReactiveFormsModule],
11181130
})
11191131
class StandaloneRadioButtons {
1120-
ariaLabel: string = 'Banana';
1121-
ariaLabelledby: string = 'xyz';
1122-
ariaDescribedby: string = 'abc';
1132+
ariaLabel: string | null = 'Banana';
1133+
ariaLabelledby: string | null = 'xyz';
1134+
ariaDescribedby: string | null = 'abc';
11231135
}
11241136

11251137
@Component({

src/material/radio/radio.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,13 +427,13 @@ export class MatRadioButton implements OnInit, AfterViewInit, DoCheck, OnDestroy
427427
@Input() name!: string;
428428

429429
/** Used to set the 'aria-label' attribute on the underlying input element. */
430-
@Input('aria-label') ariaLabel!: string;
430+
@Input('aria-label') ariaLabel!: string | null;
431431

432432
/** The 'aria-labelledby' attribute takes precedence as the element's text alternative. */
433-
@Input('aria-labelledby') ariaLabelledby!: string;
433+
@Input('aria-labelledby') ariaLabelledby!: string | null;
434434

435435
/** The 'aria-describedby' attribute is read after the element's label and field type. */
436-
@Input('aria-describedby') ariaDescribedby!: string;
436+
@Input('aria-describedby') ariaDescribedby!: string | null;
437437

438438
/** Whether ripples are disabled inside the radio button */
439439
@Input({transform: booleanAttribute})

0 commit comments

Comments
 (0)