Skip to content

Commit a9d2822

Browse files
committed
feat: add search debounce functionality to Select component
1 parent 898db4d commit a9d2822

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

adminforth/spa/src/afcl/Select.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<label v-if="!$slots.item" :for="item.value">{{ item.label }}</label>
5353
</div>
5454
<div v-if="!filteredItems.length" class="px-4 py-2 cursor-pointer text-lightDropdownOptionsText dark:text-darkDropdownOptionsText">
55-
{{ options?.length ? $t('No results found') : $t('No items here') }}
55+
{{ $t('No results found') }}
5656
</div>
5757

5858
<div v-if="$slots['extra-item']" class="px-4 py-2 dark:text-gray-400">
@@ -148,6 +148,10 @@ const props = defineProps({
148148
type: Boolean,
149149
default: false,
150150
},
151+
searchDebounceMs: {
152+
type: Number,
153+
default: 300,
154+
},
151155
});
152156
153157
const emit = defineEmits(['update:modelValue', 'scroll-near-end', 'search']);
@@ -165,14 +169,20 @@ const dropdownStyle = ref<{ top?: string; }>({
165169
166170
const selectedItems: Ref<any[]> = ref([]);
167171
const internalSelect = ref<HTMLElement | null>(null);
172+
let searchDebounceHandle: ReturnType<typeof setTimeout> | null = null;
168173
169174
function inputInput() {
170175
if (!props.multiple && selectedItems.value.length) {
171176
selectedItems.value = [];
172177
emit('update:modelValue', null);
173178
}
174179
if (!props.searchDisabled) {
175-
emit('search', search.value);
180+
if (searchDebounceHandle) {
181+
clearTimeout(searchDebounceHandle);
182+
}
183+
searchDebounceHandle = setTimeout(() => {
184+
emit('search', search.value);
185+
}, props.searchDebounceMs);
176186
}
177187
}
178188
@@ -329,6 +339,10 @@ onUnmounted(() => {
329339
if (props.teleportToBody) {
330340
window.removeEventListener('scroll', handleScroll, true);
331341
}
342+
if (searchDebounceHandle) {
343+
clearTimeout(searchDebounceHandle);
344+
searchDebounceHandle = null;
345+
}
332346
});
333347
334348
const getDropdownPosition = computed(() => {

adminforth/spa/src/components/ColumnValueInput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<Select
1818
v-else-if="column.foreignResource"
1919
ref="input"
20-
:key="`${column.name}-${(columnOptions[column.name] || []).length}`"
20+
:key="`select-${column.name}-${source}-${column.foreignResource?.name || column.foreignResource?.table || ''}`"
2121
class="w-full min-w-24"
2222
:options="columnOptions[column.name] || []"
2323
:searchDisabled="!column.foreignResource.searchableFields"

0 commit comments

Comments
 (0)