Skip to content

Commit

Permalink
fix(module:button): fix button loading bug (#2251)
Browse files Browse the repository at this point in the history
close #2191
  • Loading branch information
vthinkxie committed Oct 10, 2018
1 parent 25671b6 commit cb71e9b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 100 deletions.
49 changes: 2 additions & 47 deletions LICENSE
@@ -1,6 +1,6 @@
MIT LICENSE

Copyright (c) 2017 Alibaba.com
Copyright (c) 2017-present Alibaba.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand All @@ -19,49 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

MIT LICENSE

Copyright (c) 2015-present Alipay.com, https://www.alipay.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The MIT License

Copyright (c) 2017 Google, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 changes: 10 additions & 10 deletions angular.json
Expand Up @@ -64,16 +64,6 @@
}
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "./components/test.ts",
"karmaConfig": "./components/karma.conf.js",
"polyfills": "./components/polyfills.ts",
"tsConfig": "./components/tsconfig.spec.json",
"scripts": []
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
Expand Down Expand Up @@ -103,6 +93,16 @@
"tsConfig": "components/tsconfig.lib.json",
"project": "components/ng-package.json"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "components/test.ts",
"karmaConfig": "components/karma.conf.js",
"polyfills": "components/polyfills.ts",
"tsConfig": "components/tsconfig.spec.json",
"scripts": []
}
}
}
}
Expand Down
71 changes: 38 additions & 33 deletions components/button/nz-button.component.ts
Expand Up @@ -2,17 +2,23 @@ import {
AfterContentInit,
ChangeDetectorRef,
Component,
ContentChildren,
ElementRef,
HostBinding,
Input, NgZone, OnDestroy, OnInit,
Input,
NgZone,
OnDestroy,
OnInit,
QueryList,
Renderer2,
ViewChild
} from '@angular/core';

import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { isEmpty } from '../core/util/check';
import { filterNotEmptyNode, isEmpty } from '../core/util/check';
import { toBoolean } from '../core/util/convert';
import { NzWaveDirective } from '../core/wave/nz-wave.directive';
import { NzIconDirective } from '../icon/nz-icon.directive';

export type NzButtonType = 'primary' | 'dashed' | 'danger';
export type NzButtonShape = 'circle' | null ;
Expand All @@ -38,6 +44,7 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
private prefixCls = 'ant-btn';
private sizeMap = { large: 'lg', small: 'sm' };
@ViewChild('contentElement') contentElement: ElementRef;
@ContentChildren(NzIconDirective, { read: ElementRef }) listOfIconElement: QueryList<ElementRef>;

@Input()
set nzBlock(value: boolean) {
Expand Down Expand Up @@ -70,25 +77,25 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
}

@Input()
get nzType(): NzButtonType {
return this._type;
}

set nzType(value: NzButtonType) {
this._type = value;
this.setClassMap();
}

@Input()
get nzShape(): NzButtonShape {
return this._shape;
get nzType(): NzButtonType {
return this._type;
}

@Input()
set nzShape(value: NzButtonShape) {
this._shape = value;
this.setClassMap();
}

get nzShape(): NzButtonShape {
return this._shape;
}

@Input()
set nzSize(value: NzButtonSize) {
this._size = value;
Expand Down Expand Up @@ -127,19 +134,22 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
[ `${this.prefixCls}-loading` ] : this.nzLoading,
[ `${this.prefixCls}-icon-only` ] : this.iconOnly,
[ `${this.prefixCls}-background-ghost` ] : this.nzGhost,
[ `ant-input-search-button` ] : this.nzSearch,
[ `ant-btn-block` ] : this.nzBlock
[ `${this.prefixCls}-block` ] : this.nzBlock,
[ `ant-input-search-button` ] : this.nzSearch
};
this.nzUpdateHostClassService.updateHostClass(this.el, classMap);
}

