UNPKG

@wordpress/components

Version:
115 lines (96 loc) 2.94 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _element = require("@wordpress/element"); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _uuid = require("uuid"); var _compose = require("@wordpress/compose"); var _list = _interopRequireDefault(require("../../notice/list")); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ /** * Override the default edit UI to include notices if supported. * * @param {WPComponent} OriginalComponent Original component. * * @return {WPComponent} Wrapped component. */ var _default = (0, _compose.createHigherOrderComponent)(OriginalComponent => { function Component(props, ref) { const [noticeList, setNoticeList] = (0, _element.useState)([]); const noticeOperations = (0, _element.useMemo)(() => { /** * Function passed down as a prop that adds a new notice. * * @param {Object} notice Notice to add. */ const createNotice = notice => { const noticeToAdd = notice.id ? notice : { ...notice, id: (0, _uuid.v4)() }; setNoticeList(current => [...current, noticeToAdd]); }; return { createNotice, /** * Function passed as a prop that adds a new error notice. * * @param {string} msg Error message of the notice. */ createErrorNotice: msg => { createNotice({ status: 'error', content: msg }); }, /** * Removes a notice by id. * * @param {string} id Id of the notice to remove. */ removeNotice: id => { setNoticeList(current => current.filter(notice => notice.id !== id)); }, /** * Removes all notices */ removeAllNotices: () => { setNoticeList([]); } }; }, []); const propsOut = { ...props, noticeList, noticeOperations, noticeUI: noticeList.length > 0 && (0, _element.createElement)(_list.default, { className: "components-with-notices-ui", notices: noticeList, onRemove: noticeOperations.removeNotice }) }; return isForwardRef ? (0, _element.createElement)(OriginalComponent, (0, _extends2.default)({}, propsOut, { ref: ref })) : (0, _element.createElement)(OriginalComponent, propsOut); } let isForwardRef; const { render } = OriginalComponent; // Returns a forwardRef if OriginalComponent appears to be a forwardRef if (typeof render === 'function') { isForwardRef = true; return (0, _element.forwardRef)(Component); } return Component; }); exports.default = _default; //# sourceMappingURL=index.js.map