@opensig/opendesign
Version:
58 lines (57 loc) • 1.42 kB
JavaScript
import { isTouchDevice } from "../_utils/is.mjs";
import { ref, watchEffect } from "vue";
function useInputPassword(options) {
const showPassword = ref(true);
watchEffect(() => {
showPassword.value = options.type.value !== "password";
});
const toggle = (show) => {
if (show === void 0) {
showPassword.value = !showPassword.value;
} else {
showPassword.value = show;
}
};
const onEyeClick = () => {
if (options.disabled.value) {
return;
}
if (options.showPasswordEvent === "click") {
toggle();
}
};
const onEyeMouseUp = () => {
if (showPassword.value) {
toggle(false);
if (isTouchDevice) {
window.removeEventListener("touchend", onEyeMouseUp);
window.removeEventListener("touchcancel", onEyeMouseUp);
} else {
window.removeEventListener("mouseup", onEyeMouseUp);
}
}
};
const onEyeMouseDown = () => {
if (options.disabled.value) {
return;
}
if (options.showPasswordEvent === "pointerdown") {
toggle(true);
if (isTouchDevice) {
window.addEventListener("touchend", onEyeMouseUp);
window.addEventListener("touchcancel", onEyeMouseUp);
} else {
window.addEventListener("mouseup", onEyeMouseUp);
}
}
};
return {
showPassword,
onEyeMouseDown,
onEyeMouseUp,
onEyeClick
};
}
export {
useInputPassword
};