Skip to content

Commit

Permalink
Add defaultValue and initialValue (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectPixel authored and erikras committed Aug 15, 2019
1 parent 638d825 commit 16f19c5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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

0 comments on commit 16f19c5

Please sign in to comment.