UNPKG

@kwiz/fluentui

Version:

KWIZ common controls for FluentUI

37 lines 2.42 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 { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { Toast, ToastTitle, Toaster, useId, useToastController } from "@fluentui/react-components"; import { useCallback, useState } from "react"; import { PleaseWait } from "../controls/please-wait"; import { useEffectOnlyOnMount, useStateEX } from "./hooks"; /* Provides useful helpers for tracking if control has changes, and handling the save changes with progress bar and on success/fail messages. */ export function useEditableControl() { const [showProgress, setShowProgress] = useState(false); const [hasChanges, setHasChanges, hasChangesRef] = useStateEX(false, { skipUpdateIfSame: true }); const toasterId = useId("toaster"); const { dispatchToast } = useToastController(toasterId); const onSaveChanges = useCallback((worker) => __awaiter(this, void 0, void 0, function* () { setShowProgress(true); const success = yield worker(); setShowProgress(false); if (success.success !== true) { dispatchToast(_jsx(Toast, { children: _jsx(ToastTitle, { children: success.message || "Could not save your changes." }) }), { intent: "warning", timeout: 10000 }); } else { setHasChanges(false); dispatchToast(_jsx(Toast, { children: _jsx(ToastTitle, { children: success.message || "Changes saved!" }) }), { intent: "success", timeout: 2000 }); } }), useEffectOnlyOnMount); return { hasChanges, hasChangesRef, setHasChanges, onSaveChanges, editablePageElement: _jsxs(_Fragment, { children: [showProgress && _jsx(PleaseWait, {}), _jsx(Toaster, { toasterId: toasterId })] }) }; } //# sourceMappingURL=use-editable-control.js.map