src/app/shared/components/checkbox/checkbox.component.ts
Creates a labeled group of checkboxes and emits a current list of selections whenever a selection changes.
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-checkbox |
styleUrls | ./checkbox.component.scss |
templateUrl | ./checkbox.component.html |
Methods |
Inputs |
Outputs |
constructor(ga: GoogleAnalyticsService)
|
||||||||
Creates an instance of checkbox component.
Parameters :
|
columns | |
Type : number
|
|
Default value : 3
|
|
Number of columns |
label | |
Type : string
|
|
The label that describes the overall question the checkbox is asking |
options | |
Type : string[]
|
|
Used to generate the individual checkboxes and their individual labels |
selection | |
Type : string[]
|
|
Default value : []
|
|
A list of the checkboxes the user has checked. To be updated any time a checkbox changes. |
selectionChange | |
Type : EventEmitter
|
|
Any time a checkbox changes we emit that value so the parent component has that information |
filterOnChange | ||||||||||||
filterOnChange(event: MatCheckboxChange, option: string)
|
||||||||||||
This method captures checkbox events and decides whether to add or remove a filter selection based on the checked property
Parameters :
Returns :
void
|
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { MatCheckboxChange } from '@angular/material/checkbox';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
/**
* Creates a labeled group of checkboxes and emits a current list of selections whenever a selection changes.
*/
@Component({
selector: 'ccf-checkbox',
templateUrl: './checkbox.component.html',
styleUrls: ['./checkbox.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckboxComponent {
/**
* The label that describes the overall question the checkbox is asking
*/
@Input() label!: string;
/**
* Used to generate the individual checkboxes and their individual labels
*/
@Input() options!: string[];
/**
* A list of the checkboxes the user has checked. To be updated any time a checkbox changes.
*/
@Input() selection: string[] = [];
/**
* Number of columns
*/
@Input() columns = 3;
/**
* Any time a checkbox changes we emit that value so the parent component has that information
*/
@Output() readonly selectionChange = new EventEmitter<string[]>();
/**
* Creates an instance of checkbox component.
*
* @param ga Analytics service
*/
constructor(private readonly ga: GoogleAnalyticsService) {}
/**
* This method captures checkbox events and decides whether to add or remove a filter selection based on the checked property
*
* @param event Event object from the checkbox that contains the boolean property 'checked'
* @param option Tells us which option was checked or unchecked
*/
filterOnChange(event: MatCheckboxChange, option: string): void {
const checked = event.checked;
if (checked) {
this.selection = [...this.selection, option];
this.ga.event('filter_added', 'filter_checkbox', option);
} else {
this.selection = this.selection.filter((selection) => selection !== option);
this.ga.event('filter_removed', 'filter_checkbox', option);
}
this.selectionChange.emit(this.selection);
}
}
<div class="filter-container">
<div class="filter-label">{{ label }}</div>
<div class="options-container" [class.three]="columns === 3" [class.five]="columns === 5">
<div *ngFor="let option of options" class="option">
<mat-checkbox [checked]="selection?.includes(option)" (change)="filterOnChange($event, option)">
{{ option }}
</mat-checkbox>
</div>
</div>
</div>
./checkbox.component.scss
.filter-label {
margin-bottom: 0.5rem;
font-weight: bold;
text-wrap: nowrap;
}
.options-container {
display: flex;
flex-wrap: wrap;
.option {
display: flex;
height: 2rem;
align-items: center;
}
&.three {
.option {
width: 14.375rem;
}
}
&.five {
.option {
width: 20%;
}
}
}