File

libs/ui/toggle/src/lib/toggle/toggle.component.ts

Description

The is a toggle component

Extends

TsReactiveFormBaseComponent

Example

<ts-toggle
  arialLabel="Disable my thing"
  [formControl]="yourHelperToGetFormControl('thingIsDisabled')"
  [isDisabled]="true"
  [isRequired]="true"
  labelPosition="before"
  name="My toggle"
  theme="accent"
  (selectionChange)="myMethod($event)"
>My Toggle!</ts-toggle>

<example-url>https://getterminus.github.io/ui-demos-release/components/toggle</example-url>

Metadata

changeDetection ChangeDetectionStrategy.OnPush
encapsulation ViewEncapsulation.None
exportAs tsToggle
host {
}
providers ControlValueAccessorProviderFactory<TsToggleComponent>(TsToggleComponent)
selector ts-toggle
styleUrls ./toggle.component.scss
templateUrl ./toggle.component.html

Index

Properties
Methods
Inputs
Outputs
Accessors

Inputs

ariaLabel
Type : string | undefined

Define the aria label

isChecked
Type : boolean

Allow the checked state to be set

isDisabled
Default value : false

Define if the toggle should be disabled

isRequired
Default value : false

Define if the toggle is required

labelPosition
Type : "before" | "after"
Default value : 'after'

Define the position of the label

name
Default value : 'toggle'

Define the input name for the toggle

theme
Type : TsStyleThemeTypes
Default value : 'primary'

Define the theme

formControl
Type : FormControl
Default value : new FormControl()

Define the form control to get access to validators

Outputs

selectionChange
Type : EventEmitter<TsSlideToggleChange>

Emit an event each time the toggle value changes

Methods

Public onBlur
onBlur()

Set touched on blur

Returns : void
Protected registerOnChange
registerOnChange(fn: (_: any) => void)

Register onChange callback (from ControlValueAccessor interface)

Parameters :
Name Type Optional
fn function No
Returns : void
Protected registerOnTouched
registerOnTouched(fn: () => void)

Register onTouched callback (from ControlValueAccessor interface)

Parameters :
Name Type Optional
fn function No
Returns : void
Protected writeValue
writeValue(value: any)

Write value to inner value (from ControlValueAccessor interface)

Parameters :
Name Type Optional
value any No
Returns : void

Properties

Protected innerValue
Type : any
Default value : ''

Define the internal data model

Protected onChangeCallback
Type : function
Default value : noop

Define placeholder for callback (provided later by the control value accessor)

Protected onTouchedCallback
Type : function
Default value : noop

Define placeholder for callback (provided later by the control value accessor)

Accessors

isChecked
getisChecked()
setisChecked(value: boolean)

Allow the checked state to be set

Parameters :
Name Type Optional
value boolean No
Returns : void
import {
  ChangeDetectionStrategy,
  Component,
  EventEmitter,
  Input,
  Output,
  ViewEncapsulation,
} from '@angular/core';
import { MatSlideToggleChange } from '@angular/material/slide-toggle';

import {
  ControlValueAccessorProviderFactory,
  TsReactiveFormBaseComponent,
  TsStyleThemeTypes,
} from '@terminus/ui-utilities';


/**
 * Expose the MatSlideToggleChange event as TsSlideToggleChange. Used by {@link TsToggleComponent}
 */
export class TsSlideToggleChange extends MatSlideToggleChange {}


/**
 * The is a toggle component
 *
 * @example
 * <ts-toggle
 *              arialLabel="Disable my thing"
 *              [formControl]="yourHelperToGetFormControl('thingIsDisabled')"
 *              [isDisabled]="true"
 *              [isRequired]="true"
 *              labelPosition="before"
 *              name="My toggle"
 *              theme="accent"
 *              (selectionChange)="myMethod($event)"
 * >My Toggle!</ts-toggle>
 *
 * <example-url>https://getterminus.github.io/ui-demos-release/components/toggle</example-url>
 */
@Component({
  selector: 'ts-toggle',
  templateUrl: './toggle.component.html',
  styleUrls: ['./toggle.component.scss'],
  host: { class: 'ts-toggle' },
  providers: [ControlValueAccessorProviderFactory<TsToggleComponent>(TsToggleComponent)],
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  exportAs: 'tsToggle',
})
export class TsToggleComponent extends TsReactiveFormBaseComponent {
  /**
   * Define the aria label
   */
  @Input()
  public ariaLabel: string | undefined;

  /**
   * Allow the checked state to be set
   *
   * @param value
   */
  @Input()
  public set isChecked(value: boolean) {
    this._isChecked = value;
    this.value = this._isChecked;
  }
  public get isChecked(): boolean {
    return this._isChecked;
  }
  private _isChecked = false;

  /**
   * Define if the toggle should be disabled
   */
  @Input()
  public isDisabled = false;

  /**
   * Define if the toggle is required
   */
  @Input()
  public isRequired = false;

  /**
   * Define the position of the label
   */
  @Input()
  public labelPosition: 'before' | 'after' = 'after';

  /**
   * Define the input name for the toggle
   */
  @Input()
  public name = 'toggle';

  /**
   * Define the theme
   */
  @Input()
  public theme: TsStyleThemeTypes = 'primary';

  /**
   * Emit an event each time the toggle value changes
   */
  @Output()
  public readonly selectionChange: EventEmitter<TsSlideToggleChange> = new EventEmitter();
}

result-matching ""

    No results matching ""