Skip to content

Commit

Permalink
Filter keyboard events by type to ignore autocompletion (#4291)
Browse files Browse the repository at this point in the history
This PR solves #4290 by ignoring keyboard events that are not of type
`KeyboardEvent`.

It can be tested with the following snippet by picking an email from the
dropdown list:
```py
ui.keyboard(ui.notify, ignore=[])
ui.input('email').props('autocomplete=email')
```
Without this PR, the autocompletion triggers a keyboard event, causing
an exception due to missing attributes.
With this PR there is simply no keyboard event in this case.
  • Loading branch information
falkoschindler authored Feb 1, 2025
1 parent 2aa1355 commit 7a540bf
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions nicegui/elements/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ export default {
mounted() {
for (const event of this.events) {
document.addEventListener(event, (evt) => {
// https://github.com/zauberzeug/nicegui/issues/4290
if (!(evt instanceof KeyboardEvent)) return;

// https://stackoverflow.com/a/36469636/3419103
const focus = document.activeElement;
if (focus && this.ignore.includes(focus.tagName.toLowerCase())) return;

if (evt.repeat && !this.repeating) return;

this.$emit("key", {
action: event,
altKey: evt.altKey,
Expand Down

0 comments on commit 7a540bf

Please sign in to comment.