You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

0 lines
7.3 KiB

1 month ago
  1. {"ast":null,"code":"import { getCurrentInstance, shallowRef, ref, watch, onMounted } from 'vue';\nimport { useEventListener } from '@vueuse/core';\nimport { isElement } from '../../utils/types.mjs';\nimport { isFunction } from '@vue/shared';\nfunction useFocusController(target, {\n beforeFocus,\n afterFocus,\n beforeBlur,\n afterBlur\n} = {}) {\n const instance = getCurrentInstance();\n const {\n emit\n } = instance;\n const wrapperRef = shallowRef();\n const isFocused = ref(false);\n const handleFocus = event => {\n const cancelFocus = isFunction(beforeFocus) ? beforeFocus(event) : false;\n if (cancelFocus || isFocused.value) return;\n isFocused.value = true;\n emit(\"focus\", event);\n afterFocus == null ? void 0 : afterFocus();\n };\n const handleBlur = event => {\n var _a;\n const cancelBlur = isFunction(beforeBlur) ? beforeBlur(event) : false;\n if (cancelBlur || event.relatedTarget && ((_a = wrapperRef.value) == null ? void 0 : _a.contains(event.relatedTarget))) return;\n isFocused.value = false;\n emit(\"blur\", event);\n afterBlur == null ? void 0 : afterBlur();\n };\n const handleClick = () => {\n var _a, _b;\n if (((_a = wrapperRef.value) == null ? void 0 : _a.contains(document.activeElement)) && wrapperRef.value !== document.activeElement) return;\n (_b = target.value) == null ? void 0 : _b.focus();\n };\n watch(wrapperRef, el => {\n if (el) {\n el.setAttribute(\"tabindex\", \"-1\");\n }\n });\n useEventListener(wrapperRef, \"focus\", handleFocus, true);\n useEventListener(wrapperRef, \"blur\", handleBlur, true);\n useEventListener(wrapperRef, \"click\", handleClick, true);\n if (process.env.NODE_ENV === \"test\") {\n onMounted(() => {\n const targetEl = isElement(target.value) ? target.value : document.querySelector(\"input,textarea\");\n if (targetEl) {\n useEventListener(targetEl, \"focus\", handleFocus, true);\n useEventListener(targetEl, \"blur\", handleBlur, true);\n }\n });\n }\n return {\n isFocused,\n wrapperRef,\n handleFocus,\n handleBlur\n };\n}\nexport { useFocusController };","map":{"version":3,"names":["useFocusController","target","beforeFocus","afterFocus","beforeBlur","afterBlur","instance","getCurrentInstance","emit","wrapperRef","shallowRef","isFocused","ref","handleFocus","event","cancelFocus","isFunction","value","handleBlur","_a","cancelBlur","relatedTarget","contains","handleClick","_b","document","activeElement","focus","watch","el","setAttribute","useEventListener","process","env","NODE_ENV","onMounted","targetEl","isElement","querySelector"],"sources":["../../../../../packages/hooks/use-focus-controller/index.ts"],"sourcesContent":["import { getCurrentInstance, onMounted, ref, shallowRef, watch } from 'vue'\nimport { useEventListener } from '@vueuse/core'\nimport { isElement, isFunction } from '@element-plus/utils'\nimport type { ShallowRef } from 'vue'\n\ninterface UseFocusControllerOptions {\n /**\n * return true to cancel focus\n * @param event FocusEvent\n */\n beforeFocus?: (event: FocusEvent) => boolean | undefined\n afterFocus?: () => void\n /**\n * return true to cancel blur\n * @param event FocusEvent\n */\n beforeBlur?: (event: FocusEvent) => boolean | undefined\n afterBlur?: () => void\n}\n\nexport function useFocusController<T extends { focus: () => void }>(\n target: ShallowRef<T | undefined>,\n {\n beforeFocus,\n afterFocus,\n beforeBlur,\n afterBlur,\n }: UseFocusControllerOptions = {}\n) {\n const instance = getCurrentInstance()!\n const { emit } = instance\n const wrapperRef = shallowRef<HTMLElement>()\n const isFocused = ref(false)\n\n const handleFocus = (event: FocusEvent) => {\n const cancelFocus = isFunction(beforeFocus) ? beforeFocus(event) : false\n if (cancelFocus || isFocused.value) return\n isFocused.value = true\n emit('focus', event)\n afterFocus?.()\n }\n\n const handleBlur = (event: FocusEvent) => {\n const cancelBlur = isFunction(beforeBlur) ? beforeBlur(event) : false\