From 509ca71fb17113619bba2f16d5f850712829fb79 Mon Sep 17 00:00:00 2001 From: Thomas Haefele Date: Fri, 12 Jul 2019 20:17:26 +0200 Subject: [PATCH] Add defaultValue and initialValue --- README.md | 12 ++++++++++++ src/FieldArray.js | 4 ++++ src/types.js.flow | 2 ++ src/useFieldArray.js | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 6b1d2e3..550174d 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,8 @@ const MyForm = () => ( - [`component?: React.ComponentType`](#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, allNewValues: Array) => boolean`](#isequal-allpreviousvalues-arrayany-allnewvalues-arrayany--boolean) - [`subscription?: FieldSubscription`](#subscription-fieldsubscription) - [`validate?: (value: ?any[], allValues: Object) => ?any`](#validate-value-any-allvalues-object--any) @@ -196,6 +198,16 @@ A render function that is given [`FieldArrayRenderProps`](#fieldarrayrenderprops), as well as any non-API props passed into the `` 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, allNewValues: Array) => 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. diff --git a/src/FieldArray.js b/src/FieldArray.js index a8d8b5a..e952beb 100644 --- a/src/FieldArray.js +++ b/src/FieldArray.js @@ -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 }) diff --git a/src/types.js.flow b/src/types.js.flow index 1c5da1a..05b6517 100644 --- a/src/types.js.flow +++ b/src/types.js.flow @@ -48,6 +48,8 @@ export type RenderableProps = $Shape<{ export type UseFieldArrayConfig = { subscription?: FieldSubscription, + defaultValue?: any, + initialValue?: any, isEqual?: (any[], any[]) => boolean, validate?: (value: ?(any[]), allValues: Object, meta: ?FieldState) => ?any } diff --git a/src/useFieldArray.js b/src/useFieldArray.js index 3a33b02..6a03a9d 100644 --- a/src/useFieldArray.js +++ b/src/useFieldArray.js @@ -16,6 +16,8 @@ const useFieldArray = ( name: string, { subscription = all, + defaultValue, + initialValue, isEqual = defaultIsEqual, validate: validateProp }: UseFieldArrayConfig = {} @@ -57,6 +59,8 @@ const useFieldArray = ( ...fieldState } = useField(name, { subscription: { ...subscription, value: true, length: true }, + defaultValue, + initialValue, isEqual, validate, format: v => v