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

feat(SelectMenu): add loading state on async search #520

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/runtime/components/forms/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
v-slot="{ active, selected, disabled: optionDisabled }"
:key="index"
as="template"
:class="isSearching && 'opacity-50'"
:value="valueAttribute ? option[valueAttribute] : option"
:disabled="option.disabled"
>
Expand Down Expand Up @@ -104,6 +105,11 @@
</div>
</li>
</component>
<p v-else-if="searchable && !filteredOptions.length && isSearching" :class="uiMenu.option.empty">
<slot name="option-empty-loading" :query="query">
{{ query ? `Searching for "${query}"` : "Searching for results..." }}
</slot>
</p>
<p v-else-if="searchable && query && !filteredOptions.length" :class="uiMenu.option.empty">
<slot name="option-empty" :query="query">
No results for "{{ query }}".
Expand Down Expand Up @@ -394,10 +400,20 @@ export default defineComponent({
)
})

const debouncedSearch = typeof props.searchable === 'function' ? useDebounceFn(props.searchable, props.debounce) : undefined
const isSearching = ref(false)
const debouncedSearch = useDebounceFn(async (q: string) => {
if (typeof props.searchable === 'function') {
try {
return await props.searchable(q)
} finally {
isSearching.value = false
}
}
}, props.debounce)

const filteredOptions = computedAsync(async () => {
if (props.searchable && debouncedSearch) {
if (typeof props.searchable === 'function' && debouncedSearch) {
isSearching.value = true
return await debouncedSearch(query.value)
}

Expand Down Expand Up @@ -447,6 +463,7 @@ export default defineComponent({
wrapperClass,
// eslint-disable-next-line vue/no-dupe-keys
selectClass,
isSearching,
leadingIconName,
leadingIconClass,
leadingWrapperIconClass,
Expand Down