use-file-picker
Advanced tools
Comparing version
@@ -999,4 +999,6 @@ 'use strict'; | ||
inputElement.addEventListener('change', callback); // dispatch a click event to open the file dialog | ||
inputElement.addEventListener('change', callback); // set onblur event to call callback when user has selected file on Safari | ||
inputElement.addEventListener('blur', callback); // dispatch a click event to open the file dialog | ||
inputElement.dispatchEvent(new MouseEvent('click')); | ||
@@ -1003,0 +1005,0 @@ } |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),e=require("file-selector");function r(t,e,r,n,i,o,a){try{var c=t[o](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,i)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(i,o){var a=t.apply(e,n);function c(t){r(a,i,o,c,u,"next",t)}function u(t){r(a,i,o,c,u,"throw",t)}c(void 0)}))}}function i(){return(i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function o(t,e){if(null==t)return{};var r,n,i={},o=Object.keys(t);for(n=0;n<o.length;n++)e.indexOf(r=o[n])>=0||(i[r]=t[r]);return i}function a(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var c,u=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var i=Object.create((e&&e.prototype instanceof s?e:s).prototype),o=new P(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return{value:void 0,done:!0}}for(r.method=i,r.arg=o;;){var a=r.delegate;if(a){var c=x(a,r);if(c){if(c===f)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=l(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===f)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,o),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var f={};function s(){}function h(){}function v(){}var p={};p[i]=function(){return this};var d=Object.getPrototypeOf,m=d&&d(d(L([])));m&&m!==e&&r.call(m,i)&&(p=m);var y=v.prototype=s.prototype=Object.create(p);function g(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(i,o){function a(){return new e((function(n,a){!function n(i,o,a,c){var u=l(t[i],t,o);if("throw"!==u.type){var f=u.arg,s=f.value;return s&&"object"==typeof s&&r.call(s,"__await")?e.resolve(s.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(s).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(i,o,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=l(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function S(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function b(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(S,this),this.reset(!0)}function L(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:E}}function E(){return{value:void 0,done:!0}}return h.prototype=y.constructor=v,v.constructor=h,h.displayName=c(v,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,c(t,a,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},g(w.prototype),w.prototype[o]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,i,o){void 0===o&&(o=Promise);var a=new w(u(e,r,n,i),o);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},g(y),c(y,a,"Generator"),y[i]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=L,P.prototype={constructor:P,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(b),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var i=this.tryEntries.length-1;i>=0;--i){var o=this.tryEntries[i],a=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var c=r.call(o,"catchLoc"),u=r.call(o,"finallyLoc");if(c&&u){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(c){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,f):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),b(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;b(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(c={exports:{}}),c.exports),l=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(t,e){for(var r,n=t.minFileSize,i=t.maxFileSize,o=function(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return a(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(t,void 0):void 0}}(t))){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=t[Symbol.iterator]()).next.bind(r)}(e);!(r=o()).done;)if(n||i)return f({minFileSize:n,maxFileSize:i,fileSize:r.value.size});return Promise.resolve()},e.validateAfterParsing=function(){var t=n(u.mark((function t(e,r){return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",Promise.resolve());case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),t}(),f=function(t){var e=t.fileSize,r=t.maxFileSize,n=t.minFileSize;return new Promise((function(t,i){n&&e<1e6*n&&i({fileSizeTooSmall:!0}),r&&e>1e6*r&&i({fileSizeToolarge:!0}),t()}))},s=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(t,e){var r=t.limitFilesConfig;if(r){if(r.max&&e.length>r.max)return Promise.reject({maxLimitExceeded:!0});if(r.min&&e.length<r.min)return Promise.reject({minLimitNotReached:!0})}return Promise.resolve()},e.validateAfterParsing=function(){return Promise.resolve()},t}(),h=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(){return Promise.resolve()},e.validateAfterParsing=function(t,e,r){var n=t.imageSizeRestrictions;return"DataURL"===t.readAs&&n&&v(e.type)?p(r.result,n):Promise.resolve()},t}(),v=function(t){return t.startsWith("image")},p=function(t,e){return new Promise((function(r,n){var o=new Image;o.onload=function(){var t=e.maxHeight,o=e.maxWidth,a=e.minHeight,c=e.minWidth,u=this.width,l=this.height,f={};t&&t<l&&(f=i({},f,{imageHeightTooBig:!0})),a&&a>l&&(f=i({},f,{imageHeightTooSmall:!0})),o&&o<u&&(f=i({},f,{imageWidthTooBig:!0})),c&&c>u&&(f=i({},f,{imageWidthTooSmall:!0})),Object.keys(f).length?n(f):r()},o.onerror=function(){n({imageNotLoaded:!0})},o.src=t}))},d=[new l,new s,new h];exports.useFilePicker=function(r){var a=r.accept,c=void 0===a?"*":a,l=r.multiple,f=void 0===l||l,s=r.readAs,h=void 0===s?"Text":s,v=r.minFileSize,p=r.maxFileSize,m=r.imageSizeRestrictions,y=r.limitFilesConfig,g=r.readFilesContent,w=void 0===g||g,x=r.validators,S=void 0===x?[]:x,b=t.useState([]),P=b[0],L=b[1],E=t.useState([]),F=E[0],j=E[1],z=t.useState([]),O=z[0],A=z[1],_=t.useState(!1),k=_[0],T=_[1],N=t.useState([]),R=N[0],C=N[1],G=t.useRef([]),I=function(){C([]),L([]),j([]),A([])};return t.useEffect((function(){if(0!==P.length){T(!0);var t=P.map((function(t){return new Promise(function(){var e=n(u.mark((function e(r,a){var l,s;return u.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:(l=new FileReader)["readAs"+h].call(l,t),s=function(e){var r=e.name,n=void 0===r?t.name:r,c=o(e,["name"]);a(i({name:n,fileSizeToolarge:!1,fileSizeTooSmall:!1},c))},l.onload=n(u.mark((function e(){var n;return u.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:n=d.concat(S).map((function(e){return e.validateAfterParsing({accept:c,multiple:f,readAs:h,minFileSize:v,maxFileSize:p,imageSizeRestrictions:m,limitFilesConfig:y,readFilesContent:w},t,l).catch((function(t){return Promise.reject(s(t))}))})),Promise.all(n).then((function(){return r({content:l.result,name:t.name,lastModified:t.lastModified})}));case 2:case"end":return e.stop()}}),e)}))),l.onerror=function(){s({readerError:l.error})};case 6:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}())}));Promise.all(t).then((function(t){j(t),C(G.current),A([])})).catch((function(t){A((function(e){return[t].concat(e)}))})).finally((function(){return T(!1)}))}else j([])}),[P]),[function(){!function(t,e,r){var n=document.createElement("input");n.type="file",n.accept=t,n.multiple=e,n.addEventListener("change",r),n.dispatchEvent(new MouseEvent("click"))}(c instanceof Array?c.join(","):c,f,(function(t){I();var r=t.target;G.current=r.files?Array.from(r.files):[];var n=d.concat(S).map((function(t){return t.validateBeforeParsing({accept:c,multiple:f,readAs:h,minFileSize:v,maxFileSize:p,imageSizeRestrictions:m,limitFilesConfig:y,readFilesContent:w},G.current).catch((function(t){return Promise.reject(A((function(e){return[i({},t,e)]})))}))}));Promise.all(n).then((function(){w?e.fromEvent(t).then((function(t){L(t)})):C(G.current)}))}))},{filesContent:F,errors:O,loading:k,plainFiles:R,clear:I}]}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),e=require("file-selector");function r(t,e,r,n,i,o,a){try{var c=t[o](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,i)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(i,o){var a=t.apply(e,n);function c(t){r(a,i,o,c,u,"next",t)}function u(t){r(a,i,o,c,u,"throw",t)}c(void 0)}))}}function i(){return(i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function o(t,e){if(null==t)return{};var r,n,i={},o=Object.keys(t);for(n=0;n<o.length;n++)e.indexOf(r=o[n])>=0||(i[r]=t[r]);return i}function a(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var c,u=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var i=Object.create((e&&e.prototype instanceof s?e:s).prototype),o=new P(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return{value:void 0,done:!0}}for(r.method=i,r.arg=o;;){var a=r.delegate;if(a){var c=x(a,r);if(c){if(c===f)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=l(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===f)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,o),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var f={};function s(){}function h(){}function v(){}var d={};d[i]=function(){return this};var p=Object.getPrototypeOf,m=p&&p(p(L([])));m&&m!==e&&r.call(m,i)&&(d=m);var y=v.prototype=s.prototype=Object.create(d);function g(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(i,o){function a(){return new e((function(n,a){!function n(i,o,a,c){var u=l(t[i],t,o);if("throw"!==u.type){var f=u.arg,s=f.value;return s&&"object"==typeof s&&r.call(s,"__await")?e.resolve(s.__await).then((function(t){n("next",t,a,c)}),(function(t){n("throw",t,a,c)})):e.resolve(s).then((function(t){f.value=t,a(f)}),(function(t){return n("throw",t,a,c)}))}c(u.arg)}(i,o,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=l(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function S(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function b(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(S,this),this.reset(!0)}function L(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:E}}function E(){return{value:void 0,done:!0}}return h.prototype=y.constructor=v,v.constructor=h,h.displayName=c(v,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,v):(t.__proto__=v,c(t,a,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},g(w.prototype),w.prototype[o]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,i,o){void 0===o&&(o=Promise);var a=new w(u(e,r,n,i),o);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},g(y),c(y,a,"Generator"),y[i]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=L,P.prototype={constructor:P,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(b),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var i=this.tryEntries.length-1;i>=0;--i){var o=this.tryEntries[i],a=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var c=r.call(o,"catchLoc"),u=r.call(o,"finallyLoc");if(c&&u){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(c){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,f):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),b(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;b(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:L(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(c={exports:{}}),c.exports),l=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(t,e){for(var r,n=t.minFileSize,i=t.maxFileSize,o=function(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return a(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(t,void 0):void 0}}(t))){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=t[Symbol.iterator]()).next.bind(r)}(e);!(r=o()).done;)if(n||i)return f({minFileSize:n,maxFileSize:i,fileSize:r.value.size});return Promise.resolve()},e.validateAfterParsing=function(){var t=n(u.mark((function t(e,r){return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return",Promise.resolve());case 1:case"end":return t.stop()}}),t)})));return function(e,r){return t.apply(this,arguments)}}(),t}(),f=function(t){var e=t.fileSize,r=t.maxFileSize,n=t.minFileSize;return new Promise((function(t,i){n&&e<1e6*n&&i({fileSizeTooSmall:!0}),r&&e>1e6*r&&i({fileSizeToolarge:!0}),t()}))},s=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(t,e){var r=t.limitFilesConfig;if(r){if(r.max&&e.length>r.max)return Promise.reject({maxLimitExceeded:!0});if(r.min&&e.length<r.min)return Promise.reject({minLimitNotReached:!0})}return Promise.resolve()},e.validateAfterParsing=function(){return Promise.resolve()},t}(),h=function(){function t(){}var e=t.prototype;return e.validateBeforeParsing=function(){return Promise.resolve()},e.validateAfterParsing=function(t,e,r){var n=t.imageSizeRestrictions;return"DataURL"===t.readAs&&n&&v(e.type)?d(r.result,n):Promise.resolve()},t}(),v=function(t){return t.startsWith("image")},d=function(t,e){return new Promise((function(r,n){var o=new Image;o.onload=function(){var t=e.maxHeight,o=e.maxWidth,a=e.minHeight,c=e.minWidth,u=this.width,l=this.height,f={};t&&t<l&&(f=i({},f,{imageHeightTooBig:!0})),a&&a>l&&(f=i({},f,{imageHeightTooSmall:!0})),o&&o<u&&(f=i({},f,{imageWidthTooBig:!0})),c&&c>u&&(f=i({},f,{imageWidthTooSmall:!0})),Object.keys(f).length?n(f):r()},o.onerror=function(){n({imageNotLoaded:!0})},o.src=t}))},p=[new l,new s,new h];exports.useFilePicker=function(r){var a=r.accept,c=void 0===a?"*":a,l=r.multiple,f=void 0===l||l,s=r.readAs,h=void 0===s?"Text":s,v=r.minFileSize,d=r.maxFileSize,m=r.imageSizeRestrictions,y=r.limitFilesConfig,g=r.readFilesContent,w=void 0===g||g,x=r.validators,S=void 0===x?[]:x,b=t.useState([]),P=b[0],L=b[1],E=t.useState([]),F=E[0],j=E[1],z=t.useState([]),O=z[0],A=z[1],_=t.useState(!1),k=_[0],T=_[1],N=t.useState([]),R=N[0],C=N[1],G=t.useRef([]),I=function(){C([]),L([]),j([]),A([])};return t.useEffect((function(){if(0!==P.length){T(!0);var t=P.map((function(t){return new Promise(function(){var e=n(u.mark((function e(r,a){var l,s;return u.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:(l=new FileReader)["readAs"+h].call(l,t),s=function(e){var r=e.name,n=void 0===r?t.name:r,c=o(e,["name"]);a(i({name:n,fileSizeToolarge:!1,fileSizeTooSmall:!1},c))},l.onload=n(u.mark((function e(){var n;return u.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:n=p.concat(S).map((function(e){return e.validateAfterParsing({accept:c,multiple:f,readAs:h,minFileSize:v,maxFileSize:d,imageSizeRestrictions:m,limitFilesConfig:y,readFilesContent:w},t,l).catch((function(t){return Promise.reject(s(t))}))})),Promise.all(n).then((function(){return r({content:l.result,name:t.name,lastModified:t.lastModified})}));case 2:case"end":return e.stop()}}),e)}))),l.onerror=function(){s({readerError:l.error})};case 6:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}())}));Promise.all(t).then((function(t){j(t),C(G.current),A([])})).catch((function(t){A((function(e){return[t].concat(e)}))})).finally((function(){return T(!1)}))}else j([])}),[P]),[function(){!function(t,e,r){var n=document.createElement("input");n.type="file",n.accept=t,n.multiple=e,n.addEventListener("change",r),n.addEventListener("blur",r),n.dispatchEvent(new MouseEvent("click"))}(c instanceof Array?c.join(","):c,f,(function(t){I();var r=t.target;G.current=r.files?Array.from(r.files):[];var n=p.concat(S).map((function(t){return t.validateBeforeParsing({accept:c,multiple:f,readAs:h,minFileSize:v,maxFileSize:d,imageSizeRestrictions:m,limitFilesConfig:y,readFilesContent:w},G.current).catch((function(t){return Promise.reject(A((function(e){return[i({},t,e)]})))}))}));Promise.all(n).then((function(){w?e.fromEvent(t).then((function(t){L(t)})):C(G.current)}))}))},{filesContent:F,errors:O,loading:k,plainFiles:R,clear:I}]}; | ||
//# sourceMappingURL=use-file-picker.cjs.production.min.js.map |
@@ -995,4 +995,6 @@ import { useState, useRef, useEffect } from 'react'; | ||
inputElement.addEventListener('change', callback); // dispatch a click event to open the file dialog | ||
inputElement.addEventListener('change', callback); // set onblur event to call callback when user has selected file on Safari | ||
inputElement.addEventListener('blur', callback); // dispatch a click event to open the file dialog | ||
inputElement.dispatchEvent(new MouseEvent('click')); | ||
@@ -999,0 +1001,0 @@ } |
{ | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -29,2 +29,6 @@ # <center> Welcome to use-file-picker 👋 </center> | ||
## StoryBook | ||
https://use-file-picker-storybook-b0wsuz1yj-mrkampla.vercel.app/ | ||
## Usage | ||
@@ -31,0 +35,0 @@ |
@@ -16,4 +16,6 @@ export function openFileDialog(accept: string, multiple: boolean, callback: (arg: Event) => void) { | ||
inputElement.addEventListener('change', callback); | ||
// set onblur event to call callback when user has selected file on Safari | ||
inputElement.addEventListener('blur', callback); | ||
// dispatch a click event to open the file dialog | ||
inputElement.dispatchEvent(new MouseEvent('click')); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
304914
0.34%2561
0.2%354
1.14%