Skip to content

Commit

Permalink
chore(module:switch): refactor switch (#4771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yadong Xie authored Feb 12, 2020
1 parent 423a382 commit 6dc85c8
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 112 deletions.
6 changes: 0 additions & 6 deletions components/switch/demo/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,3 @@ title:

The most basic usage.


<style>
.ant-switch {
margin-bottom: 8px;
}
</style>
10 changes: 2 additions & 8 deletions components/switch/demo/disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ import { Component } from '@angular/core';
template: `
<nz-switch [(ngModel)]="switchValue" [nzDisabled]="isDisabled"></nz-switch>
<br />
<br />
<button nz-button [nzType]="'primary'" (click)="isDisabled = !isDisabled">Toggle disabled</button>
`,
styles: [
`
nz-switch {
margin-bottom: 8px;
}
`
]
`
})
export class NzDemoSwitchDisabledComponent {
switchValue = false;
Expand Down
10 changes: 2 additions & 8 deletions components/switch/demo/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import { Component } from '@angular/core';
template: `
<nz-switch [ngModel]="true" nzLoading></nz-switch>
<br />
<br />
<nz-switch nzSize="small" [ngModel]="false" nzLoading></nz-switch>
`,
styles: [
`
nz-switch {
margin-bottom: 8px;
}
`
]
`
})
export class NzDemoSwitchLoadingComponent {}
10 changes: 2 additions & 8 deletions components/switch/demo/size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ import { Component } from '@angular/core';
template: `
<nz-switch [ngModel]="true"></nz-switch>
<br />
<br />
<nz-switch nzSize="small" [ngModel]="true"></nz-switch>
`,
styles: [
`
nz-switch {
margin-bottom: 8px;
}
`
]
`
})
export class NzDemoSwitchSizeComponent {}
11 changes: 3 additions & 8 deletions components/switch/demo/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import { Component } from '@angular/core';
template: `
<nz-switch [ngModel]="true" nzCheckedChildren="开" nzUnCheckedChildren="关"></nz-switch>
<br />
<br />
<nz-switch [ngModel]="false" nzCheckedChildren="1" nzUnCheckedChildren="0"></nz-switch>
<br />
<br />
<nz-switch [ngModel]="true" [nzCheckedChildren]="checkedTemplate" [nzUnCheckedChildren]="unCheckedTemplate"></nz-switch>
<ng-template #checkedTemplate><i nz-icon nzType="check"></i></ng-template>
<ng-template #unCheckedTemplate><i nz-icon nzType="close"></i></ng-template>
`,
styles: [
`
nz-switch {
margin-bottom: 8px;
}
`
]
`
})
export class NzDemoSwitchTextComponent {}
29 changes: 0 additions & 29 deletions components/switch/nz-switch.component.html

This file was deleted.

4 changes: 2 additions & 2 deletions components/switch/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

