File

src/app/shared/components/labeled-slide-toggle/labeled-slide-toggle.component.ts

Description

Generic toggle slider component

Metadata

Index

Properties
Methods
Inputs
Outputs
HostBindings
Accessors

Constructor

constructor(ga: GoogleAnalyticsService)

Creates an instance of labeled slide toggle component.

Parameters :
Name Type Optional Description
ga GoogleAnalyticsService No

Analytics service

Inputs

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

Outputs

valueChange
Type : EventEmitter

Emits the datatype with the currently selected option

HostBindings

class
Type : "ccf-labeled-slide-toggle"
Default value : 'ccf-labeled-slide-toggle'

HTML class name

Methods

updateToggle
updateToggle(selection: boolean)

Updates and emits the currently selected option

Parameters :
Name Type Optional Description
selection boolean No

The current toggle state (true=left, false=right)

Returns : void

Properties

Readonly clsName
Type : string
Default value : 'ccf-labeled-slide-toggle'
Decorators :
@HostBinding('class')

HTML class name

Accessors

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;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""