UNPKG

@daysnap/vue-use

Version:
57 lines (56 loc) 2.54 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { ref } from 'vue'; export function useVisible(options = {}) { const { showCallback, hideCallback, confirmCallback } = options; const visible = ref(false); let resolve; let reject; let beforeClose; // 显示 const show = (options) => __awaiter(this, void 0, void 0, function* () { beforeClose = options === null || options === void 0 ? void 0 : options.beforeClose; yield (showCallback === null || showCallback === void 0 ? void 0 : showCallback(options)); return new Promise((_resolve, _reject) => { resolve = _resolve; reject = _reject; visible.value = true; }); }); // 隐藏 const hide = (reason) => __awaiter(this, void 0, void 0, function* () { var _a; const result = (_a = (yield (beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose('cancel', reason)))) !== null && _a !== void 0 ? _a : true; if (!result) { return; } yield (hideCallback === null || hideCallback === void 0 ? void 0 : hideCallback(reason)); reject === null || reject === void 0 ? void 0 : reject(reason || 'cancel'); visible.value = false; }); // 确认 const confirm = (...args) => __awaiter(this, void 0, void 0, function* () { var _b; const result = (_b = (yield (beforeClose === null || beforeClose === void 0 ? void 0 : beforeClose('confirm', ...args)))) !== null && _b !== void 0 ? _b : true; if (!result) { return; } const value = confirmCallback ? yield confirmCallback(...args) : args[0]; reject = null; resolve === null || resolve === void 0 ? void 0 : resolve(value); visible.value = false; }); return { visible, show, confirm, hide, }; }