export * from './nz-switch.component';
export * from './nz-switch.module';
export * from './switch.component';
export * from './switch.module';
1 change: 1 addition & 0 deletions components/switch/style/entry.less
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@import './index.less';
@import './patch.less';
3 changes: 3 additions & 0 deletions components/switch/style/patch.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nz-switch {
display: inline-block;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import {
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

import { InputBoolean, NzConfigService, NzSizeDSType, WithConfig } from 'ng-zorro-antd/core';
import { InputBoolean, NzConfigService, WithConfig } from 'ng-zorro-antd/core';
import { NzSizeDSType, OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';

const NZ_CONFIG_COMPONENT_NAME = 'switch';

@Component({
selector: 'nz-switch',
exportAs: 'nzSwitch',
preserveWhitespaces: false,
templateUrl: './nz-switch.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
providers: [
Expand All @@ -41,40 +41,59 @@ const NZ_CONFIG_COMPONENT_NAME = 'switch';
multi: true
}
],
template: `
<button
nz-wave
type="button"
class="ant-switch"
#switchElement
[disabled]="nzDisabled"
[class.ant-switch-checked]="isChecked"
[class.ant-switch-loading]="nzLoading"
[class.ant-switch-disabled]="nzDisabled"
[class.ant-switch-small]="nzSize === 'small'"
[nzWaveExtraNode]="true"
(keydown)="onKeyDown($event)"
>
<i *ngIf="nzLoading" nz-icon nzType="loading" class="ant-switch-loading-icon"></i>
<span class="ant-switch-inner">
<ng-container *ngIf="isChecked; else uncheckTemplate">
<ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container>
</ng-container>
<ng-template #uncheckTemplate>
<ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container>
</ng-template>
</span>
<div class="ant-click-animating-node"></div>
</button>
`,
host: {
'(click)': 'hostClick($event)'
},
styles: [
`
nz-switch {
display: inline-block;
}
`
]
'(click)': 'onHostClick($event)'
}
})
export class NzSwitchComponent implements ControlValueAccessor, AfterViewInit, OnDestroy {
checked = false;
onChange: (value: boolean) => void = () => null;
onTouched: () => void = () => null;
isChecked = false;
onChange: OnChangeType = () => {};
onTouched: OnTouchedType = () => {};
@ViewChild('switchElement', { static: true }) private switchElement: ElementRef;
@Input() @InputBoolean() nzLoading = false;
@Input() @InputBoolean() nzDisabled = false;
@Input() @InputBoolean() nzControl = false;
@Input() nzCheckedChildren: string | TemplateRef<void>;
@Input() nzUnCheckedChildren: string | TemplateRef<void>;
@Input() nzCheckedChildren: string | TemplateRef<void> | null = null;
@Input() nzUnCheckedChildren: string | TemplateRef<void> | null = null;
@Input() @WithConfig(NZ_CONFIG_COMPONENT_NAME, 'default') nzSize: NzSizeDSType;

hostClick(e: MouseEvent): void {
onHostClick(e: MouseEvent): void {
e.preventDefault();
if (!this.nzDisabled && !this.nzLoading && !this.nzControl) {
this.updateValue(!this.checked);
this.updateValue(!this.isChecked);
}
}

updateValue(value: boolean): void {
if (this.checked !== value) {
this.checked = value;
this.onChange(this.checked);
if (this.isChecked !== value) {
this.isChecked = value;
this.onChange(this.isChecked);
}
}

Expand All @@ -87,7 +106,7 @@ export class NzSwitchComponent implements ControlValueAccessor, AfterViewInit, O
this.updateValue(true);
e.preventDefault();
} else if (e.keyCode === SPACE || e.keyCode === ENTER) {
this.updateValue(!this.checked);
this.updateValue(!this.isChecked);
e.preventDefault();
}
}
Expand All @@ -106,11 +125,7 @@ export class NzSwitchComponent implements ControlValueAccessor, AfterViewInit, O
ngAfterViewInit(): void {
this.focusMonitor.monitor(this.switchElement.nativeElement, true).subscribe(focusOrigin => {
if (!focusOrigin) {
// When a focused element becomes disabled, the browser *immediately* fires a blur event.
// Angular does not expect events to be raised during change detection, so any state change
// (such as a form control's 'ng-touched') will cause a changed-after-checked error.
// See https://github.com/angular/angular/issues/17793. To work around this, we defer
// telling the form control it has been touched until the next tick.
/** https://github.com/angular/angular/issues/17793 **/
Promise.resolve().then(() => this.onTouched());
}
});
Expand All @@ -121,20 +136,20 @@ export class NzSwitchComponent implements ControlValueAccessor, AfterViewInit, O
}

writeValue(value: boolean): void {
this.checked = value;
this.isChecked = value;
this.cdr.markForCheck();
}

registerOnChange(fn: (_: boolean) => void): void {
registerOnChange(fn: OnChangeType): void {
this.onChange = fn;
}

registerOnTouched(fn: () => void): void {
registerOnTouched(fn: OnTouchedType): void {
this.onTouched = fn;
}

setDisabledState(isDisabled: boolean): void {
this.nzDisabled = isDisabled;
setDisabledState(disabled: boolean): void {
this.nzDisabled = disabled;
this.cdr.markForCheck();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NgModule } from '@angular/core';
import { NzOutletModule, NzWaveModule } from 'ng-zorro-antd/core';
import { NzIconModule } from 'ng-zorro-antd/icon';

import { NzSwitchComponent } from './nz-switch.component';
import { NzSwitchComponent } from './switch.component';

@NgModule({
exports: [NzSwitchComponent],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { Component, DebugElement, TemplateRef, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, flush, TestBed } from '@angular/core/testing';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { dispatchKeyboardEvent } from 'ng-zorro-antd/core';
import { NzIconTestModule } from 'ng-zorro-antd/icon/testing';

import { NzSwitchComponent } from './nz-switch.component';
import { NzSwitchModule } from './nz-switch.module';
import { NzSwitchComponent } from './switch.component';
import { NzSwitchModule } from './switch.module';

describe('switch', () => {
beforeEach(fakeAsync(() => {
Expand Down Expand Up @@ -177,16 +175,12 @@ describe('switch', () => {
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(switchElement.nativeElement.firstElementChild.firstElementChild.firstElementChild.firstElementChild!.classList).toContain(
'anticon-close'
);
expect(switchElement.nativeElement.firstElementChild.firstElementChild.firstElementChild!.classList).toContain('anticon-close');
switchElement.nativeElement.click();
fixture.detectChanges();
flush();
fixture.detectChanges();
expect(switchElement.nativeElement.firstElementChild.firstElementChild.firstElementChild.firstElementChild!.classList).toContain(
'anticon-check'
);
expect(switchElement.nativeElement.firstElementChild.firstElementChild.firstElementChild!.classList).toContain('anticon-check');
}));
});
describe('switch form', () => {
Expand Down

0 comments on commit 6dc85c8

Please sign in to comment.