mirror of
https://github.com/element-plus/element-plus.git
synced 2025-02-17 11:49:41 +08:00
fix(select): Fix select.focus is undefined bug (#862)
* fix(select): Fix select.focus is undefined bug * fix(select): Add hook test Co-authored-by: bastarder <jie.qian@blockheaders.com>
This commit is contained in:
parent
a2ad745293
commit
e4bb9daf4f
17
packages/hooks/__tests__/use-focus.spec.ts
Normal file
17
packages/hooks/__tests__/use-focus.spec.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { ref } from 'vue'
|
||||
import useFocus from '../use-focus'
|
||||
|
||||
describe('useFocus', () => {
|
||||
it('should focus el', async () => {
|
||||
const inputEl = document.createElement('input')
|
||||
document.body.appendChild(inputEl)
|
||||
|
||||
const reference = ref(inputEl)
|
||||
const { focus } = useFocus(reference)
|
||||
|
||||
focus()
|
||||
|
||||
expect(document.activeElement).toBe(inputEl)
|
||||
})
|
||||
})
|
||||
|
@ -4,3 +4,4 @@ export { default as useLockScreen } from './use-lockscreen'
|
||||
export { default as useRestoreActive } from './use-restore-active'
|
||||
export { default as useModal } from './use-modal'
|
||||
export { default as useMigrating } from './use-migrating'
|
||||
export { default as useFocus } from './use-focus'
|
||||
|
9
packages/hooks/use-focus/index.ts
Normal file
9
packages/hooks/use-focus/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Ref } from 'vue'
|
||||
|
||||
export default (el: Ref<HTMLElement>) => {
|
||||
return {
|
||||
focus: () => {
|
||||
el.value?.focus?.()
|
||||
},
|
||||
}
|
||||
}
|
@ -185,6 +185,7 @@ import { UPDATE_MODEL_EVENT, CHANGE_EVENT } from '@element-plus/utils/constants'
|
||||
import { isValidComponentSize } from '@element-plus/utils/validators'
|
||||
import { useSelect, useSelectStates } from './useSelect'
|
||||
import { selectKey } from './token'
|
||||
import { useFocus } from '@element-plus/hooks'
|
||||
|
||||
import type { PropType } from 'vue'
|
||||
|
||||
@ -301,6 +302,8 @@ export default defineComponent({
|
||||
scrollbar,
|
||||
} = useSelect(props, states, ctx)
|
||||
|
||||
const { focus } = useFocus(reference)
|
||||
|
||||
const {
|
||||
inputWidth,
|
||||
selected,
|
||||
@ -422,6 +425,7 @@ export default defineComponent({
|
||||
getValueKey,
|
||||
navigateOptions,
|
||||
dropMenuVisible,
|
||||
focus,
|
||||
|
||||
reference,
|
||||
input,
|
||||
|
Loading…
Reference in New Issue
Block a user