Issue with blur event functionality on custom multi-select Vue component not functioning as expected

The blur event seems to be malfunctioning. It works perfectly when I click anywhere within the component, except for when I click on the input field. If I click inside the input field and then outside the component, the blur event fails to trigger, preventing the options list from closing. How can I adjust the blur event on the outer div to function properly after clicking on the input field and then outside the component? (* Note that the blur event should not be triggered if I click within the components list as it is still considered part of the component, hence placing a blur event on the input field alone won't suffice)

    <template>
  <div class="flex flex-col relative w-full">
    <span v-if="label" class="font-jost-medium mb-2">{{ label }}</span>
    <div>
      <div @blur="showOptions = false" :tabindex="tabIndex">
        <div
          class="border border-[#EAEAEA] bg-white rounded-md flex flex-col w-full"
        >
          <div
            v-if="selectedOptions.length"
            class="flex flex-wrap px-4 py-2 border-b gap-2"
          >
            <div
              v-for="option in selectedOptions"
              class="border bg-secondary rounded-full py-1 px-2 flex items-center"
            >
              <span>{{ option.text }}</span>
              <vue-feather
                type="x"
                class="h-3 w-3 ml-1.5 cursor-pointer"
                @click="onDeleteOption(option)"
              />
            </div>
          </div>
          <div
            class="flex flex-row justify-end items-center px-4 cursor-pointer"
            :class="selectedOptions.length ? 'py-2' : 'p-4'"
            @click="showOptions = !showOptions"
          >
            <MagnifyingGlassIcon class="h-5 w-5 mr-2" />
            <input
              class="focus:outline-0 w-full"
              type="text"
              v-model="searchInput"
            />
            <vue-feather type="chevron-down" class="h-5 w-5" />
          </div>
        </div>
        <div v-if="showOptions && optionsMap.length" class="options-list">
          <ul role="listbox" class="w-full overflow-auto">
            <li
              class="hover:bg-primary-light px-4 py-2 rounded-md cursor-pointer"
              role="option"
              v-for="option in optionsMap"
              @mousedown="onOptionClick(option)"
            >
              {{ option.text }}
            </li>
          </ul>
        </div>
        <div
          id="not-found"
          class="absolute w-full italic text-center text-inactive-grey"
          v-else-if="!optionsMap.length"
        >
          No records found
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, watch } from "vue";
import { IconNameTypes } from "@/types/enums/IconNameTypes";
import { AppIcon } from "@/components/base/index";
import { MagnifyingGlassIcon } from "@heroicons/vue/24/outline";

export default defineComponent({
  name: "AppAutocomplete",
  components: {
    AppIcon,
    MagnifyingGlassIcon,
  },
  props: {
    modelValue: {
      type: String,
    },
    label: {
      type: String,
      default: "",
    },
    tabIndex: {
      type: Number,
      default: 0,
    },
    options: {
      type: Array as PropType<{ text: string; value: string }[]>,
      required: true,
    },
  },
  setup(props, { emit }) {
    const showOptions = ref(false);

    const optionsMap = ref(props.options);
    const selectedOptions = ref<{ text: string; value: string }[]>([]);
    const searchInput = ref("");
    watch(searchInput, () => {
      optionsMap.value = props.options.filter((option1) => {
        return (
          !selectedOptions.value.some((option2) => {
            return option1.text === option2.text;
          }) &&
          option1.text.toLowerCase().includes(searchInput.value.toLowerCase())
        );
      });
      sortOptionsMapList();
    });

    const onOptionClick = (option: { text: string; value: string }) => {
      searchInput.value = "";
      selectedOptions.value.push(option);
      optionsMap.value = optionsMap.value.filter((option1) => {
        return !selectedOptions.value.some((option2) => {
          return option1.text === option2.text;
        });
      });
      sortOptionsMapList();
      emit("update:modelValue", option.value);
    };

    const onDeleteOption = (option: { text: string; value: string }) => {
      selectedOptions.value = selectedOptions.value.filter((option2) => {
        return option2.text !== option.text;
      });
      optionsMap.value.push(option);
      sortOptionsMapList();
    };

    const sortOptionsMapList = () => {
      optionsMap.value.sort(function (a, b) {
        return a.text.localeCompare(b.text);
      });
    };
    sortOptionsMapList();

    document.addEventListener("click", () => {
      console.log(document.activeElement);
    });

    return {
      showOptions,
      optionsMap,
      searchInput,
      selectedOptions,
      IconNameTypes,
      onOptionClick,
      onDeleteOption,
    };
  },
});
</script>

<style scoped lang="scss">
.options-list,
#not-found {
  box-shadow: 0 0 50px 0 rgb(19 19 28 / 12%);

  @apply border border-[#EAEAEA] rounded-md p-4 mt-1 absolute bg-white z-10 w-full;
}
ul {
  @apply max-h-52 #{!important};
}
</style>

Answer №1

blur does not propagate to higher elements, thus it does not reach the parent div. The suitable event for your requirement is focusout

<div @focusout="showOptions = false" :tabindex="tabIndex">

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

Instructions for updating an entire object within an array using Mongoose

