Skip to content

Commit

Permalink
fix: TypeError cannot set properties of undefined (setting 'keydownHa…
Browse files Browse the repository at this point in the history
…ndlerIndex') (#9327)
  • Loading branch information
iamsivin committed Apr 30, 2024
1 parent 6917ea2 commit 4fd8c7a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/javascript/shared/mixins/keyboardEventListenerMixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,26 @@ export default {
if (events) {
const wrappedEvents = this.wrapEventsInKeybindingsHandler(events);
const keydownHandler = createKeybindingsHandler(wrappedEvents);
this.appendToHandler(keydownHandler);
document.addEventListener('keydown', keydownHandler);
this.addEventHandler(keydownHandler);
}
},
beforeDestroy() {
if (this.$el && this.$el.dataset.keydownHandlerIndex) {
if (this.$el && this.$el.dataset?.keydownHandlerIndex) {
const handlerToRemove =
taggedHandlers[this.$el.dataset.keydownHandlerIndex];
document.removeEventListener('keydown', handlerToRemove);
}
},
methods: {
appendToHandler(keydownHandler) {
addEventHandler(keydownHandler) {
const indexToAppend = taggedHandlers.push(keydownHandler) - 1;
const root = this.$el;
root.dataset.keydownHandlerIndex = indexToAppend;
if (root && root.dataset) {
// For the components with a top level v-if Vue renders it as an empty comment in the DOM
// so we need to check if the root element has a dataset property to ensure it is a valid element
document.addEventListener('keydown', keydownHandler);
root.dataset.keydownHandlerIndex = indexToAppend;
}
},
getKeyboardEvents() {
return null;
Expand Down

0 comments on commit 4fd8c7a

Please sign in to comment.