@@ -14,6 +14,7 @@ import {
1414 EventEmitter ,
1515 forwardRef ,
1616 Input ,
17+ NgZone ,
1718 OnDestroy ,
1819 OnInit ,
1920 Optional ,
@@ -22,7 +23,7 @@ import {
2223 ViewEncapsulation
2324} from '@angular/core' ;
2425import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
25- import { Subject } from 'rxjs' ;
26+ import { fromEvent , Subject } from 'rxjs' ;
2627import { takeUntil } from 'rxjs/operators' ;
2728
2829import { BooleanInput , NzSafeAny , OnChangeType , OnTouchedType } from 'ng-zorro-antd/core/types' ;
@@ -53,7 +54,6 @@ import { NzCheckboxWrapperComponent } from './checkbox-wrapper.component';
5354 [ngModel]="nzChecked"
5455 [disabled]="nzDisabled"
5556 (ngModelChange)="innerCheckedChange($event)"
56- (click)="$event.stopPropagation()"
5757 />
5858 <span class="ant-checkbox-inner"></span>
5959 </span>
@@ -69,8 +69,7 @@ import { NzCheckboxWrapperComponent } from './checkbox-wrapper.component';
6969 host : {
7070 class : 'ant-checkbox-wrapper' ,
7171 '[class.ant-checkbox-wrapper-checked]' : 'nzChecked' ,
72- '[class.ant-checkbox-rtl]' : `dir === 'rtl'` ,
73- '(click)' : 'hostClick($event)'
72+ '[class.ant-checkbox-rtl]' : `dir === 'rtl'`
7473 }
7574} )
7675export class NzCheckboxComponent implements OnInit , ControlValueAccessor , OnDestroy , AfterViewInit {
@@ -84,7 +83,7 @@ export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnDest
8483
8584 onChange : OnChangeType = ( ) => { } ;
8685 onTouched : OnTouchedType = ( ) => { } ;
87- @ViewChild ( 'inputElement' , { static : true } ) private inputElement ! : ElementRef ;
86+ @ViewChild ( 'inputElement' , { static : true } ) inputElement ! : ElementRef < HTMLInputElement > ;
8887 @Output ( ) readonly nzCheckedChange = new EventEmitter < boolean > ( ) ;
8988 @Input ( ) nzValue : NzSafeAny | null = null ;
9089 @Input ( ) @InputBoolean ( ) nzAutoFocus = false ;
@@ -93,12 +92,6 @@ export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnDest
9392 @Input ( ) @InputBoolean ( ) nzChecked = false ;
9493 @Input ( ) nzId : string | null = null ;
9594
96- hostClick ( e : MouseEvent ) : void {
97- e . preventDefault ( ) ;
98- this . focus ( ) ;
99- this . innerCheckedChange ( ! this . nzChecked ) ;
100- }
101-
10295 innerCheckedChange ( checked : boolean ) : void {
10396 if ( ! this . nzDisabled ) {
10497 this . nzChecked = checked ;
@@ -137,6 +130,7 @@ export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnDest
137130 }
138131
139132 constructor (
133+ private ngZone : NgZone ,
140134 private elementRef : ElementRef < HTMLElement > ,
141135 @Optional ( ) private nzCheckboxWrapperComponent : NzCheckboxWrapperComponent ,
142136 private cdr : ChangeDetectorRef ,
@@ -157,13 +151,34 @@ export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnDest
157151 this . nzCheckboxWrapperComponent . addCheckbox ( this ) ;
158152 }
159153
160- this . directionality . change ? .pipe ( takeUntil ( this . destroy$ ) ) . subscribe ( ( direction : Direction ) => {
154+ this . directionality . change . pipe ( takeUntil ( this . destroy$ ) ) . subscribe ( ( direction : Direction ) => {
161155 this . dir = direction ;
162156 this . cdr . detectChanges ( ) ;
163157 } ) ;
164158
165159 this . dir = this . directionality . value ;
160+
161+ this . ngZone . runOutsideAngular ( ( ) => {
162+ fromEvent ( this . elementRef . nativeElement , 'click' )
163+ . pipe ( takeUntil ( this . destroy$ ) )
164+ . subscribe ( event => {
165+ event . preventDefault ( ) ;
166+ this . focus ( ) ;
167+ if ( this . nzDisabled ) {
168+ return ;
169+ }
170+ this . ngZone . run ( ( ) => {
171+ this . innerCheckedChange ( ! this . nzChecked ) ;
172+ this . cdr . markForCheck ( ) ;
173+ } ) ;
174+ } ) ;
175+
176+ fromEvent ( this . inputElement . nativeElement , 'click' )
177+ . pipe ( takeUntil ( this . destroy$ ) )
178+ . subscribe ( event => event . stopPropagation ( ) ) ;
179+ } ) ;
166180 }
181+
167182 ngAfterViewInit ( ) : void {
168183 if ( this . nzAutoFocus ) {
169184 this . focus ( ) ;
0 commit comments