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

Duplicate or wrong event binding (keydown) #1387

Closed
thekikao opened this issue May 30, 2018 · 1 comment
Closed

Duplicate or wrong event binding (keydown) #1387

thekikao opened this issue May 30, 2018 · 1 comment

Comments

@thekikao
Copy link

Your Environment

  • flatpickr version used: 4.5.0
  • Browser name and version: 67.0.x

It seems like there is a duplicate or wrong event binding.

The keydown event is falsely bound to the window.document.body:

bind(window.document.body, "keydown", onKeyDown);

but the correct binding to the input field is already set at the next line:
if (!self.config.static) bind(self._input, "keydown", onKeyDown);

This results in firing the onKeyDown function on every keypress at the whole page.
A simple removing line 412 should prevent the keydown-event-firework if multiple instances of the picker are used at a time.

Expected

Firing the keydown event only if the flatpickr has focus.

Observed

Firing the keydown on every keypress at the whole page. Numerous if multiple pickers are used.

Solution

Removing line 412 at flatpickr/src/index.ts

chmln added a commit that referenced this issue Mar 22, 2019
@ghiscoding
Copy link
Contributor

This should be reopen and should have been followed according to the original proposition, the current keyDown event is bound to window.body and this is crazy bad, that means that even if the element is not a Flatpickr element, it will still go through that onKeyDown handler, this is bad, bad and is not following the separation of concerns, this is considered leak... and it slows down someone's Angular App using my lib (Flatpickr is a dependency of my lib Angular-Slickgrid) as shown in this post

bind(window.document.body, "keydown", onKeyDown);

chmln added a commit that referenced this issue Jan 9, 2021
…#2329)

* fix: restrict onKeyDown listener to Flatpickr only fixes #1444, #1387
- the current onKeyDown event is currently listening to `document.body` but that has major performance impact since that equals to listening to any keystroke coming from any DOM elements that exist in current body and it also adds this body event listener every single time a Flatpickr element is created (if you have 5x Flatpickr input, it will create the `onKeyDown` listener to the `document.body` 5x times and that is bad especiall for performance).
- For a good separation of concerns, we should only listen to Flatpickr events coming from the Flatpickr input itself (or its calendar element)
- this properly fixes issues #1387 and #1444

* refactor: use calendarContainer to listen to onKeyDown & fix unit tests

Co-authored-by: Gregory <chmln@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants