Skip to content

Commit

Permalink
fix(module:checkbox): fix checkbox a11y error (NG-ZORRO#3009)
Browse files Browse the repository at this point in the history
  • Loading branch information
vthinkxie authored Mar 1, 2019
1 parent 174e7c1 commit b11ec37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 1 addition & 4 deletions components/checkbox/nz-checkbox.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
[class.ant-checkbox-checked]="nzChecked && !nzIndeterminate"
[class.ant-checkbox-disabled]="nzDisabled"
[class.ant-checkbox-indeterminate]="nzIndeterminate">
<input #inputElement
[checked]="nzChecked"
type="checkbox"
class="ant-checkbox-input">
<input #inputElement [checked]="nzChecked" [ngModel]="nzChecked" [disabled]="nzDisabled" (ngModelChange)="innerCheckedChange($event)" (click)="$event.stopPropagation();" type="checkbox" class="ant-checkbox-input">
<span class="ant-checkbox-inner"></span>
</span>
<span #contentElement (cdkObserveContent)="checkContent()"><ng-content></ng-content></span>
8 changes: 6 additions & 2 deletions components/checkbox/nz-checkbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { NzCheckboxWrapperComponent } from './nz-checkbox-wrapper.component';
}
],
host : {
'(click)' : 'hostClick($event)'
'(click)': 'hostClick($event)'
}
})
export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnChanges, AfterViewInit, OnDestroy {
Expand All @@ -57,8 +57,12 @@ export class NzCheckboxComponent implements OnInit, ControlValueAccessor, OnChan
hostClick(e: MouseEvent): void {
e.preventDefault();
this.focus();
this.innerCheckedChange(!this.nzChecked);
}

innerCheckedChange(checked: boolean): void {
if (!this.nzDisabled) {
this.nzChecked = !this.nzChecked;
this.nzChecked = checked;
this.onChange(this.nzChecked);
this.nzCheckedChange.emit(this.nzChecked);
if (this.nzCheckboxWrapperComponent) {
Expand Down
14 changes: 14 additions & 0 deletions components/checkbox/nz-checkbox.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ describe('checkbox', () => {
expect(checkbox.nativeElement.firstElementChild.classList.contains('ant-checkbox-checked')).toBe(true);
expect(testComponent.modelChange).toHaveBeenCalledTimes(1);
});
it('should click input a11y correct', () => {
fixture.detectChanges();
const inputElement = checkbox.nativeElement.querySelector('input');
expect(testComponent.checked).toBe(false);
expect(inputElement.checked).toBe(false);
expect(checkbox.nativeElement.firstElementChild.classList.contains('ant-checkbox-checked')).toBe(false);
expect(testComponent.modelChange).toHaveBeenCalledTimes(0);
inputElement.click();
fixture.detectChanges();
expect(testComponent.checked).toBe(true);
expect(checkbox.nativeElement.firstElementChild.classList.contains('ant-checkbox-checked')).toBe(true);
expect(inputElement.checked).toBe(true);
expect(testComponent.modelChange).toHaveBeenCalledTimes(1);
});
it('should ngModel change', fakeAsync(() => {
testComponent.checked = true;
fixture.detectChanges();
Expand Down

0 comments on commit b11ec37

Please sign in to comment.