File

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

Description

This is the checkbox UI Component

Extends

TsReactiveFormBaseComponent

Example

<ts-checkbox
  [formControl]="myControl"
  [(ngModel]="myModel"
  id="my-id"
  [isChecked]="true"
  [isDisabled]="false"
  [isIndeterminate]="false"
  [isRequired]="false"
  tabIndex="4"
  (inputChange)="myMethod($event)"
  (indeterminateChange)="myMethod($event)"
></ts-checkbox>

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

Metadata

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

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(changeDetectorRef: ChangeDetectorRef)
Parameters :
Name Type Optional
changeDetectorRef ChangeDetectorRef No

Inputs

id
Type : string

Define an ID for the component

isChecked
Type : boolean

Toggle the underlying checkbox if the isChecked property changes

isDisabled
Default value : false

Define if the checkbox is disabled

isIndeterminate
Default value : false

Define if the checkbox should be indeterminate

isRequired
Default value : false

Define if the checkbox is required

ngModel
Type : boolean

Toggle the underlying checkbox if the ngModel changes

tabIndex
Default value : 0

Define the tabindex

formControl
Type : FormControl
Default value : new FormControl()

Define the form control to get access to validators

Outputs

indeterminateChange
Type : EventEmitter

Emit a change when moving from the indeterminate state

inputChange
Type : EventEmitter

Emit an event on input change

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 _id
Type : string
Default value : this.uid
Public checkbox
Type : MatCheckbox
Decorators :
@ViewChild(MatCheckbox, {static: true})

Provide access to the MatCheckboxComponent

Protected uid
Default value : `ts-checkbox-${nextUniqueId++}`

Define the default component ID

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

id
getid()
setid(value: string)

Define an ID for the component

Parameters :
Name Type Optional
value string No
Returns : void
isChecked
getisChecked()
setisChecked(value: boolean)

Toggle the underlying checkbox if the isChecked property changes

Parameters :
Name Type Optional
value boolean No
Returns : void
ngModel
setngModel(v: boolean)

Toggle the underlying checkbox if the ngModel changes

Parameters :
Name Type Optional
v boolean No
Returns : void
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  EventEmitter,
  Input,
  Output,
  ViewChild,
  ViewEncapsulation,
} from '@angular/core';
import {
  MatCheckbox,
  MatCheckboxChange,
} from '@angular/material/checkbox';

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

/**
 * Expose the MatCheckboxChange event as TsCheckboxChange
 */
export class TsCheckboxChange extends MatCheckboxChange {}

/**
 * Unique ID for each instance
 */
let nextUniqueId = 0;

/**
 * This is the checkbox UI Component
 *
 * @example
 * <ts-checkbox
 *              [formControl]="myControl"
 *              [(ngModel]="myModel"
 *              id="my-id"
 *              [isChecked]="true"
 *              [isDisabled]="false"
 *              [isIndeterminate]="false"
 *              [isRequired]="false"
 *              tabIndex="4"
 *              (inputChange)="myMethod($event)"
 *              (indeterminateChange)="myMethod($event)"
 * ></ts-checkbox>
 *
 * <example-url>https://getterminus.github.io/ui-demos-release/components/checkbox</example-url>
 */
@Component({
  selector: 'ts-checkbox',
  templateUrl: './checkbox.component.html',
  styleUrls: ['./checkbox.component.scss'],
  host: {
    'class': 'ts-checkbox',
    '[attr.id]': 'id',
  },
  providers: [ControlValueAccessorProviderFactory<TsCheckboxComponent>(TsCheckboxComponent)],
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  exportAs: 'tsCheckbox',
})
export class TsCheckboxComponent extends TsReactiveFormBaseComponent {
  /**
   * Define the default component ID
   */
  protected uid = `ts-checkbox-${nextUniqueId++}`;

  /**
   * Provide access to the MatCheckboxComponent
   */
  @ViewChild(MatCheckbox, { static: true })
  public checkbox!: MatCheckbox;

  /**
   * Define an ID for the component
   *
   * @param value
   */
  @Input()
  public set id(value: string) {
    this._id = value || this.uid;
  }
  public get id(): string {
    return this._id;
  }
  protected _id: string = this.uid;

  /**
   * Toggle the underlying checkbox if the isChecked property changes
   *
   * @param value
   */
  @Input()
  public set isChecked(value: boolean) {
    this._isChecked = value;
    this.value = this._isChecked;
    this.checkbox.checked = this._isChecked;
    this.changeDetectorRef.detectChanges();
  }
  public get isChecked(): boolean {
    return this._isChecked;
  }
  private _isChecked = false;

  /**
   * Define if the checkbox is disabled
   */
  @Input()
  public isDisabled = false;

  /**
   * Define if the checkbox should be indeterminate
   */
  @Input()
  public isIndeterminate = false;

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

  /**
   * Toggle the underlying checkbox if the ngModel changes
   *
   * @param v
   */
  @Input()
  public set ngModel(v: boolean) {
    this._isChecked = v;
    this.value = v;
    this.changeDetectorRef.detectChanges();
  }

  /**
   * Define the tabindex
   */
  @Input()
  public tabIndex = 0;

  /**
   * Emit an event on input change
   */
  @Output()
  public readonly inputChange = new EventEmitter<boolean>();

  /**
   * Emit a change when moving from the indeterminate state
   */
  @Output()
  public readonly indeterminateChange = new EventEmitter<boolean>();

  constructor(private changeDetectorRef: ChangeDetectorRef) {
    super();
  }
}

result-matching ""

    No results matching ""