Issue with Custom Dropdown input not triggering blur event

I've been working on a custom dropup component and have encountered some strange issues with it. The main problem is the blur event not firing when clicking outside of the input field.

My goal is to hide the options elements on blur, reset the component to the Initial View if there's no localValue, and set it to the Selected State if there is a localValue. However, I'm struggling to get the blur event to work correctly.

---Designs---

Initial State

https://i.stack.imgur.com/BNYjQ.png

Toggled State

https://i.stack.imgur.com/GqZWw.png

Selected State

https://i.stack.imgur.com/wqMiA.png

To see this issue in action, check out my CodeSandbox demo.

I'd appreciate any suggestions on how to resolve this and get the blur event working properly.

Any help or advice would be greatly welcomed!

Cheers!

NOTE

I tried putting the blur event directly on the input element, but it didn't solve the issue completely. While using optionsAreVisible.value = true did close the dropdown, I still believe triggering blur on the entire element is more appropriate.

Extra Issues:

  1. Is my combination of :key="buttonKey" and buttonKey.value++ to reset the component correct? It feels a bit hacky to me, so I'm unsure about its validity.

  2. How can I make the Initial State and Selected State the same size? The selected state has additional elements, but both designs should have equal width. I'm having difficulty achieving this without hardcoding widths.

  3. There seems to be an issue with

    :class="[localValue === '' ? '_hide' : '', '_input-active-delete']"
    , as it isn't functioning in Codesandbox. Any insights on why this might be happening? https://i.stack.imgur.com/V2Vep.png

SelectModelInput Component

<template>
  <!-- Component template code here -->
</template>

<script>
export default {
  // Script goes here
};
</script>

<style lang="scss" scoped>
/* Styling for the component */
</style>

App.vue File

<template>
  <!-- Template content -->
</template>

<script>
export default {
  // JavaScript logic here
};
</script>

<style lang="scss" scoped>
/* Styling for the app */
</style>

Answer №1

To display the choices, you need to click on the "Type to Search" <input>. The options should be hidden when this <input> loses focus. I had to create separate functions in your code to toggle selectModelVersionIsVisible and optionsAreVisible: Check out my sandbox for the updated code: codesandbox

In response to one of your previous comments, you can clear localValue by simply setting localValue = '' when the "X" button is clicked, as demonstrated in my sandbox. There's no need to use :key

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

What exactly is the role of the @altfontfamily variable in Bootstrap?

Within the variables.less file, specifically in the typography section, you will find a variable named @altfontfamily. I am interested in utilizing the Alt Font Family but I am unsure of the process. Is there a specific class that needs to be called in or ...

Is it possible to adjust the background image with properties like Scale to Fill, Aspect Fit, or Aspect Fill?

Is it possible to set the background image using the following properties in HTML? (These are properties used for iOS images, but I am unsure if there are similar ones in HTML): Scale to Fill, Aspect Fit, Aspect Fill https://i.stack.imgur.com/5X83I.jpg ...

JavaScript does not function properly with dynamically loaded content

Trying to load the page content using the JQuery load() method. See below for the code snippet: $(window).ready(function() { $('#content').load('views/login.html'); }); .mdl-layout { align-items: center; justify-content: center ...

Determine the placement of the body with CSS styling

Here is the code snippet from my website: body { background-image: url('background.jpg'); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; } .centered { /* Center entire body */ display: flex; ...

The input box fails to show larger values on the user's page

Show me the biggest number that the user enters in an input box and then display it back to them. I need some improvements to my code, ideally making it possible with just one input box instead of two. <!DOCTYPE html> <html la ...

Vue.js computed property fails to initiate

Why is the Vue JS computed property not triggered with this markup? <!-- language: lang-html --> <p>£{{plant_price}}</p> <div v-if="selected.plant.variations.length > 0 "> <select v-model="selected.plant.selected_ ...

It is not possible to rely on Vuex to retrieve data for every single component

Is it possible to fetch data in App.vue and pass the value to this.store.data so that it can be used for all other components? The issue I am facing is that when I click on the link (router-link), the function inside the components runs before fetching dat ...

Overflow and contain a flex item within another flex item

I am facing a challenge where I need to prevent a flex-child of another flex-child from overflowing the parent, ensuring it does not exceed the screen height or the height of the parent (which is also a flex-child). Here's the CodePen link for refere ...

Maintaining checked items in their original state while searching for another one in ion-searchbar can be achieved by properly handling

My goal is to maintain the checked items as checked when searching for another item in ion-searchbar. While I have managed to keep the checked items, the checkmark icon does not stay checked. What I aim for is to retain the checked state of all food items ...

What is the process for specifying a specific font family in CSS?

Despite setting my text to font-family: Times New Roman, I'm seeing a different font in my Chrome browser. Here is the relevant HTML code: <p style="color:#F17141;font-size: 120px; font-family: &quot;Times New Roman&quot; ; font-weight: b ...

Parent container fails to center align when window is resized

When I resize my window to a smaller size, like that of a smartphone screen, the three inner divs (colored red, green, and blue - .left, .center, and .right) do not align in the center. Please refer to the screenshot attached for clarity. My goal is to ens ...

equal child width as parent column container

I'm in the process of developing a custom dropdown search bar that presents results as the user enters text. One issue I'm facing is that the list of results must have a position: absolute. This causes it to disregard the width of the parent col ...

Guidance on using an array to filter an object in Javascript

Looking at the object structure in Chrome Dev Tools, it appears like this: obj: { 1: {...}, 2: {...}, 3: {...}, 4: {...}, 5: {...}, } On the other hand, there is a simple array as well: arr: [1,3,5,7] The goal here is to filter the object bas ...

There was an issue with the v-on handler: "An error occurred because it was unable to read properties of an undefined value (specifically 'input')."

Can anyone help me? When I click the icon inside the avatar, I want to select a file. But I'm getting an error: Error in v-on handler: "TypeError: Cannot read properties of undefined (reading 'input'). Could anyone help me? <v-row v-for=" ...

Despite being present in the node_modules folder, the ag-grid-vue module appears to be missing

Currently, I am diligently following the Vue.js AgGrid getting started tutorial step by step: https://www.ag-grid.com/vuejs-grid/ However, upon adding the <script> section and saving my progress, an error promptly appears: ERROR Failed to compile ...

Styling the elements that are next to each other using CSS

Hey there, what if I have HTML elements structured like this: <div> <div>Name</div> <div><input type="text" class="check"> <div>Age</div> <div><input type="number" class="check"></div> ...

What is the best way to use arrow functions in VueJS?

Here is how my method in vue currently looks: methods: { setDate: async function () { console.log(this.modal) } } I want to convert it into an arrow function. I tried the following approach: methods: { setDate: async () => { ...

Keep your Vue site/PWA fresh and up-to-date with automatic updates for every new release

My Vue application serves both as a browser app and as a PWA. I want to make sure that users always have the most recent version of the app whenever updates are pushed to the server. My tech stack includes Nginx, Django, vue-cli, and @vue/cli-plugin-pwa. ...

Tips for using jQuery to add or append a class to the final li element

I am currently struggling to understand why the last child of the li element is not being inserted into the last li. This module functions as a treeview, and my main objective is to add a class to the last li. Let me demonstrate with some sample code and ...

Developing an Easy-to-Use PDF Popup Interface

I need a simple PDF modal where I can input the text: 'The sky is blue' and have it link to a PDF that opens up when clicked. I found a similar sample online, but it includes an image plus an image modal that pops up. I want to replace the imag ...