Skip to content

Commit

Permalink
fix: Fixed dropdown toggle behavior when search is absent
Browse files Browse the repository at this point in the history
Addressed an issue where toggling the dropdown in the component was not
working correctly when the search input was not present
  • Loading branch information
Voral committed Dec 29, 2023
1 parent 8c5869f commit 08b1f26
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
11 changes: 11 additions & 0 deletions demo/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ const links = computed(() => {

<template>
<div>
<div class="section">
<h2 class="title">Autocomplete search text, multiple</h2>
<v-select
v-model="selectedOptions"
:auto-select="true"
:options="options"
:multiple="true"
>
<template #search>test</template>
</v-select>
</div>
<div class="section">
<h2 class="title">Autocomplete search text, multiple</h2>
<v-select
Expand Down
11 changes: 3 additions & 8 deletions src/components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1201,31 +1201,26 @@ export default {
if (targetIsNotSearch) {
event.preventDefault()
}
// don't react to click on deselect/clear buttons,
// they dropdown state will be set in their click handlers
const ignoredButtons = [
...(this.deselectButtons || []),
...([this.$refs['clearButton']] || []),
]
if (
this.searchEl === undefined ||
ignoredButtons
if (ignoredButtons
.filter(Boolean)
.some((ref) => ref.contains(event.target) || ref === event.target)
) {
event.preventDefault()
return
}
if (this.open && targetIsNotSearch) {
this.searchEl.blur()
if (!!this.searchEl) this.searchEl.blur()
} else if (this.open && !targetIsNotSearch && !this.searchable) {
this.open = false
} else if (!this.disabled) {
this.open = true
this.searchEl.focus()
if (!!this.searchEl) this.searchEl.focus()
}
},
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Deselecting.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {it, describe, expect, vi} from 'vitest'
import {mountDefault, selectWithProps} from '#/helpers.js'

const preventDefault = vi.fn()
function clickEvent(currentTarget) {
return {currentTarget, preventDefault, target:currentTarget}
}
describe('Removing values', () => {
it('can remove the given tag when its close icon is clicked', async () => {
const Select = selectWithProps({multiple: true})
Expand Down Expand Up @@ -156,5 +160,19 @@ describe('Removing values', () => {
Select.find('button.vs__clear').attributes().disabled
).toBeDefined()
})

it('toggleDropdown should prevent default when clicking on ignored buttons', async () => {
// Создаем Vue приложение
const Select = selectWithProps({
options: ['foo', 'bar'],
modelValue: 'foo',
});
Select.vm.$data._value = 'foo';
Select.vm.open = true
const event = clickEvent(Select.vm.$refs.clearButton);
Select.vm.toggleDropdown(event)
expect(preventDefault.mock.calls).toHaveLength(2);
expect(preventDefault).toHaveBeenCalledWith();
});
})
})

0 comments on commit 08b1f26

Please sign in to comment.