uncontrollable-input
Version:
Wrapper to easilly create strong controllable/uncontrollable inputs
2 lines (1 loc) • 7.43 kB
JavaScript
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).uncontrolledInput=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n||e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){(function(process){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source)Object.prototype.hasOwnProperty.call(source,key)&&(target[key]=source[key])}return target},_createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){return protoProps&&defineProperties(Constructor.prototype,protoProps),staticProps&&defineProperties(Constructor,staticProps),Constructor}}(),_react=require("react");var getEventValue=function(event){var target=void 0;return null==event||null==(target=event.target)?event:"input"===target.nodeName.toLowerCase()&&"checkbox"===target.type.toLowerCase()?target.checked:target.value},noop=function(){};exports.default=function(options){return function(ControlledInput){var UncontrollableInput=function(_PureComponent){!function(subClass,superClass){if("function"!=typeof superClass&&null!==superClass)throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:!1,writable:!0,configurable:!0}}),superClass&&(Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass)}(UncontrollableInput,_react.PureComponent);function UncontrollableInput(props){!function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}(this,UncontrollableInput);var _this=function(self,call){if(!self)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!call||"object"!=typeof call&&"function"!=typeof call?self:call}(this,(UncontrollableInput.__proto__||Object.getPrototypeOf(UncontrollableInput)).call(this)),opts="function"==typeof options?options(props):options;if(_this._controlled="value"in props){if("production"!==process.env.NODE_ENV&&"defaultValue"in props)throw new Error(_this.constructor.name+": controlled component should not have a default value")}else{var _props$defaultValue=props.defaultValue,defaultValue=void 0===_props$defaultValue?opts&&opts.defaultValue:_props$defaultValue;_this.state={value:defaultValue},_this._onChange=function(event){_this.props.onChange(event),null!=event&&event.defaultPrevented||_this.setState({value:getEventValue(event)})}}return _this}return _createClass(UncontrollableInput,[{key:"render",value:function(){var props=this.props;return this._controlled||delete(props=_extends({},props,{onChange:this._onChange,value:this.state.value})).defaultValue,(0,_react.createElement)(ControlledInput,props)}},{key:"value",get:function(){return this._controlled?this.props.value:this.state.value},set:function(value){if("production"!==process.env.NODE_ENV&&this._controlled)throw new Error(this.constructor.name+": should not set value on controlled component");this.setState({value:value})}}]),UncontrollableInput}();return"production"!==process.env.NODE_ENV&&(UncontrollableInput.prototype.componentWillReceiveProps=function(newProps){var name=this.constructor.name,newControlled="value"in newProps;if(this._controlled){if(!newControlled)throw new Error(name+": controlled component should not become uncontrolled")}else if(newControlled)throw new Error(name+": uncontrolled component should not become controlled");if(newProps.defaultValue!==this.props.defaultValue)throw new Error(name+": default value should not change")}),UncontrollableInput.defaultProps={onChange:noop},UncontrollableInput}}}).call(this,require("_process"))},{_process:2,react:"react"}],2:[function(require,module,exports){var cachedSetTimeout,cachedClearTimeout,process=module.exports={};function defaultSetTimout(){throw new Error("setTimeout has not been defined")}function defaultClearTimeout(){throw new Error("clearTimeout has not been defined")}!function(){try{cachedSetTimeout="function"==typeof setTimeout?setTimeout:defaultSetTimout}catch(e){cachedSetTimeout=defaultSetTimout}try{cachedClearTimeout="function"==typeof clearTimeout?clearTimeout:defaultClearTimeout}catch(e){cachedClearTimeout=defaultClearTimeout}}();function runTimeout(fun){if(cachedSetTimeout===setTimeout)return setTimeout(fun,0);if((cachedSetTimeout===defaultSetTimout||!cachedSetTimeout)&&setTimeout)return cachedSetTimeout=setTimeout,setTimeout(fun,0);try{return cachedSetTimeout(fun,0)}catch(e){try{return cachedSetTimeout.call(null,fun,0)}catch(e){return cachedSetTimeout.call(this,fun,0)}}}var currentQueue,queue=[],draining=!1,queueIndex=-1;function cleanUpNextTick(){draining&¤tQueue&&(draining=!1,currentQueue.length?queue=currentQueue.concat(queue):queueIndex=-1,queue.length&&drainQueue())}function drainQueue(){if(!draining){var timeout=runTimeout(cleanUpNextTick);draining=!0;for(var len=queue.length;len;){for(currentQueue=queue,queue=[];++queueIndex<len;)currentQueue&¤tQueue[queueIndex].run();queueIndex=-1,len=queue.length}currentQueue=null,draining=!1,function(marker){if(cachedClearTimeout===clearTimeout)return clearTimeout(marker);if((cachedClearTimeout===defaultClearTimeout||!cachedClearTimeout)&&clearTimeout)return cachedClearTimeout=clearTimeout,clearTimeout(marker);try{cachedClearTimeout(marker)}catch(e){try{return cachedClearTimeout.call(null,marker)}catch(e){return cachedClearTimeout.call(this,marker)}}}(timeout)}}process.nextTick=function(fun){var args=new Array(arguments.length-1);if(arguments.length>1)for(var i=1;i<arguments.length;i++)args[i-1]=arguments[i];queue.push(new Item(fun,args)),1!==queue.length||draining||runTimeout(drainQueue)};function Item(fun,array){this.fun=fun,this.array=array}Item.prototype.run=function(){this.fun.apply(null,this.array)},process.title="browser",process.browser=!0,process.env={},process.argv=[],process.version="",process.versions={};function noop(){}process.on=noop,process.addListener=noop,process.once=noop,process.off=noop,process.removeListener=noop,process.removeAllListeners=noop,process.emit=noop,process.prependListener=noop,process.prependOnceListener=noop,process.listeners=function(name){return[]},process.binding=function(name){throw new Error("process.binding is not supported")},process.cwd=function(){return"/"},process.chdir=function(dir){throw new Error("process.chdir is not supported")},process.umask=function(){return 0}},{}]},{},[1])(1)});