simple-forms-react
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -222,3 +222,10 @@ 'use strict'; | ||
onBlur = _ref.onBlur, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur']); | ||
validators = _ref.validators, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur', 'validators']); | ||
if (validators) { | ||
_this.validators = _this.validators || {}; | ||
_this.validators[rest.id] = validators; | ||
} | ||
return _extends({ | ||
@@ -253,3 +260,3 @@ onChange: compose(_this.onChange, onChange), | ||
case 0: | ||
validators = this.props.validators[inputName] || []; | ||
validators = this.props.validators[inputName] || (this.validators || {})[inputName] || []; | ||
_context2.next = 3; | ||
@@ -334,3 +341,3 @@ return validators.reduce(function () { | ||
var allInputs, errors, isValid; | ||
var allInputs, allValidators, errors, isValid; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -340,5 +347,5 @@ while (1) { | ||
case 0: | ||
e.preventDefault(); | ||
e && e.preventDefault(); | ||
this.setSubmitting(true); | ||
allInputs = Object.keys(this.props.initialValues); | ||
allInputs = Object.keys(_extends({}, this.props.initialValues, this.state.values, this.props.validators, this.validators || {})); | ||
// touch all fields so that errors are visible | ||
@@ -352,8 +359,9 @@ | ||
// run validations | ||
_context3.next = 6; | ||
return Promise.all(allInputs.map(function (k) { | ||
allValidators = _extends({}, this.props.validators, this.validators); | ||
_context3.next = 7; | ||
return Promise.all(Object.keys(allValidators).map(function (k) { | ||
return _this3.validate(k); | ||
})); | ||
case 6: | ||
case 7: | ||
errors = _context3.sent; | ||
@@ -365,3 +373,3 @@ isValid = !errors.filter(function (err) { | ||
if (!isValid) { | ||
_context3.next = 11; | ||
_context3.next = 12; | ||
break; | ||
@@ -380,7 +388,7 @@ } | ||
case 11: | ||
case 12: | ||
// else don't submit | ||
this.setSubmitting(false); | ||
case 12: | ||
case 13: | ||
case 'end': | ||
@@ -387,0 +395,0 @@ return _context3.stop(); |
@@ -216,3 +216,10 @@ import React from 'react'; | ||
onBlur = _ref.onBlur, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur']); | ||
validators = _ref.validators, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur', 'validators']); | ||
if (validators) { | ||
_this.validators = _this.validators || {}; | ||
_this.validators[rest.id] = validators; | ||
} | ||
return _extends({ | ||
@@ -247,3 +254,3 @@ onChange: compose(_this.onChange, onChange), | ||
case 0: | ||
validators = this.props.validators[inputName] || []; | ||
validators = this.props.validators[inputName] || (this.validators || {})[inputName] || []; | ||
_context2.next = 3; | ||
@@ -328,3 +335,3 @@ return validators.reduce(function () { | ||
var allInputs, errors, isValid; | ||
var allInputs, allValidators, errors, isValid; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -334,5 +341,5 @@ while (1) { | ||
case 0: | ||
e.preventDefault(); | ||
e && e.preventDefault(); | ||
this.setSubmitting(true); | ||
allInputs = Object.keys(this.props.initialValues); | ||
allInputs = Object.keys(_extends({}, this.props.initialValues, this.state.values, this.props.validators, this.validators || {})); | ||
// touch all fields so that errors are visible | ||
@@ -346,8 +353,9 @@ | ||
// run validations | ||
_context3.next = 6; | ||
return Promise.all(allInputs.map(function (k) { | ||
allValidators = _extends({}, this.props.validators, this.validators); | ||
_context3.next = 7; | ||
return Promise.all(Object.keys(allValidators).map(function (k) { | ||
return _this3.validate(k); | ||
})); | ||
case 6: | ||
case 7: | ||
errors = _context3.sent; | ||
@@ -359,3 +367,3 @@ isValid = !errors.filter(function (err) { | ||
if (!isValid) { | ||
_context3.next = 11; | ||
_context3.next = 12; | ||
break; | ||
@@ -374,7 +382,7 @@ } | ||
case 11: | ||
case 12: | ||
// else don't submit | ||
this.setSubmitting(false); | ||
case 12: | ||
case 13: | ||
case 'end': | ||
@@ -381,0 +389,0 @@ return _context3.stop(); |
@@ -222,3 +222,10 @@ (function (global, factory) { | ||
onBlur = _ref.onBlur, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur']); | ||
validators = _ref.validators, | ||
rest = objectWithoutProperties(_ref, ['onChange', 'onBlur', 'validators']); | ||
if (validators) { | ||
_this.validators = _this.validators || {}; | ||
_this.validators[rest.id] = validators; | ||
} | ||
return _extends({ | ||
@@ -253,3 +260,3 @@ onChange: compose(_this.onChange, onChange), | ||
case 0: | ||
validators = this.props.validators[inputName] || []; | ||
validators = this.props.validators[inputName] || (this.validators || {})[inputName] || []; | ||
_context2.next = 3; | ||
@@ -334,3 +341,3 @@ return validators.reduce(function () { | ||
var allInputs, errors, isValid; | ||
var allInputs, allValidators, errors, isValid; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -340,5 +347,5 @@ while (1) { | ||
case 0: | ||
e.preventDefault(); | ||
e && e.preventDefault(); | ||
this.setSubmitting(true); | ||
allInputs = Object.keys(this.props.initialValues); | ||
allInputs = Object.keys(_extends({}, this.props.initialValues, this.state.values, this.props.validators, this.validators || {})); | ||
// touch all fields so that errors are visible | ||
@@ -352,8 +359,9 @@ | ||
// run validations | ||
_context3.next = 6; | ||
return Promise.all(allInputs.map(function (k) { | ||
allValidators = _extends({}, this.props.validators, this.validators); | ||
_context3.next = 7; | ||
return Promise.all(Object.keys(allValidators).map(function (k) { | ||
return _this3.validate(k); | ||
})); | ||
case 6: | ||
case 7: | ||
errors = _context3.sent; | ||
@@ -365,3 +373,3 @@ isValid = !errors.filter(function (err) { | ||
if (!isValid) { | ||
_context3.next = 11; | ||
_context3.next = 12; | ||
break; | ||
@@ -380,7 +388,7 @@ } | ||
case 11: | ||
case 12: | ||
// else don't submit | ||
this.setSubmitting(false); | ||
case 12: | ||
case 13: | ||
case 'end': | ||
@@ -387,0 +395,0 @@ return _context3.stop(); |
36
index.js
@@ -83,10 +83,20 @@ import React from 'react'; | ||
fieldProps = ({ onChange, onBlur, ...rest } = {}) => ({ | ||
onChange: compose(this.onChange, onChange), | ||
onBlur: compose(this.onBlur, onBlur), | ||
...rest, | ||
}); | ||
fieldProps = ({ onChange, onBlur, validators, ...rest } = {}) => { | ||
if (validators) { | ||
this.validators = this.validators || {}; | ||
this.validators[rest.id] = validators; | ||
} | ||
return { | ||
onChange: compose(this.onChange, onChange), | ||
onBlur: compose(this.onBlur, onBlur), | ||
...rest, | ||
}; | ||
}; | ||
async validate(inputName) { | ||
const validators = this.props.validators[inputName] || []; | ||
const validators = | ||
this.props.validators[inputName] || | ||
(this.validators || {})[inputName] || | ||
[]; | ||
const error = await validators.reduce(async (acc, validator) => { | ||
@@ -112,5 +122,10 @@ const lastError = await acc; | ||
async handleSubmit(e) { | ||
e.preventDefault(); | ||
e && e.preventDefault(); | ||
this.setSubmitting(true); | ||
const allInputs = Object.keys(this.props.initialValues); | ||
const allInputs = Object.keys({ | ||
...this.props.initialValues, | ||
...this.state.values, | ||
...this.props.validators, | ||
...(this.validators || {}), | ||
}); | ||
// touch all fields so that errors are visible | ||
@@ -121,3 +136,6 @@ this.setState({ | ||
// run validations | ||
const errors = await Promise.all(allInputs.map(k => this.validate(k))); | ||
const allValidators = { ...this.props.validators, ...this.validators }; | ||
const errors = await Promise.all( | ||
Object.keys(allValidators).map(k => this.validate(k)), | ||
); | ||
const isValid = !errors.filter(err => err && !err.valid).length; | ||
@@ -124,0 +142,0 @@ |
{ | ||
"name": "simple-forms-react", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "An awesome Form for React!", | ||
@@ -5,0 +5,0 @@ "main": "dist/simple-forms-react.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
81625
1230