UNPKG

@wordpress/components

Version:
115 lines (114 loc) • 3.79 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let uuid = require("uuid"); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let ______notice_list_tsx = require("../../notice/list.cjs"); ______notice_list_tsx = __toESM(______notice_list_tsx); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/higher-order/with-notices/index.tsx /** * Override the default edit UI to include notices if supported. * * Wrapping the original component with `withNotices` encapsulates the component * with the additional props `noticeOperations` and `noticeUI`. * * ```jsx * import { withNotices, Button } from '@wordpress/components'; * * const MyComponentWithNotices = withNotices( * ( { noticeOperations, noticeUI } ) => { * const addError = () => * noticeOperations.createErrorNotice( 'Error message' ); * return ( * <div> * { noticeUI } * <Button variant="secondary" onClick={ addError }> * Add error * </Button> * </div> * ); * } * ); * ``` * * @param OriginalComponent Original component. * * @return Wrapped component. */ var with_notices_default = (0, _wordpress_compose.createHigherOrderComponent)((OriginalComponent) => { function Component(props, ref) { const [noticeList, setNoticeList] = (0, _wordpress_element.useState)([]); const noticeOperations = (0, _wordpress_element.useMemo)(() => { const createNotice = (notice) => { const noticeToAdd = notice.id ? notice : { ...notice, id: (0, uuid.v4)() }; setNoticeList((current) => [...current, noticeToAdd]); }; return { createNotice, createErrorNotice: (msg) => { createNotice({ status: "error", content: msg }); }, removeNotice: (id) => { setNoticeList((current) => current.filter((notice) => notice.id !== id)); }, removeAllNotices: () => { setNoticeList([]); } }; }, []); const propsOut = { ...props, noticeList, noticeOperations, noticeUI: noticeList.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______notice_list_tsx.default, { className: "components-with-notices-ui", notices: noticeList, onRemove: noticeOperations.removeNotice }) }; return isForwardRef ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(OriginalComponent, { ...propsOut, ref }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(OriginalComponent, { ...propsOut }); } let isForwardRef; const { render } = OriginalComponent; if (typeof render === "function") { isForwardRef = true; return (0, _wordpress_element.forwardRef)(Component); } return Component; }, "withNotices"); exports.default = with_notices_default; //# sourceMappingURL=index.cjs.map