Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiselect: Menu closes on select #4861

Open
Rakasch opened this issue Nov 22, 2023 · 6 comments
Open

Multiselect: Menu closes on select #4861

Rakasch opened this issue Nov 22, 2023 · 6 comments
Labels
Resolution: Needs Revision The pull request can't be merged. Conflicts need to be corrected or documentation and code updated.

Comments

@Rakasch
Copy link
Contributor

Rakasch commented Nov 22, 2023

Describe the bug

I want to display the selected options at the top of the list.
I therefore sort the selected options to the top.

If I now click on something further down in the menu, it is automatically sorted to the top.
At the same time, the menu closes by mistake. The menu should remain open.

Cause of the error:
Since I use the virtualscroller, not all options are rendered in the html.
Therefore the outsideClickListener does not find the clicked option and thinks it was a click outside the menu.
The menu is then incorrectly closed.

Reproducer

https://stackblitz.com/edit/lictaz?file=src%2FApp.vue

PrimeVue version

3.41.0

Vue version

3.x

Language

ES6

Build / Runtime

Vite

Browser(s)

No response

Steps to reproduce the behavior

Open Menu
Scroll Down
Select an option
=> Menu will close

Expected behavior

Menu should stay open as long as I dont click outside.

@Rakasch Rakasch added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 22, 2023
@xiaodong2008
Copy link
Contributor

You don't need to add to much options, just need to scroll and select then the problem can reproduce.

Array.from({ length: 10 }, (_, i) => ({ name: `City #${i}`, id: i }))

@xiaodong2008
Copy link
Contributor

xiaodong2008 commented Nov 22, 2023

This problem is caused by components/lib/multiselect/MultiSelect.vue -> default.methods.bindOutsideClickListener and method isOutsideClicked()

bindOutsideClickListener() {
    if (!this.outsideClickListener) {
        this.outsideClickListener = (event) => {
            if (this.overlayVisible && this.isOutsideClicked(event)) {
                this.hide();
            }
        };
        document.addEventListener('click', this.outsideClickListener);
    }
}
isOutsideClicked(event) {
    return !(this.$el.isSameNode(event.target) || this.$el.contains(event.target) || (this.overlay && this.overlay.contains(event.target)));
}

The function isOutsideClicked should add one more expression to determine is className equal to p-multiselect-item

xiaodong2008 added a commit to xiaodong2008/primevue that referenced this issue Nov 22, 2023
@tugcekucukoglu tugcekucukoglu added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Nov 27, 2023
@MaXal
Copy link

MaXal commented Apr 10, 2024

Simple reproducer: https://stackblitz.com/edit/hmwkta?file=src/App.vue,package.json

<template>
  <div class="card flex justify-content-center">
    <MultiSelect
      v-model="selectedCities"
      :options="cities"
      optionLabel="name"
      placeholder="Select Cities"
      :maxSelectedLabels="3"
      class="w-full md:w-20rem"
    />
  </div>
</template>

<script setup>
import { computed, ref } from "vue"

const selectedCities = ref()

const cities = computed(() => {
  const cities = ref([
    { name: "New York", code: "NY" },
    { name: "Rome", code: "RM" },
    { name: "London", code: "LDN" },
    { name: "Istanbul", code: "IST" },
    { name: "Paris", code: "PRS" },
  ])
  return cities.value.sort((a, b) => {
    if (selectedCities.value.includes(a)) return -1
    if (selectedCities.value.includes(b)) return 1
    return 0
  })
})
</script>

@xiaodong2008
Copy link
Contributor

I’ve tested #4864 and this pr should fix this issue.

MaXal added a commit to MaXal/primevue that referenced this issue Apr 29, 2024
MaXal added a commit to MaXal/primevue that referenced this issue Apr 29, 2024
MaXal added a commit to MaXal/primevue that referenced this issue Apr 29, 2024
@tugcekucukoglu
Copy link
Member

Cannot replicate issue with the latest version. How can replicate the issue?

@tugcekucukoglu tugcekucukoglu added Resolution: Needs Revision The pull request can't be merged. Conflicts need to be corrected or documentation and code updated. and removed Type: Bug Issue contains a bug related to a specific component. Something about the component is not working labels May 16, 2024
@MaXal
Copy link

MaXal commented May 23, 2024

@tugcekucukoglu Thank you for looking into the issue. The issue is still reproducible with V4: https://stackblitz.com/edit/ivkrzp?file=src%2FApp.vue

2024-05-23 11 23 28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Needs Revision The pull request can't be merged. Conflicts need to be corrected or documentation and code updated.
Projects
None yet
Development

No branches or pull requests

4 participants