This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dropdown): add directive for clicking outside of an element (#2615)
* fix(directive): add directive for clicking outside an element * fix(selectDropdown): add clickOut directive to Select-dropdown * fix(datatable): close config when clicked outside * fix(config): use clickOut directive to close the dropdown
- Loading branch information
1 parent
1dea93a
commit 9184140
Showing
8 changed files
with
76 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
EventEmitter, | ||
HostListener, | ||
Inject, | ||
Input, | ||
Output | ||
} from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: '[clickOut]' | ||
}) | ||
export class ClickOutDirective { | ||
|
||
@Input('exclude') exclude: string; | ||
|
||
@Output('clickOut') clickOutside: EventEmitter<any> = new EventEmitter(); | ||
|
||
@HostListener('document:click', ['$event', '$event.target']) | ||
onClick(event: MouseEvent, target: HTMLElement): void { | ||
if (!target) { | ||
return; | ||
} | ||
|
||
const clickedInside = this.element.nativeElement.contains(target); | ||
|
||
if (this.exclude) { | ||
this.isExcluded = this.excludeCheck(target); | ||
} | ||
|
||
if (!clickedInside && !this.isExcluded) { | ||
this.clickOutside.emit(event); | ||
} | ||
} | ||
|
||
private isExcluded: boolean = false; | ||
constructor(private element: ElementRef) { | ||
} | ||
|
||
excludeCheck(target: HTMLElement) { | ||
const excludeElements = Array.from(document.querySelectorAll(this.exclude)) as Array<HTMLElement>; | ||
for (let element of excludeElements) { | ||
if(element.contains(target)){ | ||
return true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { NgModule } from "@angular/core"; | ||
import { ClickOutDirective } from "./clickout.directive"; | ||
|
||
@NgModule({ | ||
declarations: [ClickOutDirective], | ||
exports: [ClickOutDirective] | ||
}) | ||
|
||
export class ClickOutModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters