-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module: checkbox&radio): ngModel&Onpush support (#167)
- Loading branch information
Showing
21 changed files
with
439 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,75 @@ | ||
import { Component, Input, Output, EventEmitter, HostBinding } from '@angular/core'; | ||
import { | ||
Component, | ||
forwardRef, | ||
Input, | ||
Output, | ||
EventEmitter, | ||
HostBinding, | ||
ViewEncapsulation, | ||
ChangeDetectionStrategy | ||
} from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
|
||
export interface OnChangeEvent { | ||
export interface CheckboxStatus { | ||
name: string; | ||
value: string; | ||
checked: boolean; | ||
} | ||
|
||
@Component({ | ||
selector: 'AgreeItem, nzm-agree-item', | ||
templateUrl: './agree-item.component.html' | ||
templateUrl: './agree-item.component.html', | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => AgreeItem), | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class AgreeItem { | ||
export class AgreeItem implements ControlValueAccessor { | ||
prefixCls: string = 'am-checkbox'; | ||
checked: boolean = false; | ||
private _disabled: boolean = false; | ||
|
||
private _ngModelOnChange: (value: boolean) => {}; | ||
private _ngModelOnTouched: () => {}; | ||
|
||
@Input() | ||
name: string; | ||
@Input() | ||
value: string; | ||
@Input() | ||
checked: boolean = false; | ||
@Input() | ||
disabled: boolean = false; | ||
get disabled(): boolean { | ||
return this._disabled; | ||
} | ||
set disabled(value: boolean) { | ||
this._disabled = value; | ||
} | ||
@Output() | ||
onChange = new EventEmitter<OnChangeEvent>(); | ||
onChange = new EventEmitter<CheckboxStatus>(); | ||
|
||
@HostBinding('class.am-checkbox-agree') | ||
checkboxAgree: boolean = true; | ||
|
||
constructor() {} | ||
|
||
agree() { | ||
if (!this.disabled) { | ||
this.checked = !this.checked; | ||
} | ||
} | ||
constructor() { } | ||
|
||
change(event) { | ||
this._ngModelOnChange(event.checked); | ||
this.onChange.emit(event); | ||
} | ||
|
||
writeValue(value: boolean): void { | ||
this.checked = value; | ||
} | ||
|
||
registerOnChange(fn: (_: boolean) => {}): void { | ||
this._ngModelOnChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: () => {}): void { | ||
this._ngModelOnTouched = fn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,64 @@ | ||
import { Component, Input, Output, EventEmitter } from '@angular/core'; | ||
import { Component, forwardRef, Input, Output, EventEmitter, ChangeDetectionStrategy, ViewEncapsulation } from '@angular/core'; | ||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
|
||
export interface OnChangeEvent { | ||
export interface CheckboxStatus { | ||
name: string; | ||
value: string; | ||
checked: boolean; | ||
} | ||
|
||
@Component({ | ||
selector: 'CheckboxItem, nzm-checkbox-item', | ||
templateUrl: './checkbox-item.component.html' | ||
templateUrl: './checkbox-item.component.html', | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => CheckboxItem), | ||
multi: true | ||
} | ||
] | ||
}) | ||
export class CheckboxItem { | ||
export class CheckboxItem implements ControlValueAccessor { | ||
prefixCls = 'am-checkbox'; | ||
checked: boolean = false; | ||
private _disabled: boolean = false; | ||
private _ngModelOnChange: (value: boolean) => {}; | ||
private _ngModelOnTouched: () => {}; | ||
|
||
@Input() | ||
name: string; | ||
@Input() | ||
value: string; | ||
@Input() | ||
checked: boolean = false; | ||
@Input() | ||
disabled: boolean = false; | ||
@Output() | ||
onChange = new EventEmitter<OnChangeEvent>(); | ||
get disabled(): boolean { | ||
return this._disabled; | ||
} | ||
set disabled(value: boolean) { | ||
this._disabled = value; | ||
} | ||
@Output() | ||
onClick = new EventEmitter(); | ||
onChange = new EventEmitter<CheckboxStatus>(); | ||
|
||
constructor() {} | ||
constructor() { } | ||
|
||
onCheckboxClick(event) { | ||
if (!this.disabled) { | ||
this.checked = !this.checked; | ||
} | ||
if (this.onClick.observers.length > 0) { | ||
this.onClick.emit(event); | ||
} | ||
} | ||
onCheckboxClick(event) { } | ||
|
||
change(event) { | ||
this._ngModelOnChange(event.checked); | ||
this.onChange.emit(event); | ||
} | ||
|
||
writeValue(value: boolean): void { | ||
this.checked = value; | ||
} | ||
|
||
registerOnChange(fn: (_: boolean) => {}): void { | ||
this._ngModelOnChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: () => {}): void { | ||
this._ngModelOnTouched = fn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.