form-container
Version:
Lightweight React form container with validation (written in TypeScript)
1 lines • 8.61 kB
JavaScript
module.exports=function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=6)}([function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});!function(t){t.Error="error",t.Warning="warning"}(e.ValidationType||(e.ValidationType={}))},function(t,e){t.exports=require("react")},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return function(e){return t.reduce(function(t,e){return e(t)},e)}},e.isEmpty=function(t){return null===t||void 0===t||0===Object.keys(t).length},e.isNil=function(t){return null==t},e.omit=function(t,e){return Object.keys(t).reduce(function(r,n){return n!==e&&(r[n]=t[n]),r},{})}},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(0);e.ValidationRuleFactory=function(t,e,r){return void 0===r&&(r=n.ValidationType.Error),function(n,o,i){return void 0===o&&(o=e),void 0===i&&(i=r),[function(e,r){return t(e[n],r)},(u={},u[n]=o,u),i];var u}},e.hasError=e.ValidationRuleFactory(function(t){return!1},"Error")},function(t,e,r){"use strict";var n={childContextTypes:!0,contextTypes:!0,defaultProps:!0,displayName:!0,getDefaultProps:!0,mixins:!0,propTypes:!0,type:!0},o={name:!0,length:!0,prototype:!0,caller:!0,callee:!0,arguments:!0,arity:!0},i=Object.defineProperty,u=Object.getOwnPropertyNames,a=Object.getOwnPropertySymbols,c=Object.getOwnPropertyDescriptor,s=Object.getPrototypeOf,l=s&&s(Object);t.exports=function t(e,r,f){if("string"!=typeof r){if(l){var p=s(r);p&&p!==l&&t(e,p,f)}var d=u(r);a&&(d=d.concat(a(r)));for(var v=0;v<d.length;++v){var b=d[v];if(!(n[b]||o[b]||f&&f[b])){var y=c(r,b);try{i(e,b,y)}catch(t){}}}return e}return e}},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(e,"__esModule",{value:!0});var o=r(10),i=function(t){function e(e){var r=t.call(this,"Submit validation failed")||this;return r.errors=e,r}return n(e,t),e}(o.default);e.SubmissionError=i,e.default=i},function(t,e,r){t.exports=r(7)},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=r(8);e.connectForm=n.connectForm;var o=r(0);e.ValidationType=o.ValidationType;var i=r(3);e.ValidationRuleFactory=i.ValidationRuleFactory;var u=r(5);e.SubmissionError=u.SubmissionError},function(t,e,r){"use strict";var n=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}(),o=this&&this.__assign||Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++){e=arguments[r];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t};Object.defineProperty(e,"__esModule",{value:!0});var i=r(1),u=r(2),a=r(9),c=r(5),s=r(4),l=function(t){return function(e){var r=function(r){function a(e,n){var i=r.call(this,e,n)||this;return i.setModel=function(t){return i.setState({model:t}),t},i.setProperty=function(t,e){return i.setState(function(r){return o({},r,{model:o({},r.model,(n={},n[t]=e,n))});var n})},i.clearSubmitError=function(t){return i.setState(function(e){return o({},e,e.submitErrors[t]&&{submitErrors:u.omit(e.submitErrors,t)})})},i.setTouched=function(t){return i.setState({touched:t}),t},i.setFieldToTouched=function(t){return i.setTouched(Object.assign({},i.state.touched,(e={},e[t]=!0,e)));var e},i.getValue=function(t){var e=t,r=i.state.model[e];return u.isNil(r)?"":r},i.bindToChangeEvent=function(t){var e=t.target,r=e.name,n=e.type,o=t.target.value;if("checkbox"===n){var u=i.state.model[r]||[],a=t.target.checked?u.concat(o):u.filter(function(t){return t!==o});o=a}i.setProperty(r,o),i.clearSubmitError(r)},i.bindToFocusEvent=function(t){},i.bindToBlurEvent=function(e){var r=e.target;i.setFieldToTouched(r.name),t.onInputBlur&&t.onInputBlur(e)},i.bindInput=function(t){return{name:t,value:i.getValue(t),onChange:i.bindToChangeEvent,onFocus:i.bindToFocusEvent,onBlur:i.bindToBlurEvent}},i.bindNativeInput=function(t){return o({},i.bindInput(t),{ref:function(e){i.state.inputs[t]||i.setState({inputs:o({},i.state.inputs,(r={},r[t]=e,r))});var r}})},i.handleSubmit=function(t){return function(){return t(i.state.model).catch(function(t){var e=t instanceof c.default?t.errors:void 0;if(e)return i.setState({submitErrors:e}),e;throw t})}},i.state={model:t.initialModel||{},touched:{},inputs:{},submitErrors:{}},i}return n(a,r),a.prototype.render=function(){var r=Object.assign({},this.props,{form:{model:this.state.model,inputs:this.state.inputs,touched:this.state.touched,submitErrors:this.state.submitErrors},formMethods:{bindInput:this.bindInput,bindNativeInput:this.bindNativeInput,bindToChangeEvent:this.bindToChangeEvent,clearSubmitError:this.clearSubmitError,setProperty:this.setProperty,setModel:this.setModel,setFieldToTouched:this.setFieldToTouched,handleSubmit:this.handleSubmit}}),n=t.middleware?t.middleware(r):r;return i.createElement(e,n)},a}(i.Component);return s(r,e)}};e.connectForm=function(t,e){return void 0===t&&(t=[]),void 0===e&&(e={}),function(r){return u.pipe(a.validate(t),l(e))(r)}}},function(t,e,r){"use strict";var n=this&&this.__assign||Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++){e=arguments[r];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t};Object.defineProperty(e,"__esModule",{value:!0});var o=r(1),i=r(3),u=r(0),a=r(2),c=r(4),s=function(t){var e=t.allProps,r=t.model,o=t.rules,i=t.validationType;return o.filter(function(t){var e=(t[0],t[1],t[2]);return(void 0===e?u.ValidationType.Error:e)===i}).reduce(function(t,o){var i=o[0],u=o[1];o[2];return i(r,e)?t:n({},t,u)},{})},l=function(t,e){var r=e.inputs,n=t.slice();for(var o in r)if(r.hasOwnProperty(o)){var u=r[o];if(u&&u.validity&&!1===u.validity.valid){var a=i.hasError(u.name,u.validationMessage);n.push(a)}}return n};e.validate=function(t){return void 0===t&&(t=[]),function(e){return c(function(r){var i=l(t,r.form),c=s({allProps:r,rules:i,model:r.form.model,validationType:u.ValidationType.Error}),f=s({allProps:r,rules:t,model:r.form.model,validationType:u.ValidationType.Warning});return o.createElement(e,Object.assign({},r,{form:n({},r.form,{isValid:a.isEmpty(c)&&a.isEmpty(r.form.submitErrors),validationErrors:c,validationWarnings:f})}))},e)}}},function(t,e,r){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var u=function(t){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";n(this,e);var r=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return Object.defineProperty(r,"message",{configurable:!0,enumerable:!1,value:t,writable:!0}),Object.defineProperty(r,"name",{configurable:!0,enumerable:!1,value:r.constructor.name,writable:!0}),Error.hasOwnProperty("captureStackTrace")?(Error.captureStackTrace(r,r.constructor),o(r)):(Object.defineProperty(r,"stack",{configurable:!0,enumerable:!1,value:new Error(t).stack,writable:!0}),r)}return i(e,t),e}(function(t){function e(){t.apply(this,arguments)}return e.prototype=Object.create(t.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t,e}(Error));e.default=u}]);