Skip to content

Commit

Permalink
feat(module:TODO): simplify boolean attributes (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
trotyl authored and vthinkxie committed Dec 5, 2017
1 parent 80d637d commit 08f10e4
Show file tree
Hide file tree
Showing 439 changed files with 2,039 additions and 3,016 deletions.
40 changes: 32 additions & 8 deletions src/components/alert/nz-alert.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Output,
EventEmitter, OnChanges, OnInit
} from '@angular/core';
import { toBoolean } from '../util/convert';
import { FadeAnimation } from '../core/animation/fade-animations';

@Component({
Expand Down Expand Up @@ -52,18 +53,45 @@ import { FadeAnimation } from '../core/animation/fade-animations';
'./style/patch.less'
]
})

export class NzAlertComponent implements OnChanges {
private _banner = false;
private _closeable = false;
private _showIcon = false;
_display = true;
antAlert = 'ant-alert';
@Input() nzType = 'info';
@Input() nzBanner = false;
@Input() nzCloseable = false;
@Input() nzDescription: string;
@Input() nzShowIcon = false;
@Input() nzCloseText: string;
@Input() nzMessage: string;
@Output() nzOnClose: EventEmitter<boolean> = new EventEmitter();

@Input()
set nzBanner(value: boolean) {
this._banner = toBoolean(value);
}

get nzBanner(): boolean {
return this._banner;
}

@Input()
set nzCloseable(value: boolean) {
this._closeable = toBoolean(value);
}

get nzCloseable(): boolean {
return this._closeable;
}

@Input()
set nzShowIcon(value: boolean) {
this._showIcon = toBoolean(value);
}

get nzShowIcon(): boolean {
return this._showIcon;
}

_classMap = {
[`${this.antAlert}`] : true,
[`${this.antAlert}-${this.nzType}`] : true,
Expand All @@ -86,8 +114,4 @@ export class NzAlertComponent implements OnChanges {
[`${this.antAlert}-with-description`]: !!this.nzDescription
};
}

constructor() {
}

}
1 change: 0 additions & 1 deletion src/components/avatar/nz-avatar.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ import { NzAvatarComponent } from './nz-avatar.component';
exports : [ NzAvatarComponent ],
imports : [ CommonModule ]
})

export class NzAvatarModule {
}
25 changes: 16 additions & 9 deletions src/components/badge/nz-badge.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
animate
} from '@angular/animations';

import { toBoolean } from '../util/convert';

