@kwiz/fluentui
Version:
KWIZ common controls for FluentUI
84 lines • 3.77 kB
JavaScript
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 { jsx as _jsx } from "react/jsx-runtime";
import { Label } from "@fluentui/react-components";
import { isFunction, isNullOrUndefined, isString } from "@kwiz/common";
import { useCallback } from "react";
import { Prompter } from "../controls/prompt";
import { useEffectOnlyOnMount, useStateEX } from "./hooks";
function isConfirmOptions(param) {
return isFunction(param) ? false : true;
}
/** Easily prompt, confirm or alert the user. To use this you must render the alertPrompt in your element. */
export function useAlerts() {
const [_prompt, _setPrompt] = useStateEX(null);
const promptEX = useCallback((info) => {
//need to release react to re-render the prompt
window.setTimeout(() => {
//prompt, if ok - clear messages and nav.
_setPrompt(Object.assign(Object.assign({}, info), { onCancel: () => {
_setPrompt(null);
if (isFunction(info.onCancel))
info.onCancel();
}, onOK: () => __awaiter(this, void 0, void 0, function* () {
let closeDialog = true;
if (isFunction(info.onOK)) {
closeDialog = (yield info.onOK()) === false ? false : true;
}
if (closeDialog)
_setPrompt(null);
}) }));
}, 1);
}, useEffectOnlyOnMount);
const confirmEX = useCallback((message, onOKOrOptions, onCancel) => {
const options = isNullOrUndefined(onOKOrOptions)
? { onCancel: onCancel }
: isConfirmOptions(onOKOrOptions) ? onOKOrOptions : {
onOK: onOKOrOptions,
onCancel: onCancel
};
return new Promise(resolve => {
promptEX({
children: isString(message) ? _jsx(Label, { children: message }) : message,
onCancel: () => {
if (isFunction(options.onCancel))
options.onCancel();
resolve(false);
},
onOK: () => {
if (isFunction(options.onOK))
options.onOK();
resolve(true);
},
okButtonProps: options.okProps,
cancelButtonProps: options.cancelProps,
});
});
}, useEffectOnlyOnMount);
const alertEX = useCallback((message, onOK) => {
return new Promise(resolve => {
promptEX({
children: isString(message) ? _jsx(Label, { children: message }) : message,
hideCancel: true,
onOK: () => {
if (isFunction(onOK))
onOK();
resolve();
}
});
});
}, useEffectOnlyOnMount);
return {
promptEX, confirmEX, alertEX,
alertPrompt: _prompt ? _jsx(Prompter, Object.assign({}, _prompt)) : undefined,
close: () => _setPrompt(null)
};
}
//# sourceMappingURL=use-alerts.js.map