Angular Material Orphaned form label on mat-select - angular-material

I am running into an issue when testing for accessibility problems on an Angular 8 Material app. I am using the Chrome Plugin WAVE Evaluation Tool to check for accessibility https://chrome.google.com/webstore/detail/wave-evaluation-tool/jbbplnpkjmmeebjpijfedlgcdilocofh.
The problem looks to be only with a Mat Select list.
-- Error
image of Mat list with Orphaned Form Lable
WAVE tool Errors
-- Code ( this is what I started with )
<mat-form-field>
<mat-select>
<mat-option *ngFor="let tag of tags" [value]="tags.value">
{{ tag.viewValue }}
</mat-option>
</mat-select>
<mat-label>Resource Tag</mat-label>
</mat-form-field>
-- Code (then after reading that it was missing the form label I changed it to this)
<mat-form-field>
<mat-select id="tags">
<mat-option *ngFor="let tag of tags" [value]="tags.value">
{{ tag.viewValue }}
</mat-option>
</mat-select>
<mat-label for="tags">Resource Tag</mat-label>
</mat-form-field>
Even after that change, the Orphaned Form Lable is still there.
I also removed all Material elements and just created a default select list using HTML elements that worked and did NOT have a missing form label. From this my guess its something going on in the Material elements.

Related

How to change the direction of a dropdown in a mat select?

I'm using a mat select to display a list of items.
<mat-label>{{ 'CLAIMS.PLACEHOLDERS.STATUS' | translate }}</mat-label>
<mat-select (valueChange)="onStatusChange($event)">
<mat-option *ngFor="let option of statusses" [value]="option">
{{ 'STATUS.' + option | translate }}
</mat-option>
</mat-select>
When selecting certain options my dropdown is "stuck" in my navbar:
Is there any way to change the "direction" of the dropdown? So it does down instead of up?
I was able to force my dropdown to open downwards by adding the disableOptionCentering option.
<mat-label>{{ 'CLAIMS.PLACEHOLDERS.CLAIMTYPE' | translate }}</mat-label>
<mat-select (valueChange)="onClaimTypeChange($event)" disableOptionCentering>
<mat-option *ngFor="let option of claimTypes" [value]="option">
{{ option }}
</mat-option>
</mat-select>

Show extra values in mat option when panel is open

<mat-select >
<mat-option *ngFor="let i of List" [value]="i.value">
{{i.viewValue}}<span *ngIf="panelOpen">{{i.viewValue2}}</span>
</mat-option>
</mat-select>
I would like to show additional values in mat option row and hide that additional value after selection. Is There any possibility using panelOpen, Thanks in advance.
If you declare the mat-select as a variable using the # notation, you can then change your ngIf to mySelect.panelOpen where mySelect is the ID you gave the mat-select:
<mat-select #mySelect>
<mat-option *ngFor="let i of List" [value]="i.value">
{{i.viewValue}}
<span *ngIf="mySelect.panelOpen">{{i.viewValue2}}</span>
</mat-option>
</mat-select>
Here's a StackBlitz.

Angular Material's mat-autocomplete input is not getting disabled as expected

I'm trying to disable an Angular Material Autocomplete component. I would have expected to just be able to set disabled on the input, but that does nothing. (I also tried setting disabled on mat-form-field and mat-autocomplete.) Setting matAutocompleteDisabled on input prevented the options from showing, but still allowed typing in the field. Setting readonly on input prevented typing, but it doesn't change the UI, so seems like that will be confusing for the user. Is this a bug, or am I missing something?
Here's the closest I've come so far, using readonly (and disabled isn't working as expected)
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text"
disabled readonly
placeholder="Pick one"
aria-label="Number"
matInput
[formControl]="myControl"
[matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
You should use formControl to set it, something like:
this.formGroupName.controls['myControl'].disable()
Use [attr.disabled]="true". It will set the disabled to true.
You can do css trick for this purpose.
Apply some class to parent tag of input. In you case upper tag is <mat-form-field class="example-full-width"> so i add disable-block class in this. And applied below css.
.disable-block {
pointer-events: none;
opacity: .7;
}
Full code here.
HTML
<form class="example-form">
<mat-form-field class="example-full-width disable-block">
<input type="text" disabled readonly placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of options" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
CSS
.disable-block {
pointer-events: none;
opacity: .7;
}
If you want more lighter text field then you can decrease opacity to .6 or .5 or more want you want.
Hope this will solve you problem.

Hide values from the Angular Material multiple drop down list

How can I hide the values in the drop down box. What I want to achieve is just a placeholder without showing the values selected from multiple. I should keep this ngmodel and selectionChange for other purposes.
Is there any way?
<mat-form-field>
<mat-select
placeholder="Select Columns"
(selectionChange)="columnSelect()"
multiple
[(ngModel)]="selectedColumns"
>
<mat-option
*ngFor="let c of fixedColumns"
[value]="c"
>
{{ c }}
</mat-option>
</mat-select>
</mat-form-field>
I solved the problem by adding mat-select-trigger;
<mat-select
(selectionChange)="columnSelect()"
multiple
[(ngModel)]="selectedColumns"
>
<mat-select-trigger class="trigger">
Select Columns
</mat-select-trigger>
<mat-option
*ngFor="let c of fixedColumns"
[value]="c"
>
{{ c }}
</mat-option>
</mat-select>
``

After upgrading to Angular 6, material 6, select option event fired continuously and browser hangs. How to stop unwanted emit events?

In below code, my country select options are fired so many times that browser stopped responding.
<div [formGroup]="countryForm">
<mat-form-field>
<mat-select formControlName="selectedCountry" class="my-item-text" placeholder="Country">
<mat-option (onSelectionChange)="onCountrySelectionChanged($event)" *ngFor="let myCountry of countries" [value]="myCountry.short" class="my-item-text">{{ myCountry.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
and my component code as below
onCountrySelectionChanged(changeEvent) {
if (changeEvent && changeEvent.isUserInput && this.selectedCountry != changeEvent.source.value) {
this.countrySelected.emit( changeEvent.source.value);
}
}
I tried to restrict by checking if its user change event [isUserInput] and also checking if the value really changed! Now am able to reduce the fire-events and the application works normal.
Is there a better way to use select-option as am now including above logic everywhere am using mat-select component.
The mat-select has an Output property you can bind to called selectionChange, which should fire whenever the option is changed by the user. Try switching up your code to this:
<div [formGroup]="countryForm">
<mat-form-field>
<mat-select (selectionChange)="onCountrySelectionChanged($event)" formControlName="selectedCountry" class="my-item-text" placeholder="Country">
<mat-option *ngFor="let myCountry of countries" [value]="myCountry.short" class="my-item-text">{{ myCountry.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
Thinking about it - what's probably happened is that you've put your onSelectionChange binding on every single mat-option, so when you change the option, you're likely firing that once for each option in your select. Moving it out to the mat-select means it will only fire once.

Resources