fix(components): [autocomplete] fetch-suggestions support AsyncFunction (#7315)

* fix: promise fetch bug

* fix: fetchSuggestions is AsyncFunction

* refactor: improve promise

* fix: del unuse

Co-authored-by: xiaochenchen <xiaochen.chen@igg.com>
Co-authored-by: 三咲智子 <sxzz@sxzz.moe>
This commit is contained in:
Xc 2022-04-27 02:14:45 +08:00 committed by GitHub
parent 1037eb75b4
commit 28e87550e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -11,7 +11,7 @@ vi.useFakeTimers()
const _mount = ( const _mount = (
payload = {}, payload = {},
type: 'fn-cb' | 'fn-promise' | 'fn-arr' | 'arr' = 'fn-cb' type: 'fn-cb' | 'fn-promise' | 'fn-arr' | 'fn-async' | 'arr' = 'fn-cb'
) => ) =>
mount({ mount({
setup() { setup() {
@ -46,6 +46,11 @@ const _mount = (
case 'fn-promise': case 'fn-promise':
return (queryString: string) => return (queryString: string) =>
Promise.resolve(filterList(queryString)) Promise.resolve(filterList(queryString))
case 'fn-async':
return async (queryString: string) => {
await Promise.resolve()
return filterList(queryString)
}
case 'fn-arr': case 'fn-arr':
return (queryString: string) => filterList(queryString) return (queryString: string) => filterList(queryString)
case 'arr': case 'arr':
@ -166,6 +171,21 @@ describe('Autocomplete.vue', () => {
expect(target.suggestions.length).toBe(4) expect(target.suggestions.length).toBe(4)
}) })
test('fetchSuggestions with fn-async', async () => {
const wrapper = _mount({ debounce: 10 }, 'fn-async')
await nextTick()
await wrapper.find('input').trigger('focus')
vi.runAllTimers()
await nextTick()
await nextTick()
const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
typeof Autocomplete
>
expect(target.suggestions.length).toBe(4)
})
test('fetchSuggestions with fn-arr', async () => { test('fetchSuggestions with fn-arr', async () => {
const wrapper = _mount({ debounce: 10 }, 'fn-arr') const wrapper = _mount({ debounce: 10 }, 'fn-arr')
await nextTick() await nextTick()

View File

@ -96,7 +96,6 @@ import {
ref, ref,
useAttrs as useCompAttrs, useAttrs as useCompAttrs,
} from 'vue' } from 'vue'
import { isPromise } from '@vue/shared'
import { debounce } from 'lodash-unified' import { debounce } from 'lodash-unified'
import { onClickOutside } from '@vueuse/core' import { onClickOutside } from '@vueuse/core'
import { useAttrs, useNamespace } from '@element-plus/hooks' import { useAttrs, useNamespace } from '@element-plus/hooks'
@ -162,7 +161,7 @@ const onSuggestionShow = () => {
}) })
} }
const getData = (queryString: string) => { const getData = async (queryString: string) => {
if (suggestionDisabled.value) { if (suggestionDisabled.value) {
return return
} }
@ -182,11 +181,9 @@ const getData = (queryString: string) => {
if (isArray(props.fetchSuggestions)) { if (isArray(props.fetchSuggestions)) {
cb(props.fetchSuggestions) cb(props.fetchSuggestions)
} else { } else {
const result = props.fetchSuggestions(queryString, cb) const result = await props.fetchSuggestions(queryString, cb)
if (isArray(result)) { if (isArray(result)) {
cb(result) cb(result)
} else if (isPromise(result)) {
result.then(cb)
} }
} }
} }