diff --git a/apps/ngx-bootstrap-docs/src/app/components/+collapse/demos/inline-display/inline-display.html b/apps/ngx-bootstrap-docs/src/app/components/+collapse/demos/inline-display/inline-display.html index 2c962efe7a..e4bf6f3f49 100644 --- a/apps/ngx-bootstrap-docs/src/app/components/+collapse/demos/inline-display/inline-display.html +++ b/apps/ngx-bootstrap-docs/src/app/components/+collapse/demos/inline-display/inline-display.html @@ -1,10 +1,10 @@ - -
-
+
Some content
diff --git a/src/collapse/collapse.directive.ts b/src/collapse/collapse.directive.ts index 00ddf7ebec..3e2b9327b0 100644 --- a/src/collapse/collapse.directive.ts +++ b/src/collapse/collapse.directive.ts @@ -11,8 +11,10 @@ import { EventEmitter, HostBinding, Input, + OnChanges, Output, - Renderer2 + Renderer2, + SimpleChanges } from '@angular/core'; import { @@ -28,7 +30,7 @@ import { '[class.collapse]': 'true' } }) -export class CollapseDirective implements AfterViewChecked { +export class CollapseDirective implements OnChanges, AfterViewChecked { /** This event fires as soon as content collapses */ @Output() collapsed: EventEmitter = new EventEmitter(); /** This event fires when collapsing is started */ @@ -50,41 +52,21 @@ export class CollapseDirective implements AfterViewChecked { // animation state @HostBinding('class.collapsing') isCollapsing = false; + /** display value used when collapse is visible */ @Input() - set display(value: string) { - if (!this.isAnimated) { - this._renderer.setStyle(this._el.nativeElement, 'display', value); - - return; - } - - this._display = value; - - if (value === 'none') { - this.hide(); - - return; - } - - this.show(); - } + display = 'block' /** turn on/off animation */ @Input() isAnimated = false; /** A flag indicating visibility of content (shown or hidden) */ @Input() set collapse(value: boolean) { this.collapseNewValue = value; - if (!this._player || this._isAnimationDone) { - this.isExpanded = value; - this.toggle(); - } } get collapse(): boolean { return this.isExpanded; } - private _display = 'block'; private _isAnimationDone?: boolean; private _player?: AnimationPlayer; private _stylesLoaded = false; @@ -104,6 +86,15 @@ export class CollapseDirective implements AfterViewChecked { this._factoryExpandAnimation = _builder.build(expandAnimation); } + ngOnChanges(changes: SimpleChanges): void { + if ('collapse' in changes) { + if (!this._player || this._isAnimationDone) { + this.isExpanded = this.collapseNewValue; + this.toggle(); + } + } + } + ngAfterViewChecked(): void { this._stylesLoaded = true; @@ -148,7 +139,7 @@ export class CollapseDirective implements AfterViewChecked { } /** allows to manually show collapsed content */ show(): void { - this._renderer.setStyle(this._el.nativeElement, 'display', this._display); + this._renderer.setStyle(this._el.nativeElement, 'display', this.display); this.isCollapsing = true; this.isExpanded = true;