Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Dropdown fix for initial open state #6130

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions projects/core/directives/dropdown/dropdown-open.directive.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import {Directive, EventEmitter, Input, Output} from '@angular/core';
import {Directive, EventEmitter, Input, OnChanges, Output} from '@angular/core';

import type {TuiDropdownDirective} from './dropdown.directive';

@Directive({
selector: '[tuiDropdownOpen],[tuiDropdownOpenChange]',
})
export class TuiDropdownOpenDirective {
export class TuiDropdownOpenDirective implements OnChanges {
@Input()
set tuiDropdownOpen(open: boolean) {
this.dropdown?.toggle(open);
}
tuiDropdownOpen = false;

@Output()
readonly tuiDropdownOpenChange = new EventEmitter<boolean>();

dropdown?: TuiDropdownDirective;

update(open: boolean): void {
this.tuiDropdownOpen = open;
this.tuiDropdownOpenChange.emit(open);
}

ngOnChanges(): void {
this.dropdown?.toggle(this.tuiDropdownOpen);
}
}
12 changes: 10 additions & 2 deletions projects/core/directives/dropdown/dropdown.directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AfterViewChecked,
AfterViewInit,
ComponentRef,
Directive,
ElementRef,
Expand Down Expand Up @@ -47,6 +48,7 @@ import {TuiDropdownOpenDirective} from './dropdown-open.directive';
export class TuiDropdownDirective
implements
AfterViewChecked,
AfterViewInit,
OnDestroy,
OnChanges,
TuiPortalItem,
Expand Down Expand Up @@ -97,6 +99,12 @@ export class TuiDropdownDirective
this.refresh$.next();
}

ngAfterViewInit(): void {
if (this.open) {
this.toggle(this.open.tuiDropdownOpen);
}
}

ngOnChanges(): void {
if (!this.content) {
this.toggle(false);
Expand All @@ -118,11 +126,11 @@ export class TuiDropdownDirective
toggle(show: boolean): void {
if (show && this.content && !this.dropdownBoxRef) {
this.dropdownBoxRef = this.dropdownService.add(this.component);
this.open?.tuiDropdownOpenChange.emit(true);
this.open?.update(true);
} else if (!show && this.dropdownBoxRef) {
this.dropdownService.remove(this.dropdownBoxRef);
this.dropdownBoxRef = null;
this.open?.tuiDropdownOpenChange.emit(false);
this.open?.update(false);
}
}
}
Loading