I am currently working with Mongoose and Typescript, and my document structure is as follows: { _id: 1, id: "a", markets: [{ type: "car", price: 10000}, {type: "toy", price: 10},...] ...

What is the best way to locate a DOM element using jQuery after I have repositioned it on the page?

I designed a page that consists of two sections, with boxes in each. The functionality I implemented allows users to click on a box in either section and move it to the other section. Initially, this feature works smoothly for the first movement. Theoretic ...

Pass a JSON-encoded PHP variable to JavaScript using the onblur event

As I work on my PHP page, I aim to create a dynamic set of input fields when a user moves away from a specific existing input text field. Essentially, my goal is to trigger the generation of additional input fields by using the onblur event handler to pass ...

Loading HTML and jQuery dynamically using AJAX

I am struggling to access a child element of HTML that is loaded dynamically through AJAX. Unfortunately, it's not working as expected. $.get('test.html', function(data) { var content = $('#content', data).html(); conso ...

Python code displays output directly on an HTML webpage

I have created a basic Python chatbot that performs calculations and responds to user input. The chatbot continuously runs in Python, checking previous responses for context. I want to make this chatbot available online and achieve the following: Send us ...

Challenges with Dynamic Imports in Django Vite and Vue 3

Why isn't the import below working properly with Vue 3? import Home from `./${store.data}/Home.vue`; The console.log(${store.data}) correctly displays the folder name as folder_1. Interestingly, the following import functions perfectly: import Home ...

Having trouble getting the toggleClass function to work properly?

I'm facing an issue with some straightforward code that I've written. $(function() { $("#tren").click(function() { $("#trens").toggleClass("show"); }); }); .show { color: "red"; } <ul> <li id="tren">Some text</li> < ...

Anchoring links on a webpage that provide users with a clear indication of their current position within the page

In the scenario of a single-page website with various sections (divs) and a header containing links to different anchors on the page, there is a desire to have an indicator highlight which anchor the user is currently viewing. An example of this can be s ...

Retrieve all properties associated with the current instance in the Angular 2 controller

I am looking to assign class variables with values from session storage if they exist, otherwise the variable will retain its default value initialized in ngOnInit. private getTableSessionItems = () => { var tSession = JSON.parse(sessionStorage.g ...

Tips for splitting lengthy text into multiple lines in Vue

Vue is being used to display a line which appears lengthy when displayed in one line. I'm interested in splitting this long line into multiple lines automatically. Can someone guide me on how this can be achieved? <span class="text-xs"> ...

Prevent Copying and Pasting within the Text Editor

@if (Model.CanMaintainNcrLineManagement) { <tr> <td>@Html.TextAreaFor(model => model.Description, new { id = "txArNcrLineDescriptionValue", @style = "height:520px" })</td> </tr> } else { <tr class="read-only-editor"> &l ...

Using JQuery Validation Engine to perform validation with an AJAX call

I am currently in the process of verifying if a specific record already exists in the database before adding a new entry. ajax Call "ajaxRecordExistsCall": { "url": "Controller?action=GET_LIST", "extraDataDynamic": ...

A guide on triggering a function in Angular 6 when scrolling up or down

Is there a way to trigger a function when a user scrolls up or down in an Angular 6 project? I want to implement a feature similar to fullpage.js in my Angular 6 application. I initially attempted to achieve this using Angular animations, but without succ ...

Unable to deactivate button with jQuery in Django

Having some trouble with a simple jQuery function that should disable a Button if the selected dropdown value is blank. Can't figure out why it's not working. Here's the snippet of HTML code: <form action="{% url 'select_controller ...

Using PHP's modulus operator, you can dynamically apply CSS classes to a grid of images based on alternating patterns

Exploring the use of modulus to dynamically apply a CSS class to images displayed in a grid under different scenarios: Situation 1 In this situation, the goal is to target and apply a CSS class to middle images marked with *. Since the number of images ...

What is the method or variable called "afterShow" used for in FancyBox V4 and how does it differ from its counterpart in JQuery-FancyBox V3?

We previously utilized the V3 edition of Fancybox, incorporating our increaseImageClicks and increaseVideoClicks functions within its afterShow function: /* FANCYBOX OLD (https://web.archive.org/web/20210325170940/https://fancyapps.com/fancybox/3/docs/): * ...

Having trouble with the chaining of AJAX calls using Promises?

I am currently attempting to send a POST request (technically a DELETE) to a php page called delete_post.ajax.php. This request takes data from my AJAX call and utilizes it to delete an item from the database. After deletion, I want my AJAX to then send a ...

What could be causing the `daterange` to display an incorrect date?

While developing my ASP MVC application, I implemented a daterange picker which worked perfectly. However, when I moved the application to production on IIS, the daterange stopped working and displayed an "invalid date" error. https://i.sstatic.net/J6Ylf. ...

Leveraging the state feature in react-router-dom's Redirect

I have been utilizing the instructions provided by react-router-dom's Redirect in my project. Currently, I have a component that displays the code snippet below: return <Redirect to={{ pathname: '/login', state: { previousPath: path } } ...

React is struggling to dynamically update text content using button click events

As a beginner in the world of React, Nodejs, and JavaScript, I am exploring onClick events to dynamically change text by clicking buttons. In my practice project, I have an input type="checkbox" that toggles the text between bold and normal style ...