Skip to content

Commit

Permalink
fix(module: core): correct hidden behavior (#6919)
Browse files Browse the repository at this point in the history
- fix hidden attribute behavior
- make hidden behavior same as angular

Closes #6918

Co-authored-by: 文杰 刘 <wenjie.liu@easystack.cn>
  • Loading branch information
hungtcs and 文杰 刘 authored Nov 4, 2021
1 parent 5859f6c commit 987b1ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 7 additions & 5 deletions components/core/transition-patch/transition-patch.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
13 changes: 12 additions & 1 deletion components/core/transition-patch/transition-patch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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({
Expand All @@ -58,5 +69,5 @@ export class TestTransitionPatchRestoreComponent {}
template: ` <button nz-button [hidden]="hidden"></button> `
})
export class TestTransitionPatchHiddenBindingComponent {
hidden = false;
hidden: NzSafeAny = false;
}

0 comments on commit 987b1ca

Please sign in to comment.