Skip to content

Commit 15abe33

Browse files
authored
perf(module:checkbox): reduce change detection cycles (#7127)
1 parent 9971faa commit 15abe33

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

components/checkbox/checkbox.component.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2425
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
25-
import { Subject } from 'rxjs';
26+
import { fromEvent, Subject } from 'rxjs';
2627
import { takeUntil } from 'rxjs/operators';
2728

2829
import { 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
})
7675
export 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();

components/checkbox/checkbox.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BidiModule, Dir } from '@angular/cdk/bidi';
2-
import { Component, DebugElement, ViewChild } from '@angular/core';
2+
import { ApplicationRef, Component, DebugElement, ViewChild } from '@angular/core';
33
import { ComponentFixture, fakeAsync, flush, TestBed, waitForAsync } from '@angular/core/testing';
44
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
55
import { By } from '@angular/platform-browser';
@@ -126,6 +126,37 @@ describe('checkbox', () => {
126126
fixture.detectChanges();
127127
expect(checkbox.nativeElement.querySelector('input') === document.activeElement).toBe(false);
128128
});
129+
describe('change detection behavior', () => {
130+
it('should not run change detection when the `input` is clicked', () => {
131+
const appRef = TestBed.inject(ApplicationRef);
132+
const event = new MouseEvent('click');
133+
134+
spyOn(appRef, 'tick');
135+
spyOn(event, 'stopPropagation').and.callThrough();
136+
137+
const nzCheckbox = fixture.debugElement.query(By.directive(NzCheckboxComponent));
138+
nzCheckbox.nativeElement.querySelector('.ant-checkbox-input').dispatchEvent(event);
139+
140+
expect(appRef.tick).not.toHaveBeenCalled();
141+
expect(event.stopPropagation).toHaveBeenCalled();
142+
});
143+
it('should not run change detection when the `nz-checkbox` is clicked and it is disabled', () => {
144+
testComponent.disabled = true;
145+
fixture.detectChanges();
146+
147+
const appRef = TestBed.inject(ApplicationRef);
148+
const event = new MouseEvent('click');
149+
150+
spyOn(appRef, 'tick');
151+
spyOn(event, 'preventDefault').and.callThrough();
152+
153+
const nzCheckbox = fixture.debugElement.query(By.directive(NzCheckboxComponent));
154+
nzCheckbox.nativeElement.dispatchEvent(event);
155+
156+
expect(appRef.tick).not.toHaveBeenCalled();
157+
expect(event.preventDefault).toHaveBeenCalled();
158+
});
159+
});
129160
});
130161
describe('checkbox group basic', () => {
131162
let fixture: ComponentFixture<NzTestCheckboxGroupComponent>;

0 commit comments

Comments
 (0)