react-values
Version:
A set of tiny, composable React components for handling state with render props.
2 lines (1 loc) • 11.2 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t(e.ReactValues={},e.React)}(this,function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},u=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t};function a(e){return function(t){function a(t,r){n(this,a);var o=i(this,(a.__proto__||Object.getPrototypeOf(a)).call(this,t)),u=t.value,c=t.defaultValue,s=t.store,l=!s,f=!1;return s||(void 0!==u?(s=new e(u,t),f=!0):s=new e(c,t)),o.state={connected:l,controlled:f,store:s,value:s.value},o}return u(a,t),r(a,[{key:"componentDidMount",value:function(){var e=this,t=this.state,n=t.controlled,r=t.store;this.mounted=!0,this.unsubscribe=r.on(function(t){e.mounted&&(n?e.onChange(t):e.setState({value:t},function(){return e.onChange(e.state.value)}))})}},{key:"componentWillUnmount",value:function(){this.mounted=!1,this.unsubscribe()}},{key:"onChange",value:function(e){this.props.onChange&&this.props.onChange(e)}},{key:"render",value:function(){var e=this.props,t=e.children,n=e.render,r=e.disabled,u=void 0!==r&&r,i=this.state,a=i.controlled,c=i.connected,s=i.store,l=a?this.props.value:this.state.value,f=u?s.noops:s.transforms,p=t||n;if(a&&(s.value=l),c||(s.props=this.props),null===p)return null;var h=o({value:l,disabled:u},f);for(var d in s.computeds)h[d]=s.computeds[d]();return"function"==typeof p?p(h):p}}]),a}(t.Component)}function c(e,a){return function(c,s){var l=new e(c,s),f=function(e){function c(){return n(this,c),i(this,(c.__proto__||Object.getPrototypeOf(c)).apply(this,arguments))}return u(c,e),r(c,[{key:"render",value:function(){return t.createElement(a,o({},this.props,{store:l}))}}],[{key:"value",get:function(){return l.value}}]),c}(t.Component);for(var p in l.transforms)f[p]=l.transforms[p];return f.store=l,f.displayName="Connected"+a.displayName,f}}var s=function(){function e(t){var r=this,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},u=arguments[2];n(this,e),this.callbacks=[],this.value=void 0===t?u:t,this.transforms={},this.noops={},this.computeds={},this.props=o,this.define("set",function(e,t){return t}),this.define("reset",function(){return r.clone(t)}),this.define("clear",function(){return r.clone(u)}),o.onChange&&this.on(o.onChange)}return r(e,[{key:"on",value:function(e){var t=this,n=this.callbacks.push(e)-1;return function(){return t.callbacks[n]=null}}},{key:"clone",value:function(e){return e}},{key:"transform",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=this.value,r=this.callbacks,o=t.mutates,u=void 0!==o&&o?this.clone(n):n,i="function"==typeof e?e(u):e;this.value=i,r.forEach(function(e){return e&&e(i)})}},{key:"define",value:function(e,t,n){var r=this;this.noops[e]=function(){},this.transforms[e]=function(){for(var e=arguments.length,o=Array(e),u=0;u<e;u++)o[u]=arguments[u];r.transform(function(e){return t.apply(void 0,[e].concat(o))},n)}}},{key:"compute",value:function(e,t){var n=this;this.computeds[e]=function(){return t(n.value)}}},{key:"proxy",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.alias,r=void 0===n?e:n,o=t.mutates,u=void 0!==o&&o;this.define(r,function(t){for(var n=arguments.length,r=Array(n>1?n-1:0),o=1;o<n;o++)r[o-1]=arguments[o];var i=t[e].apply(t,r);return u?t:i},t)}}]),e}(),l=a(s),f=c(s,l),p=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,[]));return o.compute("first",function(e){return e[0]}),o.compute("last",function(e){return e[Math.max(0,e.length-1)]}),o.proxy("concat"),o.proxy("fill",{mutates:!0}),o.proxy("filter"),o.proxy("flat"),o.proxy("flatMap"),o.proxy("map"),o.proxy("pop",{mutates:!0}),o.proxy("push",{mutates:!0}),o.proxy("reverse",{mutates:!0}),o.proxy("shift",{mutates:!0}),o.proxy("slice"),o.proxy("sort",{mutates:!0}),o.proxy("splice",{mutates:!0}),o.proxy("unshift",{mutates:!0}),o}return u(t,s),r(t,[{key:"clone",value:function(e){return e.slice()}}]),t}(),h=a(p),d=c(p,h),v=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,!1));return o.define("toggle",function(e){return!e}),o}return u(t,s),t}(),y=a(v),m=c(v,y),g=1e3,x=6e4,_=36e5,b=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,new Date));return o.compute("date",function(e){return e.getDate()}),o.compute("hours",function(e){return e.getHours()}),o.compute("milliseconds",function(e){return e.getMilliseconds()}),o.compute("minutes",function(e){return e.getMinutes()}),o.compute("month",function(e){return e.getMonth()}),o.compute("seconds",function(e){return e.getSeconds()}),o.compute("year",function(e){return e.getFullYear()}),o.define("setMonth",k),o.proxy("setDate",{mutates:!0}),o.proxy("setFullYear",{alias:"setYear",mutates:!0}),o.proxy("setFullYear",{mutates:!0}),o.proxy("setHours",{mutates:!0}),o.proxy("setMilliseconds",{mutates:!0}),o.proxy("setMinutes",{mutates:!0}),o.proxy("setSeconds",{mutates:!0}),o.define("incrementDate",function(e){return M(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)}),o.define("incrementHours",function(e){return O(e,(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*_)}),o.define("incrementMilliseconds",function(e){return O(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)}),o.define("incrementMinutes",function(e){return O(e,(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*x)}),o.define("incrementMonth",function(e){return w(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)}),o.define("incrementSeconds",function(e){return O(e,(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*g)}),o.define("incrementYear",function(e){return w(e,12*(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1))}),o.define("decrementDate",function(e){return M(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1))}),o.define("decrementHours",function(e){return O(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*_)}),o.define("decrementMilliseconds",function(e){return O(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1))}),o.define("decrementMinutes",function(e){return O(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*x)}),o.define("decrementMonth",function(e){return w(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1))}),o.define("decrementSeconds",function(e){return O(e,0-(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1)*g)}),o.define("decrementYear",function(e){return w(e,0-12*(arguments.length>1&&void 0!==arguments[1]?arguments[1]:1))}),o}return u(t,s),r(t,[{key:"clone",value:function(e){return new Date(e.getTime())}}]),t}();function O(e,t){return new Date(e.getTime()+t)}function M(e,t){var n=new Date(e.getTime());return n.setDate(n.getDate()+t),n}function w(e,t){var n=e.getFullYear(),r=e.getMonth()+t,o=new Date(0);o.setFullYear(n,r,1),o.setHours(0,0,0,0);var u=j(o);return e.setMonth(r,Math.min(u,e.getDate())),e}function k(e,t){var n=new Date(e.getTime()),r=n.getFullYear(),o=n.getDate(),u=new Date(0);u.setFullYear(r,t,15),u.setHours(0,0,0,0);var i=j(u);return n.setMonth(t,Math.min(o,i)),n}function j(e){var t=e.getFullYear(),n=e.getMonth(),r=new Date(0);return r.setFullYear(t,n+1,0),r.setHours(0,0,0,0),r.getDate()}var D=a(b),V=c(b,D),P=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,new Map));return o.define("set",function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var o=n[0];return 1===n.length?"function"==typeof o?o(e):o:e.set.apply(e,n)}),o.proxy("clear",{mutates:!0}),o.proxy("delete",{mutates:!0}),o.proxy("delete",{alias:"unset",mutates:!0}),o}return u(t,s),r(t,[{key:"clone",value:function(e){return new Map(e)}}]),t}(),S=a(P),C=c(P,S),E=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,0));return o.define("increment",function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,n="number"!=typeof t?1:t;return Math.min(e+n,o.props.max)}),o.define("decrement",function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,n="number"!=typeof t?1:t;return Math.max(e-n,o.props.min)}),o}return u(t,s),t}(),Y=a(E),F=c(E,Y);Y.defaultProps={max:Number.MAX_SAFE_INTEGER,min:Number.MIN_SAFE_INTEGER};var A=function(e){function t(e,r){n(this,t);var u=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,{}));return u.define("set",function(e){for(var t=arguments.length,n=Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var u=n[0];if(1===n.length)return"function"==typeof u?u(e):u;var i=n[0],a=n[1],c=o({},e);return c[i]=a,c}),u.define("assign",function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return o({},e,t)}),u.define("clear",function(){return{}}),u.define("delete",N),u.define("unset",N),u}return u(t,s),r(t,[{key:"clone",value:function(e){return o({},e)}}]),t}();function N(e,t){var n=o({},e);return delete n[t],n}var T=a(A),H=c(A,T),R=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,new Set));return o.define("toggle",function(e,t,n){return n?e.add(t):(e.delete(t),e)},{mutates:!0}),o.proxy("add"),o.proxy("clear",{mutates:!0}),o.proxy("delete",{mutates:!0}),o.proxy("delete",{alias:"remove",mutates:!0}),o}return u(t,s),r(t,[{key:"clone",value:function(e){return new Set(e)}}]),t}(),I=a(R),B=c(R,I),G=function(e){function t(e,r){n(this,t);var o=i(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,r,""));return o.proxy("concat"),o.proxy("normalize"),o.proxy("padEnd"),o.proxy("padStart"),o.proxy("repeat"),o.proxy("replace"),o.proxy("slice"),o.proxy("substr"),o.proxy("substring"),o.proxy("toLowerCase"),o.proxy("toUpperCase"),o.proxy("trim"),o.proxy("trimEnd"),o.proxy("trimStart"),o}return u(t,s),t}(),U=a(G),q=c(G,U);e.ArrayValue=h,e.createArrayValue=d,e.BooleanValue=y,e.createBooleanValue=m,e.DateValue=D,e.createDateValue=V,e.MapValue=S,e.createMapValue=C,e.NumberValue=Y,e.createNumberValue=F,e.ObjectValue=T,e.createObjectValue=H,e.SetValue=I,e.createSetValue=B,e.StringValue=U,e.createStringValue=q,e.Value=l,e.createValue=f,Object.defineProperty(e,"__esModule",{value:!0})});