fix(components): [select] the blur not triggered when click the outside (#18478)

* fix(components): [select] the blur not triggered when click the outside

closed #18235, #18238

* test: add test
This commit is contained in:
qiang 2024-10-11 11:47:35 +08:00 committed by GitHub
parent f23ae6237a
commit 8908e738e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 6 deletions

View File

@ -106,7 +106,7 @@ const useSelect = (props: ISelectV2Props, emit: SelectEmitFn) => {
afterComposition: (e) => onInput(e),
})
const { wrapperRef, isFocused } = useFocusController(inputRef, {
const { wrapperRef, isFocused, handleBlur } = useFocusController(inputRef, {
beforeFocus() {
return selectDisabled.value
},
@ -708,9 +708,13 @@ const useSelect = (props: ISelectV2Props, emit: SelectEmitFn) => {
}
}
const handleClickOutside = () => {
const handleClickOutside = (event: Event) => {
expanded.value = false
isFocused.value && blur()
if (isFocused.value) {
const _event = new FocusEvent('focus', event)
handleBlur(_event)
}
}
const handleMenuEnter = () => {

View File

@ -1527,6 +1527,32 @@ describe('Select', () => {
expect(handleBlur).toHaveBeenCalled()
})
it('should be target blur event when click outside', async () => {
const handleBlur = vi.fn()
wrapper = _mount(
`
<el-select @blur="handleBlur" />
<button>button</button>
`,
() => ({ handleBlur })
)
const select = wrapper.findComponent({ name: 'ElSelect' })
const input = select.find('input')
await input.trigger('focus')
expect(wrapper.find(`.${WRAPPER_CLASS_NAME}`).classes()).toContain(
'is-focused'
)
await wrapper.find('button').trigger('mousedown')
await wrapper.find('button').trigger('mouseup')
expect(wrapper.find(`.${WRAPPER_CLASS_NAME}`).classes()).not.toContain(
'is-focused'
)
expect(handleBlur).toHaveBeenCalledTimes(1)
})
test('should not open popper when automatic-dropdown not set', async () => {
wrapper = getSelectVm()
const select = wrapper.findComponent({ name: 'ElSelect' })

View File

@ -103,7 +103,7 @@ export const useSelect = (props: ISelectProps, emit) => {
afterComposition: (e) => onInput(e),
})
const { wrapperRef, isFocused } = useFocusController(inputRef, {
const { wrapperRef, isFocused, handleBlur } = useFocusController(inputRef, {
beforeFocus() {
return selectDisabled.value
},
@ -669,9 +669,13 @@ export const useSelect = (props: ISelectProps, emit) => {
deleteSelected(event)
}
const handleClickOutside = () => {
const handleClickOutside = (event: Event) => {
expanded.value = false
isFocused.value && blur()
if (isFocused.value) {
const _event = new FocusEvent('focus', event)
nextTick(() => handleBlur(_event))
}
}
const handleEsc = () => {