{"version":3,"file":"index.mjs","names":["uuid"],"sources":["../../../src/higher-order/with-notices/index.tsx"],"sourcesContent":["import { v4 as uuid } from 'uuid';\nimport { forwardRef, useState, useMemo } from '@wordpress/element';\nimport { createHigherOrderComponent } from '@wordpress/compose';\nimport NoticeList from '../../notice/list';\nimport type { WithNoticeProps } from './types';\n\n/**\n * Override the default edit UI to include notices if supported.\n *\n * Wrapping the original component with `withNotices` encapsulates the component\n * with the additional props `noticeOperations` and `noticeUI`.\n *\n * ```jsx\n * import { withNotices, Button } from '@wordpress/components';\n *\n * const MyComponentWithNotices = withNotices(\n * \t( { noticeOperations, noticeUI } ) => {\n * \t\tconst addError = () =>\n * \t\t\tnoticeOperations.createErrorNotice( 'Error message' );\n * \t\treturn (\n * \t\t\t<div>\n * \t\t\t\t{ noticeUI }\n * \t\t\t\t<Button variant=\"secondary\" onClick={ addError }>\n * \t\t\t\t\tAdd error\n * \t\t\t\t</Button>\n * \t\t\t</div>\n * \t\t);\n * \t}\n * );\n * ```\n *\n * @param OriginalComponent Original component.\n *\n * @return Wrapped component.\n */\nexport default createHigherOrderComponent( ( OriginalComponent ) => {\n\tfunction Component(\n\t\tprops: { [ key: string ]: any },\n\t\tref: React.ForwardedRef< any >\n\t) {\n\t\tconst [ noticeList, setNoticeList ] = useState<\n\t\t\tWithNoticeProps[ 'noticeList' ]\n\t\t>( [] );\n\n\t\tconst noticeOperations = useMemo<\n\t\t\tWithNoticeProps[ 'noticeOperations' ]\n\t\t>( () => {\n\t\t\tconst createNotice: WithNoticeProps[ 'noticeOperations' ][ 'createNotice' ] =\n\t\t\t\t( notice ) => {\n\t\t\t\t\tconst noticeToAdd = notice.id\n\t\t\t\t\t\t? notice\n\t\t\t\t\t\t: { ...notice, id: uuid() };\n\t\t\t\t\tsetNoticeList( ( current ) => [ ...current, noticeToAdd ] );\n\t\t\t\t};\n\n\t\t\treturn {\n\t\t\t\tcreateNotice,\n\t\t\t\tcreateErrorNotice: ( msg ) => {\n\t\t\t\t\t// @ts-expect-error TODO: Missing `id`, potentially a bug\n\t\t\t\t\tcreateNotice( {\n\t\t\t\t\t\tstatus: 'error',\n\t\t\t\t\t\tcontent: msg,\n\t\t\t\t\t} );\n\t\t\t\t},\n\t\t\t\tremoveNotice: ( id ) => {\n\t\t\t\t\tsetNoticeList( ( current ) =>\n\t\t\t\t\t\tcurrent.filter( ( notice ) => notice.id !== id )\n\t\t\t\t\t);\n\t\t\t\t},\n\t\t\t\tremoveAllNotices: () => {\n\t\t\t\t\tsetNoticeList( [] );\n\t\t\t\t},\n\t\t\t};\n\t\t}, [] );\n\n\t\tconst propsOut = {\n\t\t\t...props,\n\t\t\tnoticeList,\n\t\t\tnoticeOperations,\n\t\t\tnoticeUI: noticeList.length > 0 && (\n\t\t\t\t<NoticeList\n\t\t\t\t\tclassName=\"components-with-notices-ui\"\n\t\t\t\t\tnotices={ noticeList }\n\t\t\t\t\tonRemove={ noticeOperations.removeNotice }\n\t\t\t\t/>\n\t\t\t),\n\t\t};\n\n\t\treturn isForwardRef ? (\n\t\t\t<OriginalComponent { ...propsOut } ref={ ref } />\n\t\t) : (\n\t\t\t<OriginalComponent { ...propsOut } />\n\t\t);\n\t}\n\n\tlet isForwardRef: boolean;\n\t// @ts-expect-error - `render` will only be present when OriginalComponent was wrapped with forwardRef().\n\tconst { render } = OriginalComponent;\n\t// Returns a forwardRef if OriginalComponent appears to be a forwardRef.\n\tif ( typeof render === 'function' ) {\n\t\tisForwardRef = true;\n\t\treturn forwardRef( Component );\n\t}\n\treturn Component as React.ComponentType< any >;\n}, 'withNotices' );\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAA,uBAAe,4BAA8B,sBAAuB;CACnE,SAAS,UACR,OACA,KACC;EACD,MAAM,CAAE,YAAY,iBAAkB,SAEnC,CAAC,CAAE;EAEN,MAAM,mBAAmB,cAEhB;GACR,MAAM,gBACH,WAAY;IACb,MAAM,cAAc,OAAO,KACxB,SACA;KAAE,GAAG;KAAQ,IAAIA,GAAK;IAAE;IAC3B,eAAiB,YAAa,CAAE,GAAG,SAAS,WAAY,CAAE;GAC3D;GAED,OAAO;IACN;IACA,oBAAqB,QAAS;KAE7B,aAAc;MACb,QAAQ;MACR,SAAS;KACV,CAAE;IACH;IACA,eAAgB,OAAQ;KACvB,eAAiB,YAChB,QAAQ,QAAU,WAAY,OAAO,OAAO,EAAG,CAChD;IACD;IACA,wBAAwB;KACvB,cAAe,CAAC,CAAE;IACnB;GACD;EACD,GAAG,CAAC,CAAE;EAEN,MAAM,WAAW;GAChB,GAAG;GACH;GACA;GACA,UAAU,WAAW,SAAS,KAC7B,oBAAC,YAAD;IACC,WAAU;IACV,SAAU;IACV,UAAW,iBAAiB;GAC5B,CAAA;EAEH;EAEA,OAAO,eACN,oBAAC,mBAAD;GAAmB,GAAK;GAAiB;EAAO,CAAA,IAEhD,oBAAC,mBAAD,EAAmB,GAAK,SAAY,CAAA;CAEtC;CAEA,IAAI;CAEJ,MAAM,EAAE,WAAW;CAEnB,IAAK,OAAO,WAAW,YAAa;EACnC,eAAe;EACf,OAAO,WAAY,SAAU;CAC9B;CACA,OAAO;AACR,GAAG,aAAc"}