mirror of
https://github.com/element-plus/element-plus.git
synced 2024-11-27 02:01:15 +08:00
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:
parent
1037eb75b4
commit
28e87550e9
@ -11,7 +11,7 @@ vi.useFakeTimers()
|
||||
|
||||
const _mount = (
|
||||
payload = {},
|
||||
type: 'fn-cb' | 'fn-promise' | 'fn-arr' | 'arr' = 'fn-cb'
|
||||
type: 'fn-cb' | 'fn-promise' | 'fn-arr' | 'fn-async' | 'arr' = 'fn-cb'
|
||||
) =>
|
||||
mount({
|
||||
setup() {
|
||||
@ -46,6 +46,11 @@ const _mount = (
|
||||
case 'fn-promise':
|
||||
return (queryString: string) =>
|
||||
Promise.resolve(filterList(queryString))
|
||||
case 'fn-async':
|
||||
return async (queryString: string) => {
|
||||
await Promise.resolve()
|
||||
return filterList(queryString)
|
||||
}
|
||||
case 'fn-arr':
|
||||
return (queryString: string) => filterList(queryString)
|
||||
case 'arr':
|
||||
@ -166,6 +171,21 @@ describe('Autocomplete.vue', () => {
|
||||
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 () => {
|
||||
const wrapper = _mount({ debounce: 10 }, 'fn-arr')
|
||||
await nextTick()
|
||||
|
@ -96,7 +96,6 @@ import {
|
||||
ref,
|
||||
useAttrs as useCompAttrs,
|
||||
} from 'vue'
|
||||
import { isPromise } from '@vue/shared'
|
||||
import { debounce } from 'lodash-unified'
|
||||
import { onClickOutside } from '@vueuse/core'
|
||||
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) {
|
||||
return
|
||||
}
|
||||
@ -182,11 +181,9 @@ const getData = (queryString: string) => {
|
||||
if (isArray(props.fetchSuggestions)) {
|
||||
cb(props.fetchSuggestions)
|
||||
} else {
|
||||
const result = props.fetchSuggestions(queryString, cb)
|
||||
const result = await props.fetchSuggestions(queryString, cb)
|
||||
if (isArray(result)) {
|
||||
cb(result)
|
||||
} else if (isPromise(result)) {
|
||||
result.then(cb)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user