diff --git a/README.md b/README.md index d755ef3..2388d27 100644 --- a/README.md +++ b/README.md @@ -296,9 +296,9 @@ Updates a value of the specified index of the array field. A function to add a value to the beginning of the array. -#### `fields.value: any[]` +#### `fields.value: void | any[]` -The value of the array. Should be treated as readonly. +The value of the field. Should be treated as readonly. #### `meta.active?: boolean` diff --git a/src/index.d.ts b/src/index.d.ts index 0d41635..b4ac0ce 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -16,7 +16,7 @@ export interface FieldArrayRenderProps { shift: () => FieldValue swap: (indexA: number, indexB: number) => void unshift: (value: FieldValue) => void - value: FieldValue[] + value: void | FieldValue[] } & FieldState meta: Partial<{ // TODO: Make a diff of `FieldState` without all the functions diff --git a/src/index.js.flow b/src/index.js.flow index c25a289..ee8c004 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -9,6 +9,6 @@ import type { declare export var FieldArray: React.ComponentType declare export var useFieldArray: ( name: string, - config: UseFieldArrayConfig + config?: UseFieldArrayConfig ) => FieldArrayRenderProps declare export var version: string diff --git a/src/types.js.flow b/src/types.js.flow index 05b6517..683ba8d 100644 --- a/src/types.js.flow +++ b/src/types.js.flow @@ -35,7 +35,7 @@ export type FieldArrayRenderProps = { shift: () => any, swap: (indexA: number, indexB: number) => void, unshift: (value: any) => void, - value: any[] + value: void | any[] }, meta: Meta } diff --git a/src/useFieldArray.test.js b/src/useFieldArray.test.js index c391646..ef7d18a 100644 --- a/src/useFieldArray.test.js +++ b/src/useFieldArray.test.js @@ -58,4 +58,44 @@ describe('FieldArray', () => { expect(spy).toHaveBeenCalledTimes(2) expect(spy.mock.calls[1][0].fields.length).toBe(1) }) + + it('should have undefined as value when not initialized', () => { + const spy = jest.fn() + const MyFieldArray = () => { + spy(useFieldArray('names')) + return null + } + render( +
+ {() => ( + + + + )} + + ) + expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledTimes(1) + expect(spy.mock.calls[0][0].fields.value).toEqual(undefined) + }) + + it('should have empty array as value when initialized', () => { + const spy = jest.fn() + const MyFieldArray = () => { + spy(useFieldArray('names', { initialValue: [] })) + return null + } + render( +
+ {() => ( + + + + )} + + ) + expect(spy).toHaveBeenCalled() + expect(spy).toHaveBeenCalledTimes(1) + expect(spy.mock.calls[0][0].fields.value).toEqual([]) + }) })