Issue with Vue JS - Key press event for enter key is not triggering as expected

I am facing an issue with implementing a conditional keyup event in my code. When I try to conditionally use a computed property for @[listenToKeyup], the keyup.enter event does not get triggered.

<template>
   <div>
      <input @[listenToKeyup]="manageSearch"
             v-model="input"
             class="form-input"/>
   </div>
</template>
export default {
    props: {
      skipSearch: {
        type: Boolean,
        required: false,
        default: false
      },
      callback: {
        required: false
      },
    },
  setup(props, {emit}) {
      const input = ref('');

      const listenToKeyup = computed(() => props.skipSearch ? 'keyup.enter' : 'keyup');

      function manageSearch() {
        clearTimeout(searchTimeout)
        searchTimeout = setTimeout(props.callback(input.value), debounce);
      }

  return {
      input,
      listenToKeyup,
      manageSearch,
  };

  }

Upon further investigation, it seems that when props.skipSearch is set to true in another component, the 'manageSearch' function is not fired. However, in components where it is set to false, everything works as expected. It appears that the condition within listenToKeyup is correctly determined, but the keyup.enter event is still not being triggered. Interestingly, if I remove the condition and directly add the event '@keyup.enter="manageSearch"', it functions properly, ruling out any issues with that part of the code.

If anyone could provide insight into what might be going wrong here, I would greatly appreciate it.

Answer №1

It seems that the issue lies with the event-modifier.

While dynamic events can be implemented, modifiers are unfortunately not supported in this scenario.

To address this, you will need to modify your approach as suggested by @Boussadjra Brahim but remember that the modifier needs to be manually written out:

//manage Search
function manageSearch(event) {
  if (!props.skipSearch|| ["Enter"].includes(event.key)) {
    //perform desired action here
  }
}

Explore a Codesandbox example for reference

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorrect implementation of Bootstrap CSS in Jade template

Currently, I am leveraging Express to construct a website. However, there seems to be an issue with my jade template not displaying the bootstrap grid system accurately. Despite double-checking that my app path is correctly set through app.use(express.stat ...

Using a jquery function within a Laravel view

I am trying to retrieve a selected item from a dropdown menu using jQuery and then redirect it to a controller function. This function will return some data to be displayed based on the selected item. I could really use some assistance with this. Here is m ...

Leveraging Vuex store with vue-router

Is there a way to access the router from a Vuex store using vue-router to navigate to a new component? I've come across several discussions on this topic, but haven't found a solution that works for me. The error message I receive when attempting ...

Using dropzone.js on multiple files in a single webpage

Is it feasible to incorporate multiple dropzone elements on one webpage instead of having numerous file uploads on a single dropzone element? It appears that dropzone fails to trigger after the selection dialog box when there are several elements, each wi ...

What is the best way to reference an object within the Vue data as the value of an item?

If I wanted to incorporate the {{ $t('index-p1') }} value within the title property, how would I do so? items: [ { icon: 'mdi-apps', title: 'Welcome', to: '/' }, For int ...

Having trouble resolving the BooksAPI in my code, can anyone help?

I've been trying to troubleshoot this problem, but I can't seem to find a solution. I'm working on a web application for book reading that allows users to organize books they have read, want to read, and are currently reading. On the search ...

How can I generate a custom chunk and add a hook to index.html using Vue CLI?

I am trying to customize a chunk and include it in the generated index.html file. After creating a custom project using vue create ., I added a vue.config.js file to configure the settings. In my vue.config.js file, I specified the following settings: mod ...

Creating a soft focus effect in a specific region (behind the text)

While working on my homepage created with HTML/CSS/Javascript, I have text positioned at the top left corner of the screen. The challenge arises from the fact that I am using different backgrounds sourced from Reddit, and a script randomly selects one duri ...

Tips for dynamically passing a path in require method in JavaScript

I am facing an issue while trying to set require() with a dynamic path myPath: let myPath = './myDynamicModule'; require( { myPath } ) However, I keep encountering the following error: error: bundling failed: myComponent.js: myComponent.js ...

Error message displaying NaN and issues with setting class using color function in console log

The console log is displaying NaN for any variable I input. Additionally, the color function is not correctly changing classes, likely due to the number issue. I still need to address local storage, but my main focus is on getting the variable to display ...

The error message "Uncaught ReferenceError: $foo is undefined, please check your

Currently, I am working on a project to toggle the expansion and collapse of individual divs on a click event using jQuery within the Laravel Spark framework. Most of my code is based on the solution provided in response to this query. Upon clicking the s ...

Jquery animate - callback is triggered before animation finishes running

Is there a way for the element to first expand in height and then apply a background image once the height change is complete? Currently, I am experiencing laggy performance as the background image is applied before the height animation finishes. Can som ...

Is it possible to align an image that uses position:relative with one that uses position:absolute?

On a webpage, I have placed two images. The first image is set to position:relative and the second image is positioned right below it with position:absolute. How can I ensure that as you zoom in or out, the second image remains aligned with the first ima ...

What steps can I take to stop jQuery's $.getJSON function from converting my AJAX response keys into integers?

I am facing an issue where JQuery is changing the type of keys in a JSON response object from text to integer when populating a select box. This causes the response object to be reordered based on the numeric indexes, disrupting the order of the select box ...

The v-autocomplete feature in vuetify doesn't allow for editing the text input after an option has been selected

Link to code on CodePen: CodePen Link When you visit the provided page and enter "joe" into the search box, choose one of the two Joes that appear. Try to then select text in the search box or use backspace to delete only the last character. You will no ...

What is the best method for passing information to my frontend JavaScript files using Express?

When using ejs to render my html pages, I pass in data in the following manner: collection.find({}).toArray( function (err, results) { res.render('index', { results: results, ...

Looking to improve query performance on MongoDB using mongoskin - should you use a single query or

this relates to mongodb {cod_com:'WWWOAN', cod_prod[{prod:'proda',info:'hello world'},{prod:'pacda',info:'hello world'},{prod:'prcdb',info:'hello world'}] } {cod_com:'WWWOA2&a ...

display a different selection option depending on the previously chosen item

I'm working on displaying additional options in a select box when the user makes a selection from the main select box. I've set display:none for the select boxes, which will only appear when the user chooses an option from the main select box. C ...

The alternating colors in the sorting table are not visible due to the divs being hidden with the display set

I've come across a problem that has me stumped. So, there are two sorting filters on a table and one of them hides rows that don't apply, messing up the alternating colors. Take a look at the function that sorts and the CSS below. The issue is pr ...

Passing information between VueJs3 components

I have been coding a VueJs3 Project that includes a login system, and it's working perfectly. Now, I want to display the user's logged-in account on the homepage within a div. The message should be: "You are logged in as:" followed by the userna ...