@Component({
selector : 'nz-badge',
encapsulation: ViewEncapsulation.None,
Expand Down Expand Up @@ -60,7 +62,8 @@ import {
]
})
export class NzBadgeComponent implements OnInit {
_showZero = false;
private _showDot = false;
private _showZero = false;
count: number;
maxNumberArray;
countArray = [];
Expand All @@ -76,19 +79,23 @@ export class NzBadgeComponent implements OnInit {
@Input() nzOverflowCount = 99;

@Input()
set nzShowZero(value: boolean | string) {
if (value === '') {
this._showZero = true;
} else {
this._showZero = value as boolean;
}
set nzShowZero(value: boolean) {
this._showZero = toBoolean(value);
}

get nzShowZero() {
get nzShowZero(): boolean {
return this._showZero;
}

@Input() nzDot = false;
@Input()
set nzDot(value: boolean) {
this._showDot = toBoolean(value);
}

get nzDot(): boolean {
return this._showDot;
}

@Input() nzText: string;
@Input() nzStyle;
@Input() @HostBinding('class.ant-badge-status') nzStatus: string;
Expand Down
4 changes: 0 additions & 4 deletions src/components/breadcrumb/nz-breadcrumb.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ import {
export class NzBreadCrumbComponent {
@Input() nzSeparator = '/';
@HostBinding('class.ant-breadcrumb') _nzBreadcrumb = true;

constructor() {
}

}
6 changes: 1 addition & 5 deletions src/components/button/nz-button-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class NzButtonGroupComponent implements AfterContentInit {
@Input()
get nzSize(): NzButtonGroupSize {
return this._size;
};
}

set nzSize(value: NzButtonGroupSize) {
this._size = value;
Expand All @@ -35,10 +35,6 @@ export class NzButtonGroupComponent implements AfterContentInit {
};
}


constructor() {
}

ngAfterContentInit() {
/** trim text node between button */
Array.from(this._groupWrapper.nativeElement.childNodes).forEach((node: HTMLElement) => {
Expand Down
54 changes: 46 additions & 8 deletions src/components/button/nz-button.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { Component, DebugElement } from '@angular/core';
import { async, fakeAsync, tick, ComponentFixture, TestBed, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { Component, DebugElement, ViewChild } from '@angular/core';
import { By } from '@angular/platform-browser';
import { NzButtonModule } from './nz-button.module';
import { NzButtonComponent } from './nz-button.component';
Expand Down Expand Up @@ -123,13 +123,12 @@ describe('NzButton', () => {
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
});

it('should handle a click on the button', () => {
it('should handle a click on the button', fakeAsync(() => {
buttonDebugElement.nativeElement.click();
expect(testComponent.isLoading).toBe(true);
setTimeout(_ => {
expect(testComponent.isLoading).toBe(false);
}, 5000);
});
tick(5000);
expect(testComponent.isLoading).toBe(false);
}));
});

describe('NzButton with disabled', () => {
Expand Down Expand Up @@ -185,6 +184,35 @@ describe('NzButton', () => {
});
});

describe('NzButton with literal boolean attributes', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [ NzButtonModule ],
declarations: [ TestAppLiteral ],
providers : []
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestAppLiteral);
testComponent = fixture.debugElement.componentInstance;
});

it('should treat empty attibutes as truthy', async(() => {
fixture.detectChanges();
const component = testComponent as TestAppLiteral;
expect(component.truthyButton.nzLoading).toBe(true);
expect(component.truthyButton.nzGhost).toBe(true);
}));

it('should treat non-exist attributes as falsy', async(() => {
fixture.detectChanges();
const component = testComponent as TestAppLiteral;
expect(component.falsyButton.nzLoading).toBe(false);
expect(component.falsyButton.nzGhost).toBe(false);
}));
});

});

/** Test component that contains an nzButton. */
Expand Down Expand Up @@ -261,4 +289,14 @@ class TestAppGroup {
size = 'small';
}


@Component({
selector: 'test-app-literal',
template: `
<button #truthy nz-button nzLoading nzGhost>Truthy</button>
<button #falsy nz-button>Falsy</button>
`
})
class TestAppLiteral {
@ViewChild('truthy') truthyButton: NzButtonComponent
@ViewChild('falsy') falsyButton: NzButtonComponent
}
19 changes: 10 additions & 9 deletions src/components/button/nz-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
AfterContentInit,
Renderer2
} from '@angular/core';
import { toBoolean } from '../util/convert';

export type NzButtonType = 'primary' | 'dashed' | 'danger';
export type NzButtonShape = 'circle' | null ;
Expand All @@ -24,6 +25,8 @@ export type NzButtonSize = 'small' | 'large' | 'default' ;
]
})
export class NzButtonComponent implements AfterContentInit {
private _ghost = false;
private _loading = false;
_el: HTMLElement;
nativeElement: HTMLElement;
_iconElement: HTMLElement;
Expand All @@ -32,15 +35,13 @@ export class NzButtonComponent implements AfterContentInit {
_size: NzButtonSize;
_classList: Array<string> = [];
_iconOnly = false;
_loading = false;
_clicked = false;
_ghost = false;
_prefixCls = 'ant-btn';
_sizeMap = { large: 'lg', small: 'sm' };

@Input()
set nzGhost(value: boolean) {
this._ghost = value;
this._ghost = toBoolean(value);
this._setClassMap();
}

Expand All @@ -51,7 +52,7 @@ export class NzButtonComponent implements AfterContentInit {
@Input()
get nzType(): NzButtonType {
return this._type;
};
}

set nzType(value: NzButtonType) {
this._type = value;
Expand All @@ -61,7 +62,7 @@ export class NzButtonComponent implements AfterContentInit {
@Input()
get nzShape(): NzButtonShape {
return this._shape;
};
}

set nzShape(value: NzButtonShape) {
this._shape = value;
Expand All @@ -76,18 +77,18 @@ export class NzButtonComponent implements AfterContentInit {

get nzSize(): NzButtonSize {
return this._size;
};
}

@Input()
set nzLoading(value: boolean) {
this._loading = value;
this._loading = toBoolean(value);
this._setClassMap();
this._setIconDisplay(value);
}

get nzLoading(): boolean {
return this._loading;
};
}

/** toggle button clicked animation */
@HostListener('click')
Expand All @@ -98,7 +99,7 @@ export class NzButtonComponent implements AfterContentInit {
this._clicked = false;
this._setClassMap();
}, 300);
};
}


_setIconDisplay(value: boolean) {
Expand Down
1 change: 0 additions & 1 deletion src/components/button/nz-button.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import { CommonModule } from '@angular/common';
exports : [ NzButtonComponent, NzButtonGroupComponent ],
imports : [ CommonModule ]
})

export class NzButtonModule {
}
Loading

0 comments on commit 08f10e4

Please sign in to comment.