UNPKG

@kwiz/fluentui

Version:

KWIZ common controls for FluentUI

65 lines 2.6 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { Label } from "@fluentui/react-components"; import { isFunction, isString } from "@kwiz/common"; import { useCallback } from "react"; import { Prompter } from "../controls/prompt"; import { useEffectOnlyOnMount, useStateEX } from "./hooks"; /** set block message if you want to block nav. * - call setMessage to add a blocker message * - call onNav when you have internal navigation (open / close popups) * - render the navPrompt control to your page * FYI for page unload, most modern browsers won't show your message but a generic one instead. */ 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: () => { _setPrompt(null); if (isFunction(info.onOK)) info.onOK(); } })); }, 1); }, useEffectOnlyOnMount); const confirmEX = useCallback((message, onOK, onCancel) => { return new Promise(resolve => { promptEX({ children: isString(message) ? _jsx(Label, { children: message }) : message, onCancel: () => { if (isFunction(onCancel)) onCancel(); resolve(false); }, onOK: () => { if (isFunction(onOK)) onOK(); resolve(true); } }); }); }, 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