Skip to content
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

Improve and fix types of value in useFieldArray #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface FieldArrayRenderProps<FieldValue, T extends HTMLElement> {
shift: () => FieldValue
swap: (indexA: number, indexB: number) => void
unshift: (value: FieldValue) => void
value: FieldValue[]
value: void | FieldValue[]
} & FieldState<FieldValue[]>
meta: Partial<{
// TODO: Make a diff of `FieldState` without all the functions
Expand Down
2 changes: 1 addition & 1 deletion src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import type {
declare export var FieldArray: React.ComponentType<FieldArrayProps>
declare export var useFieldArray: (
name: string,
config: UseFieldArrayConfig
config?: UseFieldArrayConfig
) => FieldArrayRenderProps
declare export var version: string
2 changes: 1 addition & 1 deletion src/types.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
40 changes: 40 additions & 0 deletions src/useFieldArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<MyFieldArray />
</form>
)}
</Form>
)
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(
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<MyFieldArray />
</form>
)}
</Form>
)
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].fields.value).toEqual([])
})
})