Skip to content

Commit bc67ae9

Browse files
authored
Merge pull request #51 from devforth/only-one-select-active
AFCL Select component should not allow to open dropdowns for 2 instances at the same time
2 parents 819b0d1 + e4d62e8 commit bc67ae9

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

adminforth/spa/src/afcl/Select.vue

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div class="relative inline-block afcl-select">
2+
<div class="relative inline-block afcl-select" ref="internalSelect">
33
<div class="relative">
44
<input
55
ref="inputEl"
@@ -89,7 +89,6 @@ import { ref, computed, onMounted, onUnmounted, watch, type Ref } from 'vue';
8989
import { IconCaretDownSolid } from '@iconify-prerendered/vue-flowbite';
9090
import { useElementSize } from '@vueuse/core'
9191
92-
9392
const props = defineProps({
9493
options: Array,
9594
modelValue: {
@@ -123,6 +122,7 @@ const dropdownStyle = ref<{ top?: string; }>({
123122
});
124123
125124
const selectedItems: Ref<any[]> = ref([]);
125+
const internalSelect = ref<HTMLElement | null>(null);
126126
127127
function inputInput() {
128128
if (!props.multiple && selectedItems.value.length) {
@@ -147,15 +147,12 @@ function updateFromProps() {
147147
}
148148
149149
function inputClick() {
150-
if (props.isReadonly) {
151-
return;
152-
}
153-
if (!showDropdown.value) {
154-
showDropdown.value = true;
155-
} else {
156-
if (!search.value) {
157-
showDropdown.value = false;
158-
}
150+
if (props.isReadonly) return;
151+
// Toggle local dropdown
152+
showDropdown.value = !showDropdown.value;
153+
// If the dropdown is about to close, reset the search
154+
if (!showDropdown.value && !search.value) {
155+
search.value = '';
159156
}
160157
}
161158
@@ -198,10 +195,12 @@ const filteredItems = computed(() => {
198195
);
199196
});
200197
201-
const handleClickOutside = (event) => {
202-
if (!event.target.closest('.afcl-select')) {
198+
199+
const handleClickOutside = (event: MouseEvent) => {
200+
const targetEl = event.target as HTMLElement | null;
201+
const closestSelect = targetEl?.closest('.afcl-select');
202+
if (closestSelect !== internalSelect.value)
203203
showDropdown.value = false;
204-
}
205204
};
206205
207206
const addClickListener = () => {
@@ -228,7 +227,7 @@ const toogleItem = (item) => {
228227
if (!props.multiple && search.value) {
229228
search.value = '';
230229
}
231-
230+
232231
const list = selectedItems.value.map(item => item.value);
233232
const updValue = list.length ? list : null;
234233
let emitValue;
@@ -238,13 +237,10 @@ const toogleItem = (item) => {
238237
emitValue = updValue;
239238
}
240239
emit('update:modelValue', emitValue);
241-
242240
};
243241
244-
245242
onUnmounted(() => {
246243
removeClickListener();
247244
});
248245
249-
250246
</script>

0 commit comments

Comments
 (0)