UNPKG

@uploadcare/react-widget

Version:

React File Uploader. React component with uploading from multiple sources

2 lines (1 loc) 3.57 kB
import{b as e,_ as n,a as r,c as t,d as a}from"./index-a5d285d0.mjs";import l,{useState as o,useMemo as i,useRef as c,useEffect as u,useImperativeHandle as s}from"react";import f from"uploadcare-widget";import{u as d,a as C,b as m,d as p}from"./default-preview-url-callback-c8617386.mjs";import"react-fast-compare";var v=["value","apiRef","onTabChange","onChange","onProgress","customTabs","previewUrlCallback","metadataCallback"],b={height:"500px",display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center"},L=function(e,n){var r=[];return e&&(r=Array.isArray(e)?e:[e]),r},A=function(A){var w=o((function(){return"uploadcare-".concat(Date.now())})),y=e(w,1)[0],T=i((function(){return l.createElement("style",null,function(e){return"\n .".concat(e," .uploadcare--preview__done:not(.uploadcare-tab-effects--done),\n .").concat(e," .uploadcare--panel__done:not(.uploadcare-tab-effects--done) {\n display: none;\n }\n")}(y))}),[y]),g=function(e,l){var o=e.value,i=e.apiRef,f=e.onTabChange,b=e.onChange,A=e.onProgress,w=e.customTabs,y=e.previewUrlCallback,T=e.metadataCallback,g=n(e,v),E=g.tabsCss,O=g.locale,R=g.localeTranslations,_=g.localePluralize,h=c(null),P=c(null),U=d(f),D=d(b),k=d(A),F=d(T),j=d(y||p);return C(w,l),m((function(){return O&&(window.UPLOADCARE_LOCALE=O),_&&(window.UPLOADCARE_LOCALE_PLURALIZE=_),R&&(window.UPLOADCARE_LOCALE_TRANSLATIONS=R),function(){O&&delete window.UPLOADCARE_LOCALE,_&&delete window.UPLOADCARE_LOCALE_PLURALIZE,R&&delete window.UPLOADCARE_LOCALE_TRANSLATIONS}}),[O,_,R]),u((function(){l&&E&&"string"==typeof E&&(0===E.indexOf("https://")?l.tabsCss.addUrl(E):l.tabsCss.addStyle(E))}),[l,E]),u((function(){var e=L(o);P.current&&P.current.reject(),P.current=l.openPanel(h.current,e,r(r({multipleMax:g.multiple?void 0:1},g),{},{multiple:!0,metadataCallback:g.metadata?void 0:F,previewUrlCallback:j}))}),[l].concat(t(Object.values(g)))),u((function(){var e=P.current;e.progress(U);var n=function(){var e=P.current.fileColl.__items.map((function(e){return e.promise()}));D(e)},r=function(){var e=P.current.fileColl.lastProgresses();k(e)};return e.fileColl.anyProgressList.add(r),e.fileColl.anyDoneList.add(n),e.fileColl.anyFailList.add(n),e.fileColl.onRemove.add(n),e.fileColl.onReplace.add(n),e.fileColl.onSort.add(n),function(){var e=P.current;e.fileColl.anyProgressList.remove(r),e.fileColl.anyDoneList.remove(n),e.fileColl.anyFailList.remove(n),e.fileColl.onRemove.remove(n),e.fileColl.onReplace.remove(n),e.fileColl.onSort.remove(n)}}),[].concat(t(Object.values(g)),[U,D,k])),s(i,(function(){return{onTabVisibility:function(e){return P.current.onTabVisibility(e)},hideTab:function(e){return P.current.hideTab(e)},showTab:function(e){return P.current.showTab(e)},switchTab:function(e){return P.current.switchTab(e)},addFiles:function(e){return P.current.addFiles(e)},isTabVisible:function(e){return P.current.isTabVisible(e)},getFileColl:function(){return P.current.fileColl},getRawDialogApi:function(){return P.current}}}),[]),u((function(){return function(){return P.current&&P.current.reject()}}),[]),u((function(){var n=!1;P.current.fileColl.clear();var r,t=L(o),i=a(t);try{for(i.s();!(r=i.n()).done;){var c=r.value;if("string"==typeof c&&c.includes("~")){l.loadFileGroup(c,e).then((function(e){if(!n){var r=e.files();P.current.addFiles(r)}}));break}P.current.fileColl.add(l.fileFrom("uploaded",c,e))}}catch(e){i.e(e)}finally{i.f()}return function(){n=!0}}),[o]),[h]}(A,f),E=e(g,1)[0];return l.createElement("div",{id:A.id,className:y,style:b},T,l.createElement("div",{ref:E}))};export{A as default};