checkContent(): void {
this.moveIcon();
const hasIcon = this.listOfIconElement && this.listOfIconElement.length;
if (hasIcon) {
this.moveIcon();
}
this.renderer.removeStyle(this.contentElement.nativeElement, 'display');
/** https://github.com/angular/angular/issues/12530 **/
if (isEmpty(this.contentElement.nativeElement)) {
this.renderer.setStyle(this.contentElement.nativeElement, 'display', 'none');
this.iconOnly = !!this.iconElement;
this.iconOnly = !!hasIcon;
} else {
this.renderer.removeStyle(this.contentElement.nativeElement, 'display');
this.iconOnly = false;
Expand All @@ -150,39 +160,34 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy {
}

moveIcon(): void {
const firstChildElement = this.findFirstNotEmptyNode(this.contentElement.nativeElement);
const lastChildElement = this.findLastNotEmptyNode(this.contentElement.nativeElement);
if (firstChildElement && (firstChildElement.nodeName === 'I')) {
this.renderer.insertBefore(this.el, firstChildElement, this.contentElement.nativeElement);
this.iconElement = firstChildElement as HTMLElement;
} else if (lastChildElement && (lastChildElement.nodeName === 'I')) {
this.renderer.appendChild(this.el, lastChildElement);
this.iconElement = lastChildElement as HTMLElement;
} else {
this.iconElement = null;
if (this.listOfIconElement && this.listOfIconElement.length) {
const firstChildElement = this.findFirstNotEmptyNode(this.contentElement.nativeElement);
const lastChildElement = this.findLastNotEmptyNode(this.contentElement.nativeElement);
if (firstChildElement && (firstChildElement === this.listOfIconElement.first.nativeElement)) {
this.renderer.insertBefore(this.el, firstChildElement, this.contentElement.nativeElement);
this.iconElement = firstChildElement as HTMLElement;
} else if (lastChildElement && (lastChildElement === this.listOfIconElement.last.nativeElement)) {
this.renderer.appendChild(this.el, lastChildElement);
}
}
}

findFirstNotEmptyNode(value: HTMLElement): Node {
const children = value.childNodes;
findFirstNotEmptyNode(element: HTMLElement): Node {
const children = element.childNodes;
for (let i = 0; i < children.length; i++) {
const node = children.item(i);
if (node && (node.nodeType === 1) && ((node as HTMLElement).outerHTML.toString().trim().length !== 0)) {
return node;
} else if (node && (node.nodeType === 3) && ((node.textContent.toString().trim().length !== 0))) {
if (filterNotEmptyNode(node)) {
return node;
}
}
return null;
}

findLastNotEmptyNode(value: HTMLElement): Node {
const children = value.childNodes;
findLastNotEmptyNode(element: HTMLElement): Node {
const children = element.childNodes;
for (let i = children.length - 1; i >= 0; i--) {
const node = children.item(i);
if (node && (node.nodeType === 1) && ((node as HTMLElement).outerHTML.toString().trim().length !== 0)) {
return node;
} else if (node && (node.nodeType === 3) && ((node.textContent.toString().trim().length !== 0))) {
if (filterNotEmptyNode(node)) {
return node;
}
}
Expand Down
54 changes: 54 additions & 0 deletions components/button/nz-button.spec.ts
Expand Up @@ -281,6 +281,43 @@ describe('button', () => {
expect(buttons[ 3 ].nativeElement.classList.contains('ant-btn-block')).toBe(true);
});
});
describe('binding', () => {
let button;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [ NzButtonModule, NzIconModule ],
declarations: [ NzTestButtonBindingComponent ],
providers : []
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(NzTestButtonBindingComponent);
testComponent = fixture.debugElement.componentInstance;
button = fixture.debugElement.query(By.directive(NzButtonComponent));
});

it('should hide icon when loading correct', fakeAsync(() => {
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(button.nativeElement.classList.contains('ant-btn-loading')).toBe(false);
expect(button.nativeElement.firstElementChild.querySelector('svg')).toBe(null);
expect(button.nativeElement.firstElementChild.classList.contains('anticon-loading')).toBe(false);
button.nativeElement.click();
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(button.nativeElement.classList.contains('ant-btn-loading')).toBe(true);
expect(button.nativeElement.firstElementChild.classList.contains('anticon-loading')).toBe(true);
expect(button.nativeElement.querySelector('.anticon-poweroff').style.cssText).toBe('display: none;');
tick(5000);
fixture.detectChanges();
expect(button.nativeElement.classList.contains('ant-btn-loading')).toBe(false);
expect(button.nativeElement.firstElementChild.classList.contains('anticon-loading')).toBe(false);
expect(button.nativeElement.querySelector('.anticon-poweroff').style.cssText).toBe('display: inline-block;');
}));
});

});

Expand All @@ -291,3 +328,20 @@ describe('button', () => {
})
export class NzTestButtonSearchComponent {
}

/** https://github.com/NG-ZORRO/ng-zorro-antd/issues/2191 **/
@Component({
selector: 'nz-test-button-binding',
template: `<button nz-button nzType="primary" (click)="load()" [nzLoading]="loading"><i nz-icon type="poweroff"></i> {{'Click me!'}}</button>`
})
export class NzTestButtonBindingComponent {
loading = false;

load(): void {
this.loading = true;
setTimeout(_ => {
this.loading = false;
}, 5000);
}

}
41 changes: 31 additions & 10 deletions components/core/util/check.ts
Expand Up @@ -7,22 +7,32 @@ export function isNotNil(value: any): boolean {

/** 校验对象是否相等 */
export function shallowEqual(objA: {}, objB: {}): boolean {
if (objA === objB) return true;
if (objA === objB) {
return true;
}

if (typeof objA !== 'object' || !objA || typeof objB !== 'object' || !objB) return false;
if (typeof objA !== 'object' || !objA || typeof objB !== 'object' || !objB) {
return false;
}

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

if (keysA.length !== keysB.length) return false;
if (keysA.length !== keysB.length) {
return false;
}

const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);

// tslint:disable-next-line:prefer-for-of
for (let idx = 0; idx < keysA.length; idx++) {
const key = keysA[idx];
if (!bHasOwnProperty(key)) return false;
if (objA[key] !== objB[key]) return false;
const key = keysA[ idx ];
if (!bHasOwnProperty(key)) {
return false;
}
if (objA[ key ] !== objB[ key ]) {
return false;
}
}

return true;
Expand All @@ -37,16 +47,27 @@ export function isInteger(value: string | number): boolean {
export function isEmpty(element: HTMLElement): boolean {
const nodes = element.childNodes;
for (let i = 0; i < nodes.length; i++) {
const node = nodes.item(i);
if ((node.nodeType === 1) && ((node as HTMLElement).outerHTML.toString().trim().length !== 0)) {
return false;
} else if ((node.nodeType === 3) && ((node.textContent.toString().trim().length !== 0))) {
if (filterNotEmptyNode(nodes.item(i))) {
return false;
}
}
return true;
}

export function filterNotEmptyNode(node: Node): Node {
if (node) {
if ((node.nodeType === 1) && ((node as HTMLElement).outerHTML.toString().trim().length !== 0)) {
// ELEMENT_NODE
return node;
} else if ((node.nodeType === 3) && (node.textContent.toString().trim().length !== 0)) {
// TEXT_NODE
return node;
}
return null;
}
return null;
}

export function isNonEmptyString(value: any): boolean { // tslint:disable-line:no-any
return typeof value === 'string' && value !== '';
}
Expand Down

0 comments on commit cb71e9b

Please sign in to comment.