UNPKG

@auraxy/react-async-button

Version:

A button component of react, realizes the button auto-lock when it clicked and auto-unlock after the async action has done

10 lines (8 loc) 2.62 kB
/** * Bundle of @auraxy/react-async-button * Generated: 2019-10-24 * Version: 1.0.6 * License: MIT * Author: 2631541504@qq.com */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t=t||self).ReactAsyncButton={},t.React)}(this,function(m,O){"use strict";var t,j="default"in O?O.default:O;function N(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],r=!0,a=!1,c=void 0;try{for(var o,i=t[Symbol.iterator]();!(r=(o=i.next()).done)&&(n.push(o.value),!e||n.length!==e);r=!0);}catch(t){a=!0,c=t}finally{try{r||null==i.return||i.return()}finally{if(a)throw c}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function P(t,e){if(null==t)return{};var n,r,a=function(t,e){if(null==t)return{};var n,r,a={},c=Object.keys(t);for(r=0;r<c.length;r++)n=c[r],0<=e.indexOf(n)||(a[n]=t[n]);return a}(t,e);if(Object.getOwnPropertySymbols){var c=Object.getOwnPropertySymbols(t);for(r=0;r<c.length;r++)n=c[r],0<=e.indexOf(n)||Object.prototype.propertyIsEnumerable.call(t,n)&&(a[n]=t[n])}return a}function k(t,e){var n=N(O.useState(m.Status.Normal),2),r=n[0],a=n[1],c=O.useRef(!1);c.current=c.current||r!==m.Status.Normal;var o=t.children,i=!e&&c.current,u="";switch(r){case m.Status.Pending:t.loadingContent&&(o=t.loadingContent),u="btn-pending",i=!0;break;case m.Status.Failed:t.failedContent&&(o=t.failedContent),u="btn-failed";break;case m.Status.Success:t.successContent&&(o=t.successContent),u="btn-success"}return[o,i,u,a]}(t=m.Status||(m.Status={}))[t.Normal=0]="Normal",t[t.Pending=1]="Pending",t[t.Success=2]="Success",t[t.Failed=3]="Failed";m.ReactAsyncButton=function(t){var e=t.children,n=t.canUseAgain,r=void 0===n||n,a=t.loadingContent,c=void 0===a?"loading...":a,o=t.successContent,i=void 0===o?"":o,u=t.failedContent,s=void 0===u?"":u,l=t.onClick,d=t.disabled,f=t.className,b=void 0===f?"":f,y=P(t,["children","canUseAgain","loadingContent","successContent","failedContent","onClick","disabled","className"]),g=N(k({children:e,loadingContent:c,successContent:i,failedContent:s},r),4),S=g[0],v=g[1],C=g[2],p=g[3],h=O.useRef(!1);O.useEffect(function(){return function(){h.current=!0}},[]);return j.createElement("button",Object.assign({},y,{className:"".concat(b," ").concat(C),onClick:function(t){p(m.Status.Pending),l(t).then(function(){h.current||p(m.Status.Success)}).catch(function(){h.current||p(m.Status.Failed)})},disabled:d||v}),S)},m.useButtonStatus=k,Object.defineProperty(m,"__esModule",{value:!0})});