Skip to content

Fix mutators definition #172

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 1 commit into from
Oct 19, 2022
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-flow-strip-types": "^7.12.1",
"@babel/plugin-transform-react-jsx-source": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-flow": "^7.12.1",
Expand Down Expand Up @@ -86,8 +87,8 @@
"peerDependencies": {
"final-form": "^4.15.0",
"final-form-arrays": ">=1.0.4",
"react-final-form": "^6.2.1",
"react": "^16.8.0 || ^17.0.0"
"react": "^16.8.0 || ^17.0.0",
"react-final-form": "^6.2.1"
},
"jest": {
"watchPlugins": [
Expand Down
60 changes: 56 additions & 4 deletions src/FieldArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ErrorBoundary, Toggle, wrapWith } from './testUtils'
import { Form, Field } from 'react-final-form'
import { FieldArray, version } from '.'

const onSubmitMock = values => {}
const onSubmitMock = values => { }
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
async function sleep(ms) {
await act(async () => {
Expand All @@ -22,7 +22,7 @@ describe('FieldArray', () => {
})

it('should warn if not used inside a form', () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
jest.spyOn(console, 'error').mockImplementation(() => { })
const errorSpy = jest.fn()
render(
<ErrorBoundary spy={errorSpy}>
Expand All @@ -38,7 +38,7 @@ describe('FieldArray', () => {
})

it('should warn if no render strategy is provided', () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
jest.spyOn(console, 'error').mockImplementation(() => { })
const errorSpy = jest.fn()
render(
<ErrorBoundary spy={errorSpy}>
Expand All @@ -58,7 +58,7 @@ describe('FieldArray', () => {
})

it('should warn if no array mutators provided', () => {
jest.spyOn(console, 'error').mockImplementation(() => {})
jest.spyOn(console, 'error').mockImplementation(() => { })
const errorSpy = jest.fn()
render(
<ErrorBoundary spy={errorSpy}>
Expand Down Expand Up @@ -451,6 +451,58 @@ describe('FieldArray', () => {
expect(queryByTestId('names[1]')).not.toBe(null)
})

it('should push a new value to right place after changing name', () => {
const { getByText, queryByTestId } = render(
<Toggle>
{isCats => (
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<FieldArray name={isCats ? 'cats' : 'dogs'}>
{({ fields }) => (
<div>
{fields.map(field => (
<Field
name={field}
key={field}
component="input"
data-testid={field}
/>
))}
<button type="button" onClick={() => fields.push({})}>
Add
</button>
</div>
)}
</FieldArray>
</form>
)}
</Form>
)}
</Toggle>
)
expect(queryByTestId('dogs[0]')).toBe(null)
expect(queryByTestId('dogs[1]')).toBe(null)

// push
fireEvent.click(getByText('Add'))

expect(queryByTestId('dogs[0]')).not.toBe(null)
expect(queryByTestId('dogs[1]')).toBe(null)

// change name
fireEvent.click(getByText('Toggle'))

expect(queryByTestId('cats[0]')).toBe(null)
expect(queryByTestId('cats[1]')).toBe(null)

// push
fireEvent.click(getByText('Add'))

expect(queryByTestId('cats[0]')).not.toBe(null)
expect(queryByTestId('cats[1]')).toBe(null)
})

it('should not re-render Field when subscription is empty object', () => {
const nameFieldRender = jest.fn()
const surnameFieldRender = jest.fn()
Expand Down
7 changes: 4 additions & 3 deletions src/useFieldArray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
import { useMemo } from 'react';
import { useForm, useField } from 'react-final-form'
import { fieldSubscriptionItems, ARRAY_ERROR } from 'final-form'
import type { Mutators } from 'final-form-arrays'
Expand Down Expand Up @@ -31,13 +32,13 @@ const useFieldArray = (
'Array mutators not found. You need to provide the mutators from final-form-arrays to your form'
)
}
const mutators = useConstant<Mutators>(() =>
const mutators = useMemo<Mutators>(() =>
// curry the field name onto all mutator calls
Object.keys(formMutators).reduce((result, key) => {
result[key] = (...args) => formMutators[key](name, ...args)
return result
}, {})
)
}, {}
), [name, formMutators])

const validate: FieldValidator = useConstant(
() => (value, allValues, meta) => {
Expand Down