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

fix(components): [autocomplete] the blur event is not triggered #10091

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/components/autocomplete/__tests__/autocomplete.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,50 @@ describe('Autocomplete.vue', () => {
expect(formItem.attributes().role).toBe('group')
})
})

test('event:focus', async () => {
const onFocus = vi.fn()
const wrapper = _mount({ onFocus })
await nextTick()

const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
typeof Autocomplete
>

await wrapper.find('input').trigger('focus')
vi.runAllTimers()
await nextTick()
expect(onFocus).toHaveBeenCalledTimes(1)

await target.handleSelect({ value: 'Go', tag: 'go' })
expect(target.modelValue).toBe('Go')
vi.runAllTimers()
await nextTick()
expect(onFocus).toHaveBeenCalledTimes(1)

await wrapper.find('input').trigger('blur')
vi.runAllTimers()
await nextTick()
expect(onFocus).toHaveBeenCalledTimes(1)
})

test('event:blur', async () => {
const onBlur = vi.fn()
const wrapper = _mount({ onBlur })
await nextTick()

const target = wrapper.getComponent(Autocomplete).vm as InstanceType<
typeof Autocomplete
>

await wrapper.find('input').trigger('focus')
await target.handleSelect({ value: 'Go', tag: 'go' })
expect(target.modelValue).toBe('Go')
expect(onBlur).toHaveBeenCalledTimes(0)

await wrapper.find('input').trigger('blur')
vi.runAllTimers()
await nextTick()
expect(onBlur).toHaveBeenCalledTimes(1)
})
})
32 changes: 18 additions & 14 deletions packages/components/autocomplete/src/autocomplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
:transition="`${ns.namespace.value}-zoom-in-top`"
persistent
@before-show="onSuggestionShow"
@show="onShow"
@hide="onHide"
>
<div
Expand Down Expand Up @@ -178,12 +177,7 @@ const onSuggestionShow = async () => {
}
}

const onShow = () => {
ignoreFocusEvent = true
}

const onHide = () => {
ignoreFocusEvent = false
highlightedIndex.value = -1
}

Expand Down Expand Up @@ -245,19 +239,29 @@ const handleChange = (value: string) => {
}

const handleFocus = (evt: FocusEvent) => {
if (ignoreFocusEvent) return
if (!ignoreFocusEvent) {
activated.value = true
emit('focus', evt)

activated.value = true
emit('focus', evt)
// fix https://github.com/element-plus/element-plus/issues/8278
if (props.triggerOnFocus && !readonly) {
debouncedGetData(String(props.modelValue))
if (props.triggerOnFocus && !readonly) {
debouncedGetData(String(props.modelValue))
}
} else {
ignoreFocusEvent = false
}
}

const handleBlur = (evt: FocusEvent) => {
if (ignoreFocusEvent) return
emit('blur', evt)
setTimeout(() => {
// validate current focus event is inside el-tooltip-content
// if so, ignore the blur event and the next focus event
if (popperRef.value?.isFocusInsideContent()) {
ignoreFocusEvent = true
return
}
activated.value && close()
emit('blur', evt)
})
}

const handleClear = () => {
Expand Down