-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(module:button): fix button animation bug caused by angular
close #2697
- Loading branch information
vthinkxie
committed
Jan 10, 2020
1 parent
7d841a4
commit 9e0df2a
Showing
13 changed files
with
182 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
export * from './public-api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"ngPackage": { | ||
"lib": { | ||
"entryFile": "public-api.ts" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
export { NzTransitionPatchModule as ɵNzTransitionPatchModule } from './transition-patch.module'; | ||
export { NzTransitionPatchDirective as ɵNzTransitionPatchDirective } from './transition-patch.directive'; |
62 changes: 62 additions & 0 deletions
62
components/core/transition-patch/transition-patch.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
import { Platform } from '@angular/cdk/platform'; | ||
import { AfterViewInit, Directive, ElementRef, Renderer2 } from '@angular/core'; | ||
|
||
/** | ||
* hack the bug | ||
* angular router change with unexpected transition trigger after calling applicationRef.attachView | ||
* https://github.com/angular/angular/issues/34718 | ||
*/ | ||
@Directive({ | ||
selector: '[nz-button],nz-button-group,[nz-icon]' | ||
}) | ||
export class NzTransitionPatchDirective implements AfterViewInit { | ||
readonly nativeElement: HTMLElement | null = null; | ||
private readonly hiddenAttribute: string | null = null; | ||
getHiddenAttribute(element: HTMLElement): string | null { | ||
if (this.platform.isBrowser) { | ||
return element.getAttribute('hidden'); | ||
} else { | ||
return null; | ||
} | ||
} | ||
getHiddenAttributeCount(attribute: string | null): number { | ||
if (attribute === null) { | ||
return 0; | ||
} else if (typeof +attribute === 'number' && !isNaN(+attribute)) { | ||
return +attribute; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
setHiddenAttribute(element: HTMLElement): void { | ||
const increasedHiddenAttribute = this.getHiddenAttributeCount(this.getHiddenAttribute(element)) + 1; | ||
this.renderer.setAttribute(element, 'hidden', `${increasedHiddenAttribute}`); | ||
} | ||
restoreHiddenAttribute(element: HTMLElement, originHiddenAttribute: string | null): void { | ||
const decreasedHiddenAttribute = this.getHiddenAttributeCount(this.getHiddenAttribute(element)) - 1; | ||
if (decreasedHiddenAttribute === 0) { | ||
this.renderer.removeAttribute(element, 'hidden'); | ||
} else if (decreasedHiddenAttribute === 1) { | ||
this.renderer.setAttribute(element, 'hidden', originHiddenAttribute || ''); | ||
} else { | ||
this.renderer.setAttribute(element, 'hidden', `${decreasedHiddenAttribute}`); | ||
} | ||
} | ||
|
||
constructor(elementRef: ElementRef, private renderer: Renderer2, private platform: Platform) { | ||
this.nativeElement = elementRef.nativeElement; | ||
this.hiddenAttribute = this.getHiddenAttribute(this.nativeElement!); | ||
this.setHiddenAttribute(this.nativeElement!); | ||
} | ||
ngAfterViewInit(): void { | ||
this.restoreHiddenAttribute(this.nativeElement!, this.hiddenAttribute); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
components/core/transition-patch/transition-patch.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
import { PlatformModule } from '@angular/cdk/platform'; | ||
import { NgModule } from '@angular/core'; | ||
import { NzTransitionPatchDirective } from './transition-patch.directive'; | ||
|
||
@NgModule({ | ||
imports: [PlatformModule], | ||
exports: [NzTransitionPatchDirective], | ||
declarations: [NzTransitionPatchDirective] | ||
}) | ||
export class NzTransitionPatchModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license | ||
* Copyright Alibaba.com All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
*/ | ||
|
||
import { Component } from '@angular/core'; | ||
import { By } from '@angular/platform-browser'; | ||
import { ɵcreateComponentBed as createComponentBed } from 'ng-zorro-antd/core'; | ||
import { NzTransitionPatchDirective } from './transition-patch.directive'; | ||
|
||
describe('transition-patch', () => { | ||
it('should visible after afterViewInit', () => { | ||
const testBed = createComponentBed(TestTransitionPatchComponent, { declarations: [NzTransitionPatchDirective] }); | ||
const buttonElement = testBed.debugElement.query(By.directive(NzTransitionPatchDirective)).nativeElement; | ||
expect(buttonElement.getAttribute('hidden')).toBeFalsy(); | ||
}); | ||
it('should hidden after afterViewInit', () => { | ||
const testBed = createComponentBed(TestTransitionPatchHiddenComponent, { declarations: [NzTransitionPatchDirective] }); | ||
const buttonElement = testBed.debugElement.query(By.directive(NzTransitionPatchDirective)).nativeElement; | ||
expect(buttonElement.getAttribute('hidden')).toBeFalsy(); | ||
}); | ||
it('should restore after afterViewInit', () => { | ||
const testBed = createComponentBed(TestTransitionPatchRestoreComponent, { declarations: [NzTransitionPatchDirective] }); | ||
const buttonElement = testBed.debugElement.query(By.directive(NzTransitionPatchDirective)).nativeElement; | ||
expect(buttonElement.getAttribute('hidden')).toBe('abc'); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: ` | ||
<button nz-button id="patch"></button> | ||
` | ||
}) | ||
export class TestTransitionPatchComponent {} | ||
|
||
@Component({ | ||
template: ` | ||
<button nz-button hidden id="patch"></button> | ||
` | ||
}) | ||
export class TestTransitionPatchHiddenComponent {} | ||
|
||
@Component({ | ||
template: ` | ||
<button nz-button hidden="abc" id="patch"></button> | ||
` | ||
}) | ||
export class TestTransitionPatchRestoreComponent {} |