UNPKG

@jswork/react-sms-send

Version:

Sms button for react.

2 lines 3.33 kB
var b=Object.defineProperty;var l=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,y=Object.prototype.propertyIsEnumerable;var f=(n,e,t)=>e in n?b(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,C=(n,e)=>{for(var t in e||(e={}))x.call(e,t)&&f(n,t,e[t]);if(l)for(var t of l(e))y.call(e,t)&&f(n,t,e[t]);return n};var a=(n,e)=>b(n,"name",{value:e,configurable:!0});var E=(n,e)=>{var t={};for(var s in n)x.call(n,s)&&e.indexOf(s)<0&&(t[s]=n[s]);if(n!=null&&l)for(var s of l(n))e.indexOf(s)<0&&y.call(n,s)&&(t[s]=n[s]);return t};var o=(n,e,t)=>f(n,typeof e!="symbol"?e+"":e,t);var D=(n,e,t)=>new Promise((s,i)=>{var c=r=>{try{h(t.next(r))}catch(m){i(m)}},u=r=>{try{h(t.throw(r))}catch(m){i(m)}},h=r=>r.done?s(r.value):Promise.resolve(r.value).then(c,u);h((t=t.apply(n,e)).next())});import I from"classnames";import U,{Component as A}from"react";import{ReactHarmonyEvents as M}from"@jswork/harmony-events";var g="react-sms-send",p=class p extends A{constructor(){super(...arguments);o(this,"timer");o(this,"harmonyEvents",null);o(this,"state",{status:"init",count:this.props.count});o(this,"handleClick",a(t=>D(this,null,function*(){let{onBeforeSend:s,onClick:i}=this.props,{status:c}=this.state,u=yield s==null?void 0:s();this.isDisabled||!u||(c==="init"&&this.sending(),c==="done"&&this.resend(),i==null||i(t))}),"handleClick"));o(this,"reset",a(t=>{this.setState({status:"init",count:this.props.count},t),this.clear()},"reset"));o(this,"sending",a(()=>{let{min:t}=this.props;this.setState({status:"loading"}),this.timer=setInterval(()=>{let{count:s}=this.state;s===t?(this.clear(),this.setState({status:"done"})):this.setState({count:s-1})},1e3)},"sending"));o(this,"resend",a(()=>{this.reset(this.sending)},"resend"));o(this,"clear",a(()=>{this.timer&&clearInterval(this.timer)},"clear"))}get isDisabled(){let{disabled:t}=this.props,{status:s}=this.state;return s==="loading"||t}get template(){let{template:t}=this.props,{status:s,count:i}=this.state;return t==null?void 0:t({status:s,count:i})}componentDidMount(){this.harmonyEvents=M.create(this)}componentDidUpdate(t,s){var h,r;let i=s.count!==this.state.count,c=s.status!==this.state.status,u=t.count!==this.props.count;(i||c)&&((r=(h=this.props).onChange)==null||r.call(h,this.state)),u&&this.setState({count:this.props.count})}componentWillUnmount(){var t;this.reset(),this.clear(),(t=this.harmonyEvents)==null||t.destroy()}render(){let v=this.props,{className:t,children:s,as:i,asProps:c,count:u,min:h,template:r,onBeforeSend:m,onChange:w,onClick:B,disabled:H}=v,N=E(v,["className","children","as","asProps","count","min","template","onBeforeSend","onChange","onClick","disabled"]),k=i;return U.createElement(k,C(C({disabled:this.isDisabled,onClick:this.handleClick,"data-component":g,className:I(g,t)},N),c),this.template)}};a(p,"ReactSmsSend"),o(p,"displayName",g),o(p,"version","__VERSION__"),o(p,"event"),o(p,"events",["reset","resend"]),o(p,"defaultProps",{name:"@",as:"button",count:30,min:1,onBeforeSend:a(()=>!0,"onBeforeSend")});var d=p;var P=a(n=>{let e=n||"@",t=a((c,...u)=>{var h;return(h=d.event)==null?void 0:h.emit(`${e}:${c}`,...u)},"execute");return{reset:a(()=>t("reset"),"reset"),resend:a(()=>t("resend"),"resend")}},"useCommand"),$=P;var Q=d;export{Q as default,$ as useCommand}; //# sourceMappingURL=main.esm.js.map