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

"Implementing CSS inline styles for improved appearance on a Safari browser when visiting

Having some trouble using CSS to create rounded corners on my Facebook fan page. Everything works fine except for Safari browser. I've tried inline styles and a separate stylesheet, but no luck. Any help would be appreciated. My CSS for rounded corne ...

Styling with CSS to ensure divs are displayed in two rows

Check out this example I found: https://jsfiddle.net/n3qs0wke/ .wrapper { max-width: 600px; border: 1px solid #333; } .big-div { min-width: 212px; min-height: 212px; max-width: 212px; max-height: 212px; display: inline-flex; ...

What is the best way to display the user login in the center of a page with all necessary controls

Challenge How can the user login be displayed in the center of the page? Additional Information The current layout displays user login data on the left-hand side. I am looking to have the user login data centralized on the page, which includes labels f ...

Forecast the width of an element using JavaScript

Is there a way to display tab elements on a webpage without them automatically wrapping into a new row if there are too many? Instead, I would like an arrow icon to appear at the end of the tabs so users can cycle between tab groups. The challenge is that ...

Adjust the size of an HTML image for printing using a JavaScript window.print() function

On my website, I have a print option set up where specific elements are hidden using @media. How can I adjust the size of an image within the @media query when my JavaScript print function is triggered? I am able to modify a regular div element easily, but ...

The Bootstrap dropdown vanishes before I can even click on it

I recently encountered an issue with my Bootstrap dropdown menu on my website. After making some changes, the dropdown disappears after 1-2 seconds when viewed on a mobile phone. Interestingly, the original Bootstrap menu works fine, but not my customized ...

Manipulating certain attributes of a CSS class for a specific division element

I'm working with a div that has a CSS class applied to it. The class declares the height as x pixels, but I actually want my div to be y pixels high. Is there a way to override the height parameter of the CSS without making changes to the CSS file? ...

Guide on fetching data using Index Number in Vue Js from an Api?

Query 1: Is there a method in Vue Js to retrieve data based on Index Number from an API? Q2. I am facing an issue where using {{item.title}} results in an error stating that the title is undefined... Any suggestions for resolving this problem? ...

Executing the beforeRouteLeave navigation guard on a vue component for testing purposes

I am facing a challenge with unit testing the routing behavior of a vue component using jest. Specifically, when navigating away from the component, the 'beforeRouteLeave' guard in Vue-router is not triggering during testing, even though it works ...

Guide to positioning elements in CSS

I'm struggling to get all the elements to align in a single row. I've tried using display: inline-block, but it's not working as expected. I'm working with DataTables and I want the button images to be aligned with the page number box. ...

"Utilizing a background image within a component to completely cover the entirety

Hey everyone, I need some help with a confusing issue. I'm currently working on an AngularJs project and I am struggling to set a background image for a specific component without affecting the entire application. My goal is to have the image cover t ...

Is there a way to keep the spotlight/shadow position fixed in relation to the horizontal center, regardless of the zoom level?

After working on the following code snippet, I've managed to keep the spotlight centered horizontally, except when the zoom factor is too high. This means that when I zoom in, the spot light no longer remains centered in the horizontal view port. It& ...

Creating styles for different screen sizes including mobile devices, low-resolution desktops, and high-resolution desktops

One of the techniques I'm using involves analyzing both the horizontal and vertical screen dimensions to differentiate between mobile devices and desktops, as well as distinguish high-resolution from low-resolution desktop displays. In order to achie ...

Assigning active classes based on the href attributes of child <a> tags

I've created a navigation structure as follows: <ul class="page-sidebar-menu"> <li class="menu"> <a href=""> <span class="title">Menu Item</span> <span class="arrow"></span> < ...

What's the best way to use Flexbox to center a component with a "fixed width"?

As I work on my component, here is the code snippet from my highest parent, App.component: app.component.html <div class="wrapper_flex"> <div style="width:400px; background-color: pink;"></div> <div style=& ...

What is the best way to ensure that the closing </a> tag remains on the same line while all other tags are placed on a new line in a Vue

vue/html-closing-bracket-newline is causing an unexpected space after the link text, leading to conflicts and errors in prettier and eslint. I am searching for a straightforward configuration change to resolve this issue without resorting to individual lin ...

what distinguishes CSS properties for Id versus class selectors

Currently in the process of creating my inaugural website, which consists of just four pages. Each page follows the standard layout structure with a header, navigation bar, content division (div id="content"), and footer. As I delve into adding CSS proper ...

Unable to apply CSS styles to a form input using Ajax

Alright, so I've got this Ajax form that successfully fetches the data and displays it in #register_msg. However, I'm also trying to apply some styles to the input forms. Here's my Ajax code, along with the form. But for some reason, the st ...

Utilizing Vue Router to leverage specific getters from Vuex

I am currently facing an issue with accessing the authenticated user in my Vuex store within my router.js file. { path: '/admin/login', name: 'admin-login', component: AdminLogin, beforeEnter(to, from, next) { console.log(s ...

How to turn off code splitting (chunks) in Vue.js and Vite.js

Is there a way to turn off chunking in Vue.js and Vite.js when building a project using Rollup.js? I attempted the following approach, but it did not work for me: export default defineConfig({ build: { rollupOptions: { output: { ...