-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,6 +339,68 @@ submitForm({ | |
}); | ||
``` | ||
## Usage - Custom form initial values | ||
By default, formup will already create the form initial values according to your yup schema. This means that any `default(...)` will be taken into consideration, and it will always initialize the form as an empty valid object of your schema. | ||
For example, the schema: | ||
```js | ||
const schema = yup.object().shape({ | ||
userId: yup | ||
.number() | ||
.default(100), | ||
personalInformation: yup.object().shape({ | ||
firstName: yup | ||
.string() | ||
.required(), | ||
email: yup | ||
.string() | ||
.default("[email protected]") | ||
.required(), | ||
}), | ||
}); | ||
``` | ||
Will automatically translate to: | ||
```js | ||
{ | ||
userId: 100, | ||
personalInformation: { | ||
firstName: "", | ||
email: "[email protected]", | ||
}, | ||
} | ||
``` | ||
However, you can customize any value generated within your initial values by passing `initialValues` to `useFormup` options. Formup will automatically merge the two objects into one, taking your overrides into consideration. | ||
Here's an example, using the schema declared above: | ||
|
||
```js | ||
useFormup(schema, { | ||
initialValues: { | ||
userId: 999, | ||
personalInformation: { | ||
firstName: "Foo", | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
This will produce: | ||
|
||
```js | ||
{ | ||
userId: 999, | ||
personalInformation: { | ||
firstName: "Foo", | ||
email: "[email protected]", | ||
}, | ||
} | ||
``` | ||
|
||
## Contributing | ||
|
||
Pull requests are welcome! | ||
|