@slan-health/taro-vueuse
Version:
taro vue版本hooks
58 lines (57 loc) • 1.63 kB
JavaScript
import { showToast, hideToast } from "@tarojs/taro";
import { timeOutPromise } from "../utils/index.js";
function useToast(initialOption) {
const showToastAsync = (option) => {
return new Promise((resolve, reject) => {
try {
if (!option && !initialOption) {
console.warn("please provide a option");
return reject(new Error("please provide a option"));
} else {
const options = Object.assign({}, initialOption || {}, option || {});
if (!options.title) {
reject({ errMsg: "showToast: fail" });
} else {
showToast({
...options,
success: resolve,
fail: reject
}).catch(reject);
}
}
} catch (e) {
reject(e);
}
});
};
const hideToastAsync = () => {
return new Promise((resolve, reject) => {
try {
hideToast({
success: resolve,
fail: reject
});
} catch (e) {
reject(e);
}
});
};
const showSuccessAsync = (title) => showToastAsync({ title, icon: "success" });
const showErrorAsync = (title) => showToastAsync({ title, icon: "error" });
const showNoneAsync = (title) => showToastAsync({ title, icon: "none" });
const showNoneTimeoutAsync = (title) => showToastAsync({ title, icon: "none" }).then(() => timeOutPromise(1500));
return [
{
showToastAsync,
showSuccessAsync,
showErrorAsync,
showNoneAsync,
showNoneTimeoutAsync
},
hideToastAsync
];
}
export {
useToast as default
};
//# sourceMappingURL=index.js.map