-
Notifications
You must be signed in to change notification settings - Fork 232
refactor(helper): inicializa listener de focus após abertura do popover #2693
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,8 +73,6 @@ export class PoHelperComponent extends PoHelperBaseComponent implements AfterVie | |
| } | ||
| ngAfterViewInit(): void { | ||
| PoHelperComponent.instances.push(this); | ||
| this.boundFocusIn = this.closePopoverOnFocusOut.bind(this); | ||
| window.addEventListener('focusin', this.boundFocusIn, true); | ||
| queueMicrotask(() => { | ||
| this.setPopoverPositionByScreen(); | ||
| }); | ||
|
|
@@ -99,9 +97,6 @@ export class PoHelperComponent extends PoHelperBaseComponent implements AfterVie | |
|
|
||
| ngOnDestroy(): void { | ||
| PoHelperComponent.instances = PoHelperComponent.instances.filter(i => i !== this); | ||
| if (this.boundFocusIn) { | ||
| window.removeEventListener('focusin', this.boundFocusIn, true); | ||
| } | ||
| } | ||
|
|
||
| openHelperPopover(): void { | ||
|
|
@@ -214,4 +209,15 @@ export class PoHelperComponent extends PoHelperBaseComponent implements AfterVie | |
| const literals = this.poHelperLiterals[lang] || this.poHelperLiterals['en']; | ||
| return literals[type]; | ||
| } | ||
|
|
||
| protected handleOpen(): void { | ||
| this.boundFocusIn = this.closePopoverOnFocusOut.bind(this); | ||
| window.addEventListener('focusin', this.boundFocusIn, true); | ||
| } | ||
|
|
||
| protected handleClose(): void { | ||
| if (this.boundFocusIn) { | ||
| window.removeEventListener('focusin', this.boundFocusIn, true); | ||
| } | ||
| } | ||
|
Comment on lines
+213
to
+222
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import { PO_CONTROL_POSITIONS } from './../../services/po-control-position/po-co | |
|
|
||
| const PO_POPOVER_DEFAULT_POSITION = 'right'; | ||
| const PO_POPOVER_DEFAULT_TRIGGER = 'click'; | ||
| const PO_POPOVER_TRIGGERS = ['click', 'hover']; | ||
| const PO_POPOVER_TRIGGERS = ['click', 'hover', 'function']; | ||
|
|
||
| /** | ||
| * @description | ||
|
|
@@ -96,6 +96,9 @@ export class PoPopoverBaseComponent { | |
| /** Evento disparado ao fechar o popover. */ | ||
| @Output('p-close') closePopover = new EventEmitter<any>(); | ||
|
|
||
| /** Evento disparado ao abrir o popover. */ | ||
| @Output('p-open') openPopover = new EventEmitter<any>(); | ||
|
Comment on lines
+99
to
+100
|
||
|
|
||
| // Controla se o popover fica oculto ou visível, por padrão é oculto. | ||
| isHidden: boolean = true; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -103,6 +103,11 @@ export class PoPopoverComponent extends PoPopoverBaseComponent implements AfterV | |||||||||||||||||||||||||||||||||||
| close(): void { | ||||||||||||||||||||||||||||||||||||
| this.isHidden = true; | ||||||||||||||||||||||||||||||||||||
| this.closePopover.emit(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (this.trigger === 'function' && this.clickoutListener) { | ||||||||||||||||||||||||||||||||||||
| this.clickoutListener(); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| this.cd.detectChanges(); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -121,8 +126,16 @@ export class PoPopoverComponent extends PoPopoverBaseComponent implements AfterV | |||||||||||||||||||||||||||||||||||
| this.setElementsControlPosition(); | ||||||||||||||||||||||||||||||||||||
| this.setPopoverPosition(); | ||||||||||||||||||||||||||||||||||||
| this.setOpacity(1); | ||||||||||||||||||||||||||||||||||||
| this.openPopover.emit(); | ||||||||||||||||||||||||||||||||||||
| this.cd.detectChanges(); | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (this.trigger === 'function') { | ||||||||||||||||||||||||||||||||||||
| this.clickoutListener = this.renderer.listen('document', 'click', (event: MouseEvent) => { | ||||||||||||||||||||||||||||||||||||
| this.togglePopup(event); | ||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+133
to
+137
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
Comment on lines
130
to
+138
|
||||||||||||||||||||||||||||||||||||
| this.cd.detectChanges(); | |
| }); | |
| if (this.trigger === 'function') { | |
| this.clickoutListener = this.renderer.listen('document', 'click', (event: MouseEvent) => { | |
| this.togglePopup(event); | |
| }); | |
| } | |
| if (this.trigger === 'function') { | |
| this.clickoutListener = this.renderer.listen('document', 'click', (event: MouseEvent) => { | |
| this.togglePopup(event); | |
| }); | |
| } | |
| this.cd.detectChanges(); | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The boundFocusIn property is being reassigned on every popover open without clearing the previous value. If the popover is opened, closed, and reopened multiple times, the boundFocusIn reference will be overwritten, but the old event listener reference will be lost. When handleClose() is called, it will only remove the most recent listener, leaving previous listeners attached. This creates a memory leak. Consider removing the existing listener before reassigning boundFocusIn, or check if it already exists before creating a new one.