From cb71e9b2bea97c31dac9bf9f5d8561f92a75c053 Mon Sep 17 00:00:00 2001 From: VTHINKXIE Date: Wed, 10 Oct 2018 11:56:57 +0800 Subject: [PATCH] fix(module:button): fix button loading bug (#2251) close #2191 --- LICENSE | 49 +--------------- angular.json | 20 +++---- components/button/nz-button.component.ts | 71 +++++++++++++----------- components/button/nz-button.spec.ts | 54 ++++++++++++++++++ components/core/util/check.ts | 41 ++++++++++---- 5 files changed, 135 insertions(+), 100 deletions(-) diff --git a/LICENSE b/LICENSE index 63ce9e4488..5d29fb0199 100644 --- a/LICENSE +++ b/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 @@ -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. \ No newline at end of file diff --git a/angular.json b/angular.json index 33b05424ee..8152c48314 100644 --- a/angular.json +++ b/angular.json @@ -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": { @@ -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": [] + } } } } diff --git a/components/button/nz-button.component.ts b/components/button/nz-button.component.ts index ad283a60c6..0b37fc02ee 100644 --- a/components/button/nz-button.component.ts +++ b/components/button/nz-button.component.ts @@ -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 ; @@ -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; @Input() set nzBlock(value: boolean) { @@ -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; @@ -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; @@ -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; } } diff --git a/components/button/nz-button.spec.ts b/components/button/nz-button.spec.ts index 7edf975f92..85b6e6b0dc 100644 --- a/components/button/nz-button.spec.ts +++ b/components/button/nz-button.spec.ts @@ -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;'); + })); + }); }); @@ -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: `` +}) +export class NzTestButtonBindingComponent { + loading = false; + + load(): void { + this.loading = true; + setTimeout(_ => { + this.loading = false; + }, 5000); + } + +} diff --git a/components/core/util/check.ts b/components/core/util/check.ts index ba84538857..79ffa21dfd 100644 --- a/components/core/util/check.ts +++ b/components/core/util/check.ts @@ -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; @@ -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 !== ''; }