Shweta Nikam
Shweta Nikam

Reputation: 1

Getting missing Form label for mat-list-option for angular material 15 after MDC migration

After angular material upgrade to version 15 and MDC upgrade we are getting an accessibility error from the WAVE tool. We are getting missing-form-label error for every checkbox for the mat-list-option. I have tried adding aria-label ,alt-text, title and even tried adding mat-label inside the <mat-list-option>/</mat-list-option>, nothing has been worked till yet. Can someone please help me with that.

code snippet:

 <mat-selection-list (change)="onChange($event)" (selectionChange)="onChange($event)"
  role="listbox" #list [tabindex]="tabindex" [attr.aria-label]="getAriaLabel()">
  <mat-label hidden aria-hidden="true">MockLabel</mat-label>
  <mat-list-option *ngFor="let option of to.options" value="{{option['value']}}" [selected]="option.selected" 
    checkboxPosition="before" role="option" disabled="{{option['disabled']==true}}" [attr.aria-label]="option['value']"
     title="option['value']" alt-text="option['value']">
    {{option['value']}}
    <mat-label hidden aria-hidden="true">MockLabel</mat-label>
  </mat-list-option>
</mat-selection-list>

Upvotes: 0

Views: 447

Answers (1)

Eliseo
Eliseo

Reputation: 57981

Material angular create the inputs "on-fly" so you can not add attributes to this elements created.

Well, you can use a directive that look for inside the html element mat-option and add attributes.

@Directive({
  selector: '[accessibility]',
  standalone: true,
  exportAs: 'AttribAccess'
})
export class AttribAccessDirective implements AfterViewInit {
  @Input('accessibility') attributes!:any
  constructor(private el:ElementRef){}
  ngAfterViewInit()
  {
    //get the element neccesary
    const element=(this.el.nativeElement.getElementsByTagName('input') ||[null])[0]
    if (element)
    {
      Object.keys(this.attributes).forEach(x=>{
        element.setAttribute(x,this.attributes[x])
      })
      
    }
  }
}

Then you can use the directive

 <mat-list-option *ngFor="let shoe of typesOfShoes" 
    [accessibility]="{'aria-label':'shoe '+shoe,'aria-text':'shoe '+shoe}">
    <span >{{shoe}}</span>
 </mat-list-option>

NOTE That I get the element using javascript getEmlementsByTagName, you can "reach" any element if have a class with getElementsByClassName

The construction

const element=(this.el.nativeElement.get....() || [null])[0]

makes that elements becomes the first element javascript funcitn find or null

A stackblitz

Upvotes: 0

Related Questions