diff --git a/components/core/transition-patch/transition-patch.directive.ts b/components/core/transition-patch/transition-patch.directive.ts index 9ac05d0a278..c0bc9d5f0f2 100644 --- a/components/core/transition-patch/transition-patch.directive.ts +++ b/components/core/transition-patch/transition-patch.directive.ts @@ -19,12 +19,14 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types'; export class NzTransitionPatchDirective implements AfterViewInit, OnChanges { @Input() hidden: NzSafeAny = null; setHiddenAttribute(): void { - if (this.hidden === true) { - this.renderer.setAttribute(this.elementRef.nativeElement, 'hidden', ''); - } else if (this.hidden === false || this.hidden === null) { + if (this.hidden) { + if (typeof this.hidden === 'string') { + this.renderer.setAttribute(this.elementRef.nativeElement, 'hidden', this.hidden); + } else { + this.renderer.setAttribute(this.elementRef.nativeElement, 'hidden', ''); + } + } else { this.renderer.removeAttribute(this.elementRef.nativeElement, 'hidden'); - } else if (typeof this.hidden === 'string') { - this.renderer.setAttribute(this.elementRef.nativeElement, 'hidden', this.hidden); } } diff --git a/components/core/transition-patch/transition-patch.spec.ts b/components/core/transition-patch/transition-patch.spec.ts index e2e5889b029..df897534692 100644 --- a/components/core/transition-patch/transition-patch.spec.ts +++ b/components/core/transition-patch/transition-patch.spec.ts @@ -7,6 +7,7 @@ import { Component } from '@angular/core'; import { By } from '@angular/platform-browser'; import { ɵcreateComponentBed as createComponentBed } from 'ng-zorro-antd/core/testing'; +import { NzSafeAny } from 'ng-zorro-antd/core/types'; import { NzTransitionPatchDirective } from './transition-patch.directive'; import { NzTransitionPatchModule } from './transition-patch.module'; @@ -37,6 +38,16 @@ describe('transition-patch', () => { testBed.fixture.detectChanges(); expect(buttonElement.getAttribute('hidden')).toBe(''); }); + it('should work if hidden binding with undefined', () => { + const testBed = createComponentBed(TestTransitionPatchHiddenBindingComponent, { + imports: [NzTransitionPatchModule] + }); + const buttonElement = testBed.debugElement.query(By.directive(NzTransitionPatchDirective)).nativeElement; + expect(buttonElement.getAttribute('hidden')).toBeFalsy(); + testBed.component.hidden = undefined; + testBed.fixture.detectChanges(); + expect(buttonElement.hasAttribute('hidden')).toBeFalse(); + }); }); @Component({ @@ -58,5 +69,5 @@ export class TestTransitionPatchRestoreComponent {} template: ` ` }) export class TestTransitionPatchHiddenBindingComponent { - hidden = false; + hidden: NzSafeAny = false; }