Skip to content

Add defaultValue and initialValue #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const MyForm = () => (
- [`component?: React.ComponentType<FieldArrayRenderProps>`](#component-reactcomponenttypefieldarrayrenderprops)
- [`name: string`](#name-string)
- [`render?: (props: FieldArrayRenderProps) => React.Node`](#render-props-fieldarrayrenderprops--reactnode)
- [`defaultValue?: any`](#defaultvalue-any)
- [`initialValue?: any`](#initialvalue-any)
- [`isEqual?: (allPreviousValues: Array<any>, allNewValues: Array<any>) => boolean`](#isequal-allpreviousvalues-arrayany-allnewvalues-arrayany--boolean)
- [`subscription?: FieldSubscription`](#subscription-fieldsubscription)
- [`validate?: (value: ?any[], allValues: Object) => ?any`](#validate-value-any-allvalues-object--any)
Expand Down Expand Up @@ -200,6 +202,16 @@ A render function that is given
[`FieldArrayRenderProps`](#fieldarrayrenderprops), as well as any non-API props
passed into the `<FieldArray/>` component.

#### `defaultValue?: any`

⚠️ You probably want `initialValue`! ⚠️

_**Before using this prop, read and understand the 🏁 Final Form documentation on [`initialValue`](https://github.com/final-form/final-form#initialvalue-any) and [`defaultValue`](https://github.com/final-form/final-form#defaultvalue-any)!**_

#### `initialValue?: any`

[See the 🏁 Final Form docs on `initialValue`](https://github.com/final-form/final-form#initialvalue-any)

#### `isEqual?: (allPreviousValues: Array<any>, allNewValues: Array<any>) => boolean`

A function that can be used to compare two arrays of values (before and after every change) and calculate pristine/dirty checks. Defaults to a function that will `===` check each element of the array.
Expand Down
4 changes: 4 additions & 0 deletions src/FieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ const versions = {
const FieldArray = ({
name,
subscription,
defaultValue,
initialValue,
isEqual,
validate,
...rest
}: FieldArrayProps) => {
const { fields, meta } = useFieldArray(name, {
subscription,
defaultValue,
initialValue,
isEqual,
validate
})
Expand Down
2 changes: 2 additions & 0 deletions src/types.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export type RenderableProps<T> = $Shape<{

export type UseFieldArrayConfig = {
subscription?: FieldSubscription,
defaultValue?: any,
initialValue?: any,
isEqual?: (any[], any[]) => boolean,
validate?: (value: ?(any[]), allValues: Object, meta: ?FieldState) => ?any
}
Expand Down
4 changes: 4 additions & 0 deletions src/useFieldArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const useFieldArray = (
name: string,
{
subscription = all,
defaultValue,
initialValue,
isEqual = defaultIsEqual,
validate: validateProp
}: UseFieldArrayConfig = {}
Expand Down Expand Up @@ -58,6 +60,8 @@ const useFieldArray = (
...fieldState
} = useField(name, {
subscription: { ...subscription, length: true },
defaultValue,
initialValue,
isEqual,
validate,
format: v => v
Expand Down