UNPKG

react-delay-input

Version:

React component that renders an Input with delayed onChange

1 lines 6.78 kB
(function(a,b){'object'==typeof exports&&'object'==typeof module?module.exports=b(require('React')):'function'==typeof define&&define.amd?define(['React'],b):'object'==typeof exports?exports.DelayInput=b(require('React')):a.DelayInput=b(a.React)})(this,function(a){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={i:d,l:!1,exports:{}};return a[d].call(e.exports,e,e.exports,b),e.l=!0,e.exports}var c={};return b.m=a,b.c=c,b.d=function(a,c,d){b.o(a,c)||Object.defineProperty(a,c,{configurable:!1,enumerable:!0,get:d})},b.n=function(a){var c=a&&a.__esModule?function(){return a['default']}:function(){return a};return b.d(c,'a',c),c},b.o=function(a,b){return Object.prototype.hasOwnProperty.call(a,b)},b.p='',b(b.s=0)}([function(a,b,c){'use strict';var d=c(1),e=d.DelayInput;e.DelayInput=e,a.exports=e},function(a,b,c){'use strict';function d(a,b){var c={};for(var d in a)0<=b.indexOf(d)||Object.prototype.hasOwnProperty.call(a,d)&&(c[d]=a[d]);return c}function e(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}function f(a,b){if(!a)throw new ReferenceError('this hasn\'t been initialised - super() hasn\'t been called');return b&&('object'==typeof b||'function'==typeof b)?b:a}function g(a,b){if('function'!=typeof b&&null!==b)throw new TypeError('Super expression must either be null or a function, not '+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)}Object.defineProperty(b,'__esModule',{value:!0}),c.d(b,'DelayInput',function(){return n});var h=c(2);var i=c.n(h);var j=c(3);var k=c.n(j);var l=Object.assign||function(a){for(var b=1;b<arguments.length;b++){var c=arguments[b];for(var d in c)Object.prototype.hasOwnProperty.call(c,d)&&(a[d]=c[d])}return a};var m=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,'value'in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();var n=function(a){function b(a){e(this,b);var c=f(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,a));return c.onChange=function(a){a.persist();var b=c.state.value;c.setState({value:a.target.value},function(){var d=c.state.value;return d.length>=c.props.minLength?void c.notify(a):void(b.length>d.length&&c.notify(l({},a,{target:l({},a.target,{value:''})})))})},c.onKeyDown=function(a){var b=c.props.onKeyDown;'Enter'===a.key&&c.forceNotify(a),b&&b(a)},c.onBlur=function(a){var b=c.props.onBlur;c.forceNotify(a),b&&b(a)},c.doNotify=function(){var a=c.props.onChange;a.apply(void 0,arguments)},c.forceNotify=function(a){if(c.isDebouncing){c.cancel&&c.cancel();var b=c.state.value;var d=c.props.minLength;b.length>=d?c.doNotify(a):c.doNotify(l({},a,{target:l({},a.target,{value:b})}))}},c.state={value:a.value||''},c.isDebouncing=!1,c}return g(b,a),m(b,[{key:'componentDidMount',value:function(){this.createNotifier(this.props)}},{key:'componentDidUpdate',value:function(a){if(!this.isDebouncing){var b=this.props,c=b.delayMax,d=b.delayTimeout,e=b.leadingNotify,f=b.trailingNotify,g=b.value;var h=a.delayMax,i=a.delayTimeout,j=a.leadingNotify,k=a.trailingNotify,l=a.value;'undefined'!=typeof g&&l!==g&&this.setState({value:g}),(h!==c||i!==d||j!==e||k!==f)&&this.createNotifier(this.props)}}},{key:'componentWillUnmount',value:function(){this.flush&&this.flush()}},{key:'createNotifier',value:function(a){var b=this;var c=a.delayMax,d=a.delayTimeout,e=a.leadingNotify,f=a.trailingNotify;if(0>d)this.notify=function(){return null};else if(0===d)this.notify=this.doNotify;else{var g={leading:e,trailing:f};void 0!==c&&null!==c&&(g.maxWait=c);var h=k()(function(a){b.isDebouncing=!1,b.doNotify(a)},d,g);this.notify=function(a){b.isDebouncing=!0,h(a)},this.flush=function(){return h.flush()},this.cancel=function(){b.isDebouncing=!1,h.cancel()}}}},{key:'render',value:function(){var a=this.props,b=a.element,c=a.forceNotifyByEnter,e=a.forceNotifyOnBlur,f=a.onKeyDown,g=a.onBlur,h=a.inputRef,j=a.delayMax,k=a.delayTimeout,m=a.leadingNotify,n=a.minLength,o=a.onChange,p=a.trailingNotify,q=a.value,r=d(a,['element','forceNotifyByEnter','forceNotifyOnBlur','onKeyDown','onBlur','inputRef','delayMax','delayTimeout','leadingNotify','minLength','onChange','trailingNotify','value']);var s=c?{onKeyDown:this.onKeyDown}:f?{onKeyDown:f}:{};var t=e?{onBlur:this.onBlur}:g?{onBlur:g}:{};var u=h?{ref:h}:{};return i.a.createElement(b,l({},r,{onChange:this.onChange,value:this.state.value},s,t,u))}}]),b}(i.a.PureComponent);n.defaultProps={element:'input',type:'text',onKeyDown:void 0,onBlur:void 0,value:void 0,minLength:0,delayMax:void 0,delayTimeout:100,forceNotifyByEnter:!0,forceNotifyOnBlur:!0,inputRef:void 0,leadingNotify:!1,trailingNotify:!0}},function(b){b.exports=a},function(a,b,c){(function(b){function c(a){var b=typeof a;return!!a&&('object'==b||'function'==b)}function d(a){return!!a&&'object'==typeof a}function e(a){return'symbol'==typeof a||d(a)&&r.call(a)==h}function f(a){if('number'==typeof a)return a;if(e(a))return g;if(c(a)){var b='function'==typeof a.valueOf?a.valueOf():a;a=c(b)?b+'':b}if('string'!=typeof a)return 0===a?a:+a;a=a.replace(i,'');var d=k.test(a);return d||l.test(a)?m(a.slice(2),d?2:8):j.test(a)?g:+a}var g=0/0;var h='[object Symbol]';var i=/^\s+|\s+$/g;var j=/^[-+]0x[0-9a-f]+$/i;var k=/^0b[01]+$/i;var l=/^0o[0-7]+$/i;var m=parseInt;var n='object'==typeof b&&b&&b.Object===Object&&b;var o='object'==typeof self&&self&&self.Object===Object&&self;var p=n||o||Function('return this')();var q=Object.prototype;var r=q.toString;var s=Math.max,t=Math.min;var u=function(){return p.Date.now()};a.exports=function(a,b,d){function e(b){var c=m,d=n;return m=n=void 0,v=b,p=a.apply(d,c),p}function g(a){return v=a,q=setTimeout(j,b),w?e(a):p}function h(a){var c=a-r,d=a-v,e=b-c;return x?t(e,o-d):e}function i(a){var c=a-r,d=a-v;return void 0==r||c>=b||0>c||x&&d>=o}function j(){var a=u();return i(a)?k(a):void(q=setTimeout(j,h(a)))}function k(a){return(q=void 0,y&&m)?e(a):(m=n=void 0,p)}function l(){var a=u(),c=i(a);if(m=arguments,n=this,r=a,c){if(void 0===q)return g(r);if(x)return q=setTimeout(j,b),e(r)}return void 0===q&&(q=setTimeout(j,b)),p}var m,n,o,p,q,r,v=0,w=!1,x=!1,y=!0;if('function'!=typeof a)throw new TypeError('Expected a function');return b=f(b)||0,c(d)&&(w=!!d.leading,x='maxWait'in d,o=x?s(f(d.maxWait)||0,b):o,y='trailing'in d?!!d.trailing:y),l.cancel=function(){void 0!==q&&clearTimeout(q),v=0,m=r=n=q=void 0},l.flush=function(){return void 0===q?p:k(u())},l}}).call(b,c(4))},function(a){var b=function(){return this}();try{b=b||Function('return this')()||(1,eval)('this')}catch(a){'object'==typeof window&&(b=window)}a.exports=b}])});