Skip to content

Commit 4a37a36

Browse files
committed
refactor(dropdown-close): add input for dropdown component, skip on disabled
1 parent 41a981d commit 4a37a36

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

projects/coreui-angular/src/lib/dropdown/dropdown-close/dropdown-close.directive.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { Directive, HostBinding, HostListener, Input, Optional } from '@angular/core';
1+
import { AfterViewInit, Directive, HostBinding, HostListener, Input, Optional } from '@angular/core';
22
import { DropdownService } from '../dropdown.service';
33
import { DropdownComponent } from '../dropdown/dropdown.component';
44

55
@Directive({
66
selector: '[cDropdownClose]',
77
exportAs: 'cDropdownClose'
88
})
9-
export class DropdownCloseDirective {
9+
export class DropdownCloseDirective implements AfterViewInit {
10+
11+
constructor(
12+
private dropdownService: DropdownService,
13+
@Optional() public dropdown?: DropdownComponent
14+
) { }
1015

1116
/**
1217
* Disables a dropdown-close directive.
@@ -15,10 +20,14 @@ export class DropdownCloseDirective {
1520
*/
1621
@Input() disabled?: boolean;
1722

18-
constructor(
19-
private dropdownService: DropdownService,
20-
@Optional() public dropdown?: DropdownComponent
21-
) { }
23+
@Input() dropdownComponent?: DropdownComponent;
24+
25+
ngAfterViewInit(): void {
26+
if (this.dropdownComponent) {
27+
this.dropdown = this.dropdownComponent;
28+
this.dropdownService = this.dropdownComponent?.dropdownService;
29+
}
30+
}
2231

2332
@HostBinding('class')
2433
get hostClasses(): any {
@@ -44,13 +53,13 @@ export class DropdownCloseDirective {
4453

4554
@HostListener('click', ['$event'])
4655
private onClick($event: MouseEvent): void {
47-
this.dropdownService.toggle({ visible: false, dropdown: this.dropdown });
56+
!this.disabled && this.dropdownService.toggle({ visible: false, dropdown: this.dropdown });
4857
}
4958

5059
@HostListener('keyup', ['$event'])
5160
private onKeyUp($event: KeyboardEvent): void {
5261
if ($event.key === 'Enter') {
53-
this.dropdownService.toggle({ visible: false, dropdown: this.dropdown });
62+
!this.disabled && this.dropdownService.toggle({ visible: false, dropdown: this.dropdown });
5463
}
5564
}
5665
}

0 commit comments

Comments
 (0)