@daysnap/horn-use
Version:
horn use
46 lines (45 loc) • 1.26 kB
JavaScript
import { ref } from 'vue';
export const useVisible = (options = {}) => {
const { showCallback, hideCallback, confirmCallback } = options;
const visible = ref(false);
let resolve;
let reject;
let beforeClose;
// 显示
const show = async (options) => {
beforeClose = options?.beforeClose;
await showCallback?.(options);
return new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
visible.value = true;
});
};
// 隐藏
const hide = async (reason) => {
const result = (await beforeClose?.('cancel', reason)) ?? true;
if (!result) {
return;
}
await hideCallback?.(reason);
reject?.(reason || 'cancel');
visible.value = false;
};
// 确认
const confirm = async (...args) => {
const result = (await beforeClose?.('confirm', ...args)) ?? true;
if (!result) {
return;
}
reject = null;
const value = confirmCallback ? await confirmCallback(...args) : args[0];
resolve?.(value);
visible.value = false;
};
return {
visible,
show,
confirm,
hide,
};
};