fix: the disabled state should not trigger focus (#18108)

This commit is contained in:
btea 2024-08-31 19:48:48 +08:00 committed by GitHub
parent 4f380a6911
commit d7e5f193cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 20 additions and 1 deletions

View File

@ -263,6 +263,9 @@ const _ref = computed(() => input.value || textarea.value)
const { wrapperRef, isFocused, handleFocus, handleBlur } = useFocusController(
_ref,
{
beforeFocus() {
return inputDisabled.value
},
afterBlur() {
if (props.validateEvent) {
elFormItem?.validate?.('blur').catch((err) => debugWarn(err))

View File

@ -173,6 +173,9 @@ const handleInputKeyDown = (e: KeyboardEvent | Event) => {
}
const { wrapperRef } = useFocusController(elInputRef, {
beforeFocus() {
return props.disabled
},
afterFocus() {
syncAfterCursorMove()
},

View File

@ -106,6 +106,9 @@ const useSelect = (props: ISelectV2Props, emit) => {
})
const { wrapperRef, isFocused } = useFocusController(inputRef, {
beforeFocus() {
return selectDisabled.value
},
afterFocus() {
if (props.automaticDropdown && !expanded.value) {
expanded.value = true

View File

@ -104,6 +104,9 @@ export const useSelect = (props: ISelectProps, emit) => {
})
const { wrapperRef, isFocused, handleBlur } = useFocusController(inputRef, {
beforeFocus() {
return selectDisabled.value
},
afterFocus() {
if (props.automaticDropdown && !expanded.value) {
expanded.value = true

View File

@ -117,6 +117,7 @@ describe('useFocusController', () => {
await nextTick()
expect(wrapper.find('span').text()).toBe('false')
expect(wrapper.find('div').attributes('tabindex')).toBe('-1')
expect(focusHandler).not.toHaveBeenCalled()
expect(blurHandler).not.toHaveBeenCalled()

View File

@ -1,4 +1,4 @@
import { getCurrentInstance, onMounted, ref, shallowRef } from 'vue'
import { getCurrentInstance, onMounted, ref, shallowRef, watch } from 'vue'
import { useEventListener } from '@vueuse/core'
import { isElement, isFunction } from '@element-plus/utils'
import type { ShallowRef } from 'vue'
@ -64,6 +64,12 @@ export function useFocusController<T extends { focus: () => void }>(
target.value?.focus()
}
watch(wrapperRef, (el) => {
if (el) {
el.setAttribute('tabindex', '-1')
}
})
useEventListener(wrapperRef, 'focus', handleFocus, true)
useEventListener(wrapperRef, 'blur', handleBlur, true)
useEventListener(wrapperRef, 'click', handleClick, true)