src/app/shared/components/labeled-slide-toggle/labeled-slide-toggle.component.ts
Generic toggle slider component
changeDetection | ChangeDetectionStrategy.OnPush |
selector | ccf-labeled-slide-toggle |
styleUrls | ./labeled-slide-toggle.component.scss |
templateUrl | ./labeled-slide-toggle.component.html |
Properties |
|
Methods |
Inputs |
Outputs |
HostBindings |
Accessors |
constructor(ga: GoogleAnalyticsService)
|
||||||||
Creates an instance of labeled slide toggle component.
Parameters :
|
disabled | |
Type : boolean
|
|
Default value : false
|
|
Whether or not the slider is disabled |
labels | |
Type : [string, string]
|
|
Default value : ['Left', 'Right']
|
|
The two selection options to be toggled |
value | |
Type : string
|
|
Default value : 'Left'
|
|
Input value for toggle slider |
valueChange | |
Type : EventEmitter
|
|
Emits the datatype with the currently selected option |
class |
Type : "ccf-labeled-slide-toggle"
|
Default value : 'ccf-labeled-slide-toggle'
|
HTML class name |
updateToggle | ||||||||
updateToggle(selection: boolean)
|
||||||||
Updates and emits the currently selected option
Parameters :
Returns :
void
|
Readonly clsName |
Type : string
|
Default value : 'ccf-labeled-slide-toggle'
|
Decorators :
@HostBinding('class')
|
HTML class name |
left |
getleft()
|
Determines if left toggle option is selected
Returns :
boolean
|
import { ChangeDetectionStrategy, Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { GoogleAnalyticsService } from 'ngx-google-analytics';
/**
* Generic toggle slider component
*/
@Component({
selector: 'ccf-labeled-slide-toggle',
templateUrl: './labeled-slide-toggle.component.html',
styleUrls: ['./labeled-slide-toggle.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LabeledSlideToggleComponent {
/**
* HTML class name
*/
@HostBinding('class') readonly clsName = 'ccf-labeled-slide-toggle';
/**
* The two selection options to be toggled
*/
@Input() labels: [string, string] = ['Left', 'Right'];
/**
* Input value for toggle slider
*/
@Input() value = 'Left';
/**
* Whether or not the slider is disabled
*/
@Input() disabled = false;
/**
* Emits the datatype with the currently selected option
*/
@Output() readonly valueChange = new EventEmitter<string>();
/**
* Creates an instance of labeled slide toggle component.
*
* @param ga Analytics service
*/
constructor(private readonly ga: GoogleAnalyticsService) {}
/**
* Determines if left toggle option is selected
*/
get left(): boolean {
const { value, labels } = this;
return value !== labels[1];
}
/**
* Updates and emits the currently selected option
*
* @param selection The current toggle state (true=left, false=right)
*/
updateToggle(selection: boolean): void {
this.value = selection ? this.labels[0] : this.labels[1];
this.ga.event('slide_toggle_toggled', 'slide_toggle', this.value);
this.valueChange.emit(this.value);
}
}
<span
class="slide-label"
[class.disabled]="disabled"
[class.highlighted]="left"
(click)="left ? '' : toggle.toggle(); updateToggle(true)"
>
{{ labels[0] }}
</span>
<mat-slide-toggle
[disabled]="disabled"
[checked]="!left"
hideIcon
(change)="updateToggle(!$event.checked)"
class="slider"
#toggle
>
</mat-slide-toggle>
<span
class="slide-label"
[class.disabled]="disabled"
[class.highlighted]="!left"
(click)="!left ? '' : toggle.toggle(); updateToggle(false)"
>
{{ labels[1] }}
</span>
./labeled-slide-toggle.component.scss
:host {
.slide-label {
margin: 0 1rem;
transition: color 0.6s;
cursor: pointer;
}
.disabled {
opacity: 30%;
cursor: not-allowed;
}
}