Skip to content

Commit 66517a3

Browse files
committed
add selectID prop and integrate dropdown store for improved select management
1 parent 5217a93 commit 66517a3

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

adminforth/spa/src/afcl/Select.vue

Lines changed: 28 additions & 17 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" :data-select-id="internalID">
33
<div class="relative">
44
<input
55
ref="inputEl"
@@ -89,6 +89,7 @@ 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+
import { useDropdownStore } from '@/stores/dropdown';
9293
9394
const props = defineProps({
9495
options: Array,
@@ -107,6 +108,10 @@ const props = defineProps({
107108
type: Boolean,
108109
default: false,
109110
},
111+
selectID: {
112+
type: String,
113+
required: false,
114+
},
110115
});
111116
112117
const emit = defineEmits(['update:modelValue']);
@@ -118,11 +123,14 @@ const dropdownEl = ref<HTMLElement | null>(null);
118123
const { height: dropdownHeight } = useElementSize(dropdownEl);
119124
const isTop = ref<boolean>(false);
120125
126+
const dropdownStore = useDropdownStore(); // Access the store
127+
121128
const dropdownStyle = ref<{ top?: string; }>({
122129
top: "0px",
123130
});
124131
125132
const selectedItems: Ref<any[]> = ref([]);
133+
const internalID = props.selectID || `afcb-select-${Math.random().toString(36).substring(7)}`
126134
127135
function inputInput() {
128136
if (!props.multiple && selectedItems.value.length) {
@@ -147,17 +155,24 @@ function updateFromProps() {
147155
}
148156
149157
function inputClick() {
150-
if (props.isReadonly) {
151-
return;
152-
}
153-
if (!showDropdown.value) {
154-
showDropdown.value = true;
155-
} else {
156-
if (!search.value) {
158+
if (props.isReadonly) return;
159+
160+
if (dropdownStore.openSelectID !== internalID)
161+
dropdownStore.setOpenSelectID(internalID);
162+
163+
showDropdown.value = !showDropdown.value;
164+
if (!showDropdown.value && !search.value)
165+
search.value = '';
166+
}
167+
168+
// Watch for store changes to close this dropdown if it's not the active one
169+
watch(
170+
() => dropdownStore.openSelectID,
171+
(newID) => {
172+
if (newID !== internalID && showDropdown.value)
157173
showDropdown.value = false;
158-
}
159174
}
160-
}
175+
);
161176
162177
watch(
163178
() => ({ show: showDropdown.value, dropdownHeight: dropdownHeight.value }),
@@ -198,10 +213,9 @@ const filteredItems = computed(() => {
198213
);
199214
});
200215
201-
const handleClickOutside = (event) => {
202-
if (!event.target.closest('.afcl-select')) {
216+
const handleClickOutside = (event: MouseEvent) => {
217+
if (!event.target || !(event.target as HTMLElement).closest('.afcl-select'))
203218
showDropdown.value = false;
204-
}
205219
};
206220
207221
const addClickListener = () => {
@@ -228,7 +242,7 @@ const toogleItem = (item) => {
228242
if (!props.multiple && search.value) {
229243
search.value = '';
230244
}
231-
245+
232246
const list = selectedItems.value.map(item => item.value);
233247
const updValue = list.length ? list : null;
234248
let emitValue;
@@ -238,13 +252,10 @@ const toogleItem = (item) => {
238252
emitValue = updValue;
239253
}
240254
emit('update:modelValue', emitValue);
241-
242255
};
243256
244-
245257
onUnmounted(() => {
246258
removeClickListener();
247259
});
248260
249-
250261
</script>

0 commit comments

Comments
 (0)