ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
116 lines (101 loc) • 2.68 kB
text/typescript
import {
forwardRef,
Component,
HostListener,
Input,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { toBoolean } from '../util/convert';
export class NzSwitchComponent implements OnInit, ControlValueAccessor {
private _disabled = false;
_prefixCls = 'ant-switch';
_innerPrefixCls = `${this._prefixCls}-inner`;
_classMap;
_size: string;
_checked = false;
// ngModel Access
onChange: (value: boolean) => void = () => null;
onTouched: () => void = () => null;
set nzSize(value: string) {
this._size = value;
this.setClassMap();
}
get nzSize(): string {
return this._size;
}
set nzDisabled(value: boolean) {
this._disabled = toBoolean(value);
this.setClassMap();
}
get nzDisabled(): boolean {
return this._disabled;
}
onClick(e: MouseEvent): void {
e.preventDefault();
if (!this._disabled) {
this.updateValue(!this._checked);
this.onChange(this._checked);
}
}
updateValue(value: boolean): void {
if (this._checked === value) {
return;
}
this._checked = value;
this.setClassMap();
}
setClassMap(): void {
this._classMap = {
[this._prefixCls] : true,
[`${this._prefixCls}-checked`] : this._checked,
[`${this._prefixCls}-disabled`]: this._disabled,
[`${this._prefixCls}-small`] : this._size === 'small'
};
}
writeValue(value: boolean): void {
this.updateValue(value);
}
registerOnChange(fn: (_: boolean) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
this.nzDisabled = isDisabled;
}
ngOnInit(): void {
this.setClassMap();
}
}