Skip to content

Commit

Permalink
🔨 driver / form / API
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Feb 21, 2019
1 parent 2c014c6 commit 44d09cc
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 76 deletions.
10 changes: 7 additions & 3 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

### Breaking changes
- `@k-ramel/driver-http` is not imported by default
* In order to keep `k-ramel` as light as possible we remove the default usage of this driver
* When we added it as a default driver, we thought that almost everybody would use it but since `graphql` is becoming strong, we believe it's time to not make it a default one
* `k-ramel` will be even lighter by default!
* In order to keep `k-ramel` as light as possible we remove the default usage of this driver
* When we added it as a default driver, we thought that almost everybody would use it but since `graphql` is becoming strong, we believe it's time to not make it a default one
* `k-ramel` will be even lighter by default!
- `@k-ramel/driver-form` API has changed
* `set` now remove older fields
* `clearErrors` is renamed to `resetErrors`
* `remove` is renamed to `reset`

### May be breaking changes
- `devtools` options (from `createStore`) is now `undefined` by default
Expand Down
34 changes: 34 additions & 0 deletions packages/drivers/form/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,40 @@ const mapStore = inject((store, { formName, field }, drivers) => {
export default mapStore(Component)
```

## API
### Local to a form
These functions are availables on form that is returned by this call: `drivers.form(formName)` where `formName` is your form name.

| function | description |
| --- | --- |
| `set(<object>)` | set values to the `formName` form, and remove the old ones |
| `update(fieldName)(value)` | update the value of the given fieldName to the `formName` form |
| `addOrUpdate(<object>)` | add or update given values to the `formName` form |
| `setErrors(<object>)` | set errors to the `formName` form, and remove the old ones |
| `addOrUpdateErrors(<object>)` | add or update errors to the `formName` form |
| `resetErrors()` | remove all errors to the `formName` form |
| `reset()` | remove all errors and values to the `formName` form |
| --- | --- |
| `exists()` | test that the `formName` form exists |
| `get()` | retrieve the key/value object from the `formName` form, all fields are retrieved |
| `get(<string>)` | retrieve the value of the given field name from the `formName` form |
| `getErrors()` | retrieve the key/value error object from the `formName` form |
| `getErrors(<string>)` | retrieve error of the given field name from the `formName` form |

### Batched to all given forms at once

These functions are used right into the `drivers.form` field without giving a name to it.

| function | description |
| --- | --- |
| `set([{ name: <string>, values: <object> }, ...])` | set `values` to the form identified by `name`, older values are removed |
| `addOrUpdate([{ name: <string>, values: <object> }, ...])` | add or update `values` to the form identified by `name` |
| `resetErrors([<string>, ...])` | reset errors for all the given form names |
| `reset([<string>, ...])` | reset errors and values for all the given form names |
| --- | --- |
| `find(<regexp>)` | find all form names that matches the given `regexp` |


## Helpers
- `drivers.form.getUpdatedValues(action)` will return an _object_ of all updated pair (field name -> field value) for the given action.
- `drivers.form.getUpdatedEntries(action)` will return an _array_ of all updated pair (field name -> field value) for the given action.
Expand Down
94 changes: 67 additions & 27 deletions packages/drivers/form/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`drivers/form [custom path and key] should add or update fields (errors) in a given form 1`] = `
Object {
"errors": Object {
"before": "field-error-updated",
"new": "field-error",
"should": "still be there (error)",
},
}
`;

exports[`drivers/form [custom path and key] should add or update fields (values) in a given form 1`] = `
Object {
"values": Object {
"before": "field-value-updated",
"new": "field-value",
"should": "still be there",
},
}
`;

exports[`drivers/form [custom path and key] should check if form exists 1`] = `
Object {
"exists": true,
"notExists": false,
}
`;

exports[`drivers/form [custom path and key] should check if form exists 2`] = `
exports[`drivers/form [custom path and key] should check if forms exists 1`] = `
Object {
"formNames": Array [
"form-1",
Expand Down Expand Up @@ -95,7 +115,7 @@ Object {
}
`;

exports[`drivers/form [custom path and key] should remove form 1`] = `
exports[`drivers/form [custom path and key] should reset form 1`] = `
Object {
"formValues": Object {
"ui": Object {
Expand Down Expand Up @@ -156,7 +176,7 @@ Object {
}
`;

exports[`drivers/form [custom path and key] should remove form with an array of formName 1`] = `
exports[`drivers/form [custom path and key] should reset form with an array of formName 1`] = `
Object {
"formValues": Object {
"ui": Object {
Expand Down Expand Up @@ -299,7 +319,7 @@ Object {
"form": Object {
"errors": Object {
"data": Array [],
"initialized": false,
"initialized": true,
},
"values": Object {
"data": Array [],
Expand Down Expand Up @@ -478,14 +498,34 @@ Object {
}
`;

exports[`drivers/form [default path and key] should add or update fields (errors) in a given form 1`] = `
Object {
"errors": Object {
"before": "field-error-updated",
"new": "field-error",
"should": "still be there (error)",
},
}
`;

exports[`drivers/form [default path and key] should add or update fields (values) in a given form 1`] = `
Object {
"values": Object {
"before": "field-value-updated",
"new": "field-value",
"should": "still be there",
},
}
`;

exports[`drivers/form [default path and key] should check if form exists 1`] = `
Object {
"exists": true,
"notExists": false,
}
`;

exports[`drivers/form [default path and key] should check if form exists 2`] = `
exports[`drivers/form [default path and key] should check if forms exists 1`] = `
Object {
"formNames": Array [
"form-1",
Expand Down Expand Up @@ -573,7 +613,7 @@ Object {
}
`;

exports[`drivers/form [default path and key] should remove form 1`] = `
exports[`drivers/form [default path and key] should reset form 1`] = `
Object {
"formValues": Object {
"form": Object {
Expand Down Expand Up @@ -634,7 +674,7 @@ Object {
}
`;

exports[`drivers/form [default path and key] should remove form with an array of formName 1`] = `
exports[`drivers/form [default path and key] should reset form with an array of formName 1`] = `
Object {
"formValues": Object {
"form": Object {
Expand Down Expand Up @@ -775,7 +815,7 @@ Object {
"form": Object {
"errors": Object {
"data": Array [],
"initialized": false,
"initialized": true,
},
"values": Object {
"data": Array [],
Expand Down Expand Up @@ -956,49 +996,49 @@ Object {
}
`;

exports[`drivers/form bulk mode should clearErrors errors 1`] = `
exports[`drivers/form bulk mode should reset forms 1`] = `
Object {
"state": Object {
"errors": Object {},
"values": Object {
"form-1": Object {
"errors": Object {
"form-3": Object {
"@@form-fields": Array [
"lastname",
],
"@@form-name": "form-1",
"lastname": "crespel",
"@@form-name": "form-3",
"lastname": "required",
},
"form-2": Object {
},
"values": Object {
"form-3": Object {
"@@form-fields": Array [
"firstname",
],
"@@form-name": "form-2",
"firstname": "fabien",
"@@form-name": "form-3",
"firstname": "delphine",
},
},
},
}
`;

exports[`drivers/form bulk mode should remove forms 1`] = `
exports[`drivers/form bulk mode should resetErrors errors 1`] = `
Object {
"state": Object {
"errors": Object {
"form-3": Object {
"errors": Object {},
"values": Object {
"form-1": Object {
"@@form-fields": Array [
"lastname",
],
"@@form-name": "form-3",
"lastname": "required",
"@@form-name": "form-1",
"lastname": "crespel",
},
},
"values": Object {
"form-3": Object {
"form-2": Object {
"@@form-fields": Array [
"firstname",
],
"@@form-name": "form-3",
"firstname": "delphine",
"@@form-name": "form-2",
"firstname": "fabien",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/drivers/form/dist/index.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 44d09cc

Please sign in to comment.