Skip to content

Commit

Permalink
Merge pull request #52 from selbekk/fix/context
Browse files Browse the repository at this point in the history
fix(context): make context consistent
  • Loading branch information
ArrayKnight authored May 2, 2019
2 parents 3bb43e5 + 48daeec commit 3273b09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
65 changes: 21 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,24 @@ When you have a simple form to validate.
Accepts all of the props from `Form` and `Validation` below.
#### Context
The `FormContext` object represents the core internal state of the form and is passed through event callbacks and into template functions.
```js
{
dirty: object, // Object with all fields isDirty state, keyed per field
errors: object, // Object with all error messages, keyed per field
fields: object, // Object with all field inputs, keyed per field
isValid: bool, // Boolean indicating whether your form is valid or not
resetAll: func, // call this to programmatically trigger a full state reset
setError: func, // callback accepting a diff object, updating errors like setState
setField: func, // callback accepting a diff object, updating fields like setState
submit: func, // call this to programmatically trigger a submitted state
submitted: bool, // flag showing whether the form has been submitted once or not
}
```
### `Form`
`import { Form } from 'calidators';`
Expand Down Expand Up @@ -430,43 +448,15 @@ This callback is fired whenever the form is submitted. That can happen whenever
somebody clicks the submit button, hits `enter` in the form, or calls the `submit` function passed in the `children`
function.
The `onSubmit` function is called with an object with the following props:
```js
{
dirty: object, // Object with all fields isDirty state, keyed per field
errors: object, // Object with all error messages, keyed per field
fields: object, // Object with all field inputs, keyed per field
isValid: bool, // Boolean indicating whether your form is valid or not
resetAll: func, // call this to programmatically trigger a full state reset
setError: func, // callback accepting a diff object, updating errors like setState
setField: func, // callback accepting a diff object, updating fields like setState
submit: func, // call this to programmatically trigger a submitted state
submitted: bool, // flag showing whether the form has been submitted once or not
}
```
The `onSubmit` function is called with the `FormContext` object as its only prop.
##### `onUpdate: func`
This callback is fired whenever the form state changes. This can happen when `Validation` components
are `register`ed or `unregister`ed, when a field is updated by the user or when the `setField` method is called,
when the `setError` method is called, or when the form is reset or the `resetAll` method is called.
The `onUpdate` function is called with an object with the follow props:
```js
{
dirty: object, // Object with all fields isDirty state, keyed per field
errors: object, // Object with all error messages, keyed per field
fields: object, // Object with all field inputs, keyed per field
isValid: bool, // Boolean indicating whether your form is valid or not
resetAll: func, // call this to programmatically trigger a full state reset
setError: func, // callback accepting a diff object, updating errors like setState
setField: func, // callback accepting a diff object, updating fields like setState
submit: func, // call this to programmatically trigger a submitted state
submitted: bool, // flag showing whether the form has been submitted once or not
}
```
The `onUpdate` function is called with the `FormContext` object as its only prop.
### `Validation`
Expand All @@ -479,20 +469,7 @@ with the `Form` tag)
##### `children: func.isRequired`
The `children` function is called with an object with the following props:
```js
{
dirty: object, // Object with all fields isDirty state, keyed per field
errors: object, // object with the same keys as `fields`, but with error messages
fields: object, // object with the form field values, to make controlled components
resetAll: func, // call this to programmatically trigger a full state reset
setError: func, // callback accepting a diff object, updating errors like setState
setField: func, // callback accepting a diff object, updating fields like setState
submit: func, // call this to programmatically trigger a submitted state
submitted: bool, // flag showing whether the form has been submitted once or not
}
```
The `children` function is called with the `FormContext` object as its only prop.
##### `config: object.isRequired`
Expand Down
11 changes: 7 additions & 4 deletions src/Validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ class Validation extends Component {
// OwnProps
...propTypes,
// FormContext
dirty: shape({}),
errors: shape({}),
fields: shape({}),
dirty: shape({}).isRequired,
errors: shape({}).isRequired,
fields: shape({}).isRequired,
isValid: bool.isRequired,
register: func.isRequired,
resetAll: func.isRequired,
setError: func.isRequired,
setField: func.isRequired,
submit: func.isRequired,
submitted: bool,
submitted: bool.isRequired,
unregister: func.isRequired,
};

Expand Down Expand Up @@ -73,6 +74,7 @@ class Validation extends Component {
dirty,
errors,
fields,
isValid,
resetAll,
setError,
setField,
Expand All @@ -83,6 +85,7 @@ class Validation extends Component {
dirty,
errors,
fields,
isValid,
resetAll,
setError,
setField,
Expand Down

0 comments on commit 3273b09

Please sign in to comment.