form-hooks
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -37,3 +37,7 @@ "use strict"; | ||
onSubmit = _ref.onSubmit, | ||
validate = _ref.validate; | ||
validate = _ref.validate, | ||
_ref$validateOnBlur = _ref.validateOnBlur, | ||
validateOnBlur = _ref$validateOnBlur === void 0 ? true : _ref$validateOnBlur, | ||
_ref$validateOnChange = _ref.validateOnChange, | ||
validateOnChange = _ref$validateOnChange === void 0 ? true : _ref$validateOnChange; | ||
@@ -74,2 +78,15 @@ var _useState = (0, _react.useState)({}), | ||
function handleValidate() { | ||
return Promise.resolve(validate(values)).then(function (errors) { | ||
return setErrors(errors); | ||
}); | ||
} | ||
function shouldValidate(touchedFields) { | ||
var initialFields = Object.keys(initialValues); | ||
return initialFields.every(function (f) { | ||
return touchedFields.indexOf(f) > -1; | ||
}); | ||
} | ||
function handleBlur(event) { | ||
@@ -82,5 +99,9 @@ var name = event.target.name; | ||
setTouched(_objectSpread({}, touched, _defineProperty({}, name, true)), function () { | ||
validateField(value(event.target)); | ||
}); | ||
setTouched(_objectSpread({}, touched, _defineProperty({}, name, true))); | ||
if (validateOnBlur) { | ||
if (shouldValidate(_toConsumableArray(Object.keys(touched)).concat([name]))) { | ||
handleValidate(); | ||
} | ||
} | ||
} | ||
@@ -96,2 +117,6 @@ | ||
setValues(_objectSpread({}, values, _defineProperty({}, name, value(event.target)))); | ||
if (validateOnChange && shouldValidate(Object.keys(touched))) { | ||
handleValidate(); | ||
} | ||
} | ||
@@ -98,0 +123,0 @@ |
{ | ||
"name": "form-hooks", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Easy forms in React with hooks", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -6,3 +6,3 @@ # form-hooks | ||
some missing cases here (for instance resetting the form on value changes, | ||
per field validation and validation on blur) 🤷♂️. | ||
per field validation and React native) 🤷♂️. | ||
@@ -77,3 +77,3 @@ ## Getting Started | ||
#### `validate(values)` | ||
#### `validate(values, errors)` | ||
@@ -83,4 +83,17 @@ Called when a form is submitted prior to the `onSubmit` call. Returns an object | ||
### `useForm` - returned | ||
#### `validateOnBlur` - *true* | ||
Indicates if `useForm` should re-validate the input on blur. | ||
Only fired when all fields have been touched that were in the `initialValues` | ||
object. | ||
#### `validateOnChange` - *true* | ||
Indicates if `useForm` should re-validate the input on change. | ||
Only fired when all fields have been touched that were in the `initialValues` | ||
object. | ||
### `useForm` - `returned` | ||
#### `errors` | ||
@@ -101,11 +114,11 @@ | ||
#### `handleBlur` | ||
#### `handleBlur()` | ||
Marks a field as `touched` to show errors after all fields are touched. | ||
#### `handleChange` | ||
#### `handleChange()` | ||
Changes the fields value in the `values` state. | ||
#### `handleSubmit` | ||
#### `handleSubmit(values)` | ||
@@ -119,3 +132,3 @@ Handles calling validation prior to the `onSubmit` handler and setting the | ||
#### `setErrors` | ||
#### `setErrors()` | ||
@@ -122,0 +135,0 @@ Function that allows for errors to be set outside of the `useForm` |
10465
121
133