I have a MatSelectionList
, where I must display multiple lines for each option and have a faMap
icon displayed. If the user clicks on the map, I should display an actual map elsewhere in the application.
The problem is, the material selection list allows the full line to be clicked in order to mark/unmark the checkbox. This way, If I click on the map icon itself, the checkbox change is triggered too, which is undesired. The map click should be a separate action from the checkbox selection.
Please, follow this link for a StackBlitz demo.
I found this solution, but this does not work for me, as I have multiple mat lines and I do not want them to slide into each other. (Also, it seems like a hack?)
I will also paste the code here:
html:
<form [formGroup]="demoSelectForm">
<mat-selection-list (selectionChange)="onSelectionChange($event)" formControlName="selection">
<mat-list-option value="1" checkboxPosition="before">
<mat-icon mat-list-icon>
<fa-icon
[icon]="['far', 'map']"
(click)="onMapClick()"
>
</fa-icon>
</mat-icon>
<h4 mat-line>The option 1</h4>
<p mat-line>Description line 1.1</p>
<p mat-line>Description line 1.2</p>
<p mat-line>Description line 1.3</p>
</mat-list-option>
<mat-list-option value="2" checkboxPosition="before">
<mat-icon mat-list-icon>
<fa-icon
[icon]="['far', 'map']"
(click)="onMapClick()"
>
</fa-icon>
</mat-icon>
<h4 mat-line>The option 2</h4>
<p mat-line>Description line 2.1</p>
<p mat-line>Description line 2.2</p>
<p mat-line>Description line 2.3</p>
</mat-list-option>
<mat-list-option value="3" checkboxPosition="before">
<mat-icon mat-list-icon >
<fa-icon
[icon]="['far', 'map']"
(click)="onMapClick()"
>
</fa-icon>
</mat-icon>
<h4 mat-line>The option 3</h4>
<p mat-line>Description line 3.1</p>
<p mat-line>Description line 3.2</p>
<p mat-line>Description line 3.3</p>
</mat-list-option>
</mat-selection-list>
</form>
{{ demoSelectForm.get('selection').value | json }}
component:
import { Component } from '@angular/core';
import { AbstractControl, FormArray,FormControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
demoSelectForm = new FormGroup({
selection: new FormControl([])
});
onSelectionChange($event) {
// console.log($event);
}
onMapClick(): void {
console.log('MAP GOT CLICKED');
}
}