forked from fdzcxy212206413/gsl_grs
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.
67 lines
2.1 KiB
67 lines
2.1 KiB
import { getCurrentInstance, shallowRef, ref, watch, onMounted } from 'vue';
|
|
import { useEventListener } from '@vueuse/core';
|
|
import '../../utils/index.mjs';
|
|
import { isFunction } from '@vue/shared';
|
|
import { isElement } from '../../utils/types.mjs';
|
|
|
|
function useFocusController(target, {
|
|
beforeFocus,
|
|
afterFocus,
|
|
beforeBlur,
|
|
afterBlur
|
|
} = {}) {
|
|
const instance = getCurrentInstance();
|
|
const { emit } = instance;
|
|
const wrapperRef = shallowRef();
|
|
const isFocused = ref(false);
|
|
const handleFocus = (event) => {
|
|
const cancelFocus = isFunction(beforeFocus) ? beforeFocus(event) : false;
|
|
if (cancelFocus || isFocused.value)
|
|
return;
|
|
isFocused.value = true;
|
|
emit("focus", event);
|
|
afterFocus == null ? void 0 : afterFocus();
|
|
};
|
|
const handleBlur = (event) => {
|
|
var _a;
|
|
const cancelBlur = isFunction(beforeBlur) ? beforeBlur(event) : false;
|
|
if (cancelBlur || event.relatedTarget && ((_a = wrapperRef.value) == null ? void 0 : _a.contains(event.relatedTarget)))
|
|
return;
|
|
isFocused.value = false;
|
|
emit("blur", event);
|
|
afterBlur == null ? void 0 : afterBlur();
|
|
};
|
|
const handleClick = () => {
|
|
var _a, _b;
|
|
if (((_a = wrapperRef.value) == null ? void 0 : _a.contains(document.activeElement)) && wrapperRef.value !== document.activeElement)
|
|
return;
|
|
(_b = target.value) == null ? void 0 : _b.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);
|
|
if (process.env.NODE_ENV === "test") {
|
|
onMounted(() => {
|
|
const targetEl = isElement(target.value) ? target.value : document.querySelector("input,textarea");
|
|
if (targetEl) {
|
|
useEventListener(targetEl, "focus", handleFocus, true);
|
|
useEventListener(targetEl, "blur", handleBlur, true);
|
|
}
|
|
});
|
|
}
|
|
return {
|
|
isFocused,
|
|
wrapperRef,
|
|
handleFocus,
|
|
handleBlur
|
|
};
|
|
}
|
|
|
|
export { useFocusController };
|
|
//# sourceMappingURL=index.mjs.map
|