Skip to content

Commit

Permalink
fix: correct even in useFocusableControl (#4484)
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem authored Feb 25, 2025
1 parent 8d87159 commit f6c342a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
5 changes: 2 additions & 3 deletions packages/ui/src/components/va-input/VaInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ import type { AnyStringPropType } from '../../utils/types/prop-type'
import { VaInputWrapper } from '../va-input-wrapper'
import { VaIcon } from '../va-icon'
import { combineFunctions } from '../../utils/combine-functions'
import { omit } from '../../utils/omit'
import { pick } from '../../utils/pick'
const VaInputWrapperProps = extractComponentProps(VaInputWrapper)
const { createEmits: createInputEmits, createListeners: createInputListeners } = useEmitProxy(
['change', 'keyup', 'keypress', 'keydown', 'focus', 'blur', 'input'],
['change', 'keyup', 'keypress', 'keydown', 'input'],
)
const { createEmits: createFieldEmits, createListeners: createFieldListeners } = useEmitProxy([
Expand Down Expand Up @@ -178,7 +177,7 @@ const inputListeners = createInputListeners(emit)
const inputEvents = {
...inputListeners,
onBlur: combineFunctions(onBlur, inputListeners.onBlur),
onBlur,
}
const setInputValue = (newValue: string) => {
Expand Down
19 changes: 9 additions & 10 deletions packages/ui/src/composables/fabrics/useFocusableControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ export const useFocusableControl = (
autofocus: boolean,
disabled?: boolean,
},
emit: (event: (typeof useFocusableControlEmits)[number]) => void,
emit: (event: (typeof useFocusableControlEmits)[number], e: FocusEvent) => void,
) => {
const isFocused = useElementFocused(el)
const isFocused = useElementFocused(el, {
onBlur (e) {
emit('blur', e)
},
onFocus (e) {
emit('focus', e)
},
})

const focus = () => {
if (props.disabled) { return }
Expand All @@ -37,14 +44,6 @@ export const useFocusableControl = (
}
})

watch(isFocused, (value) => {
if (value) {
emit('focus')
} else {
emit('blur')
}
})

return {
focus,
blur,
Expand Down
14 changes: 11 additions & 3 deletions packages/ui/src/composables/std/browser/useElementFocused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import { focusElement, blurElement } from '../../../utils/focus'
import { unwrapEl } from '../../../utils/unwrapEl'
import { TemplateRef } from '../../../utils/types/template-ref'

export const useElementFocused = (el: Ref<TemplateRef>) => {
export const useElementFocused = (
el: Ref<TemplateRef>,
options: {
onFocus?: (e: FocusEvent) => void,
onBlur?: (e: FocusEvent) => void
} = {},
) => {
const isFocused = ref(false)

useEvent('focus', () => {
useEvent('focus', (e) => {
isFocused.value = true
options.onFocus?.(e)
}, el)

useEvent('blur', () => {
useEvent('blur', (e) => {
isFocused.value = false
options.onBlur?.(e)
}, el)

return computed({
Expand Down

0 comments on commit f6c342a

Please sign in to comment.