react-powerplug
Version:
Give life to your dumb components
2 lines (1 loc) • 9.06 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(n.ReactPowerPlug={},n.React)}(this,function(n,a){"use strict";function i(n,t){if(null==n)return{};var e,r,o={},u=Object.keys(n);for(r=0;r<u.length;r++)t.indexOf(e=u[r])<0&&(o[e]=n[e]);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(n);for(r=0;r<i.length;r++)t.indexOf(e=i[r])<0&&Object.prototype.propertyIsEnumerable.call(n,e)&&(o[e]=n[e])}return o}function c(n,t,e){return t in n?Object.defineProperty(n,t,{value:e,enumerable:!0,configurable:!0,writable:!0}):n[t]=e,n}function l(t){for(var n=1;n<arguments.length;n++){var e=null!=arguments[n]?arguments[n]:{},r=Object.keys(e);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(e).filter(function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.forEach(function(n){c(t,n,e[n])})}return t}function e(n,t){n.prototype=Object.create(t.prototype),(n.prototype.constructor=n).__proto__=t}function f(n){if(void 0===n)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return n}var s=function(n){for(var t=n.children,e="function"==typeof t?t:n.render,r=arguments.length,o=Array(1<r?r-1:0),u=1;u<r;u++)o[u-1]=arguments[u];return e?e.apply(void 0,o):null},v=function(){},h=function(u){function n(){for(var n,o,t=arguments.length,e=Array(t),r=0;r<t;r++)e[r]=arguments[r];return n=o=u.call.apply(u,[this].concat(e))||this,c(f(f(o)),"state",l({},o.props.initial)),c(f(f(o)),"_setState",function(n,t){void 0===t&&(t=v);var e=o.props.onChange,r=void 0===e?v:e;o.setState(n,function(){r(o.state),t()})}),n||f(o)}return e(n,u),n.prototype.render=function(){return s(this.props,{state:this.state,setState:this._setState})},n}(a.Component),d=function(t,e){return void 0===t&&(t=v),function(n){t(n[e])}},r=function(){for(var n=arguments.length,t=Array(n),e=0;e<n;e++)t[e]=arguments[e];var r=t.reverse();return function(i){return function t(e,r,o){void 0===o&&(o=[]);var n=r[e],u=0===e;return("function"==typeof n.type?a.cloneElement:a.createElement)(n,{},function(n){return u?s.apply(void 0,[i].concat(o.concat(n))):t(e-1,r,o.concat(n))})}(t.length-1,r)}},p=function(n,t){return"function"==typeof n?n(t):n},u=function(t){return function(n){return{count:n.count+t}}},t=function(u){function n(){for(var n,t,e=arguments.length,r=Array(e),o=0;o<e;o++)r[o]=arguments[o];return n=t=u.call.apply(u,[this].concat(r))||this,c(f(f(t)),"state",{times:0}),c(f(f(t)),"intervalId",void 0),c(f(f(t)),"_clearIntervalIfNecessary",function(){t.intervalId&&(t.intervalId=clearInterval(t.intervalId))}),c(f(f(t)),"_setIntervalIfNecessary",function(n){Number.isFinite(n)&&(t.intervalId=setInterval(function(){return t.setState(function(n){return{times:n.times+1}})},n))}),c(f(f(t)),"stop",function(){t._clearIntervalIfNecessary()}),c(f(f(t)),"start",function(n){t._setIntervalIfNecessary("number"==typeof n?n:null!=t.props.delay?t.props.delay:1e3)}),c(f(f(t)),"toggle",function(){t.intervalId?t.stop():t.start()}),n||f(t)}e(n,u);var t=n.prototype;return t.componentDidMount=function(){this.start()},t.componentDidUpdate=function(n){n.delay!==this.props.delay&&(this.stop(),this.start())},t.componentWillUnmount=function(){this.stop()},t.render=function(){return s(this.props,{start:this.start,stop:this.stop,toggle:this.toggle})},n}(a.Component),g=function(n,t){return-1!==n.indexOf(t)};n.Active=function(n){var t=n.onChange,e=i(n,["onChange"]);return a.createElement(h,{initial:{active:!1},onChange:d(t,"active")},function(n){var t=n.setState;return s(e,{active:n.state.active,bind:{onMouseDown:function(){return t({active:!0})},onMouseUp:function(){return t({active:!1})}}})})},n.Compose=function(n){var t=n.components,e=i(n,["components"]);return r.apply(void 0,t)(e)},n.Counter=function(n){var t=n.initial,e=void 0===t?0:t,r=n.onChange,o=i(n,["initial","onChange"]);return a.createElement(h,{initial:{count:e},onChange:d(r,"count")},function(n){var e=n.setState;return s(o,{count:n.state.count,inc:function(){return e(u(1))},dec:function(){return e(u(-1))},incBy:function(n){return e(u(n))},decBy:function(n){return e(u(-n))},set:function(t){return e(function(n){return{count:p(t,n.count)}})}})})},n.Focus=function(n){var t=n.onChange,e=i(n,["onChange"]);return a.createElement(h,{initial:{focused:!1},onChange:d(t,"focused")},function(n){var t=n.setState;return s(e,{focused:n.state.focused,bind:{onFocus:function(){return t({focused:!0})},onBlur:function(){return t({focused:!1})}}})})},n.FocusManager=function(n){var t=n.onChange,r=i(n,["onChange"]),o=!0;return a.createElement(h,{initial:{focused:!1},onChange:d(t,"focused")},function(n){var t=n.state,e=n.setState;return s(r,{focused:t.focused,blur:function(){t.focused&&document.activeElement.blur()},bind:{tabIndex:-1,onBlur:function(){o&&e({focused:!1})},onFocus:function(){e({focused:!0})},onMouseDown:function(){o=!1},onMouseUp:function(){o=!0}}})})},n.Form=function(n){var t=n.initial,e=void 0===t?{}:t,r=n.onChange,u=i(n,["initial","onChange"]);return a.createElement(h,{initial:l({},e),onChange:r},function(n){var t=n.state,o=n.setState;return s(u,{values:l({},t),input:function(r){var n=t[r]||"";return{bind:{onChange:function(n){return o(((t={})[r]=n.target.value,t));var t},value:n},set:function(e){return o(function(n){var t;return(t={})[r]=p(e,n.value),t})},value:n}}})})},n.Hover=function(n){var t=n.onChange,e=i(n,["onChange"]);return a.createElement(h,{initial:{hovered:!1},onChange:d(t,"hovered")},function(n){var t=n.setState;return s(e,{hovered:n.state.hovered,bind:{onMouseEnter:function(){return t({hovered:!0})},onMouseLeave:function(){return t({hovered:!1})}}})})},n.Input=function(n){var t=n.initial,e=void 0===t?"":t,r=n.onChange,o=i(n,["initial","onChange"]);return a.createElement(h,{initial:{value:e},onChange:d(r,"value")},function(n){var t=n.state,e=n.setState;return s(o,{bind:{onChange:function(n){return e({value:n.target.value})},value:t.value},set:function(t){return e(function(n){return{value:p(t,n.value)}})},value:t.value})})},n.Interval=t,n.List=function(n){var t=n.initial,e=void 0===t?[]:t,r=n.onChange,o=i(n,["initial","onChange"]);return a.createElement(h,{initial:{list:e},onChange:d(r,"list")},function(n){var t=n.state,r=n.setState;return s(o,{list:t.list,first:function(){return t.list[0]},last:function(){return t.list[Math.max(0,t.list.length-1)]},set:function(t){return r(function(n){return{list:p(t,n.list)}})},push:function(){for(var n=arguments.length,t=Array(n),e=0;e<n;e++)t[e]=arguments[e];return r(function(n){return{list:n.list.concat(t)}})},pull:function(e){return r(function(n){return{list:n.list.filter((t=e,function(){return!t.apply(void 0,arguments)}))};var t})},sort:function(t){return r(function(n){return{list:n.list.concat().sort(t)}})}})})},n.Map=function(n){var t=n.initial,e=void 0===t?{}:t,r=n.onChange,u=i(n,["initial","onChange"]);return a.createElement(h,{initial:l({},e),onChange:r},function(n){var t=n.state,o=n.setState;return s(u,{values:t,set:function(n,t){var e;return o(((e={})[n]=t,e))},over:function(e,r){return o(function(n){var t;return(t={})[e]=r(n[e]),t})},get:function(n){return t[n]}})})},n.Set=function(n){var e,t=n.initial,r=void 0===t?[]:t,o=n.onChange,u=i(n,["initial","onChange"]);return a.createElement(h,{initial:{values:(e=r,e.filter(function(n,t){return e.indexOf(n)===t}))},onChange:d(o,"values")},function(n){var r=n.state,o=n.setState;return s(u,{values:r.values,add:function(n){return o({values:(t=r.values,e=n,g(t,e)?t:t.concat([e]))});var t,e},clear:function(){return o({values:[]})},remove:function(n){return o({values:(t=r.values,e=n,g(t,e)?t.filter(function(n){return n!==e}):t)});var t,e},has:function(n){return g(r.values,n)}})})},n.State=h,n.Toggle=function(n){var t=n.initial,e=void 0!==t&&t,r=n.onChange,o=i(n,["initial","onChange"]);return a.createElement(h,{initial:{on:e},onChange:d(r,"on")},function(n){var e=n.setState;return s(o,{on:n.state.on,toggle:function(){return e(function(n){return{on:!n.on}})},set:function(t){return e(function(n){return{on:p(t,n.on)}})}})})},n.Touch=function(n){var t=n.onChange,e=i(n,["onChange"]);return a.createElement(h,{initial:{touched:!1},onChange:d(t,"touched")},function(n){var t=n.setState;return s(e,{touched:n.state.touched,bind:{onTouchStart:function(){return t({touched:!0})},onTouchEnd:function(){return t({touched:!1})}}})})},n.Value=function(n){var t=n.initial,e=n.onChange,r=i(n,["initial","onChange"]);return a.createElement(h,{initial:{value:t},onChange:d(e,"value")},function(n){var e=n.setState;return s(r,{value:n.state.value,set:function(t){return e(function(n){return{value:p(t,n.value)}})}})})},n.compose=r,n.composeEvents=function(){for(var n=arguments.length,t=Array(n),e=0;e<n;e++)t[e]=arguments[e];return t.reverse().reduce(function(t,e){var r={},n=function(n){r[n]=t[n]?function(){e[n].apply(e,arguments),t[n].apply(t,arguments)}:e[n]};for(var o in e)n(o);return l({},t,r)})},n.renderProps=s,Object.defineProperty(n,"__esModule",{value:!0})});