libs/ui/checkbox/src/lib/checkbox/checkbox.component.ts
This is the checkbox UI Component
<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>
changeDetection | ChangeDetectionStrategy.OnPush |
encapsulation | ViewEncapsulation.None |
exportAs | tsCheckbox |
host | { |
providers |
ControlValueAccessorProviderFactory<TsCheckboxComponent>(TsCheckboxComponent)
|
selector | ts-checkbox |
styleUrls | ./checkbox.component.scss |
templateUrl | ./checkbox.component.html |
Properties |
|
Methods |
|
Inputs |
Outputs |
Accessors |
constructor(changeDetectorRef: ChangeDetectorRef)
|
||||||
Parameters :
|
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()
|
|
Inherited from
TsReactiveFormBaseComponent
|
|
Defined in
TsReactiveFormBaseComponent:41
|
|
Define the form control to get access to validators |
indeterminateChange | |
Type : EventEmitter
|
|
Emit a change when moving from the indeterminate state |
inputChange | |
Type : EventEmitter
|
|
Emit an event on input change |
Public onBlur |
onBlur()
|
Inherited from
TsReactiveFormBaseComponent
|
Defined in
TsReactiveFormBaseComponent:63
|
Set touched on blur
Returns :
void
|
Protected registerOnChange | ||||||
registerOnChange(fn: (_: any) => void)
|
||||||
Inherited from
TsReactiveFormBaseComponent
|
||||||
Defined in
TsReactiveFormBaseComponent:73
|
||||||
Register onChange callback (from ControlValueAccessor interface)
Parameters :
Returns :
void
|
Protected registerOnTouched | ||||||
registerOnTouched(fn: () => void)
|
||||||
Inherited from
TsReactiveFormBaseComponent
|
||||||
Defined in
TsReactiveFormBaseComponent:82
|
||||||
Register onTouched callback (from ControlValueAccessor interface)
Parameters :
Returns :
void
|
Protected writeValue | ||||||
writeValue(value: any)
|
||||||
Inherited from
TsReactiveFormBaseComponent
|
||||||
Defined in
TsReactiveFormBaseComponent:92
|
||||||
Write value to inner value (from ControlValueAccessor interface)
Parameters :
Returns :
void
|
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 : ''
|
Inherited from
TsReactiveFormBaseComponent
|
Defined in
TsReactiveFormBaseComponent:24
|
Define the internal data model |
Protected onChangeCallback |
Type : function
|
Default value : noop
|
Inherited from
TsReactiveFormBaseComponent
|
Defined in
TsReactiveFormBaseComponent:30
|
Define placeholder for callback (provided later by the control value accessor) |
Protected onTouchedCallback |
Type : function
|
Default value : noop
|
Inherited from
TsReactiveFormBaseComponent
|
Defined in
TsReactiveFormBaseComponent:35
|
Define placeholder for callback (provided later by the control value accessor) |
id | ||||||
getid()
|
||||||
setid(value: string)
|
||||||
Define an ID for the component
Parameters :
Returns :
void
|
isChecked | ||||||
getisChecked()
|
||||||
setisChecked(value: boolean)
|
||||||
Toggle the underlying checkbox if the isChecked property changes
Parameters :
Returns :
void
|
ngModel | ||||||
setngModel(v: boolean)
|
||||||
Toggle the underlying checkbox if the ngModel changes
Parameters :
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();
}
}