Skip to content

Commit

Permalink
Allowed swappable final-form APIs (final-form#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikras authored Jun 11, 2019
1 parent 9fe62c5 commit c3912b0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ReactFinalForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
debug,
decorators,
destroyOnUnregister,
form: alternateFormApi,
initialValues,
initialValuesEqual,
keepDirtyOnReinitialize,
Expand All @@ -65,7 +66,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
}

const form: FormApi<FormValues> = useConstant(() => {
const f = createForm<FormValues>(config)
const f = alternateFormApi || createForm<FormValues>(config)
f.pauseValidation()
return f
})
Expand Down
24 changes: 24 additions & 0 deletions src/ReactFinalForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render, fireEvent, cleanup } from 'react-testing-library'
import 'jest-dom/extend-expect'
import deepEqual from 'fast-deep-equal'
import { ErrorBoundary, Toggle, wrapWith } from './testUtils'
import { createForm } from 'final-form'
import Form from './ReactFinalForm'
import Field from './Field'

Expand Down Expand Up @@ -907,4 +908,27 @@ describe('ReactFinalForm', () => {
expect(recordSubmitting).toHaveBeenCalledTimes(3)
expect(recordSubmitting.mock.calls[2][0]).toBe(false)
})

it('should allow an alternative form api to be passed in', () => {
const onSubmit = jest.fn()
const form = createForm({ onSubmit: onSubmit })
const formMock = jest.spyOn(form, 'registerField')
render(
<Form form={form}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Field name="name" component="input" />
</form>
)}
</Form>
)
expect(formMock).toHaveBeenCalled()

// called once on first render to get initial state, and then again to subscribe
expect(formMock).toHaveBeenCalledTimes(2)
expect(formMock.mock.calls[0][0]).toBe('name')
expect(formMock.mock.calls[0][2].active).toBe(true) // default subscription
expect(formMock.mock.calls[1][0]).toBe('name')
expect(formMock.mock.calls[1][2].active).toBe(true) // default subscription
})
})
1 change: 1 addition & 0 deletions src/types.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type RenderableProps<T> = {
export type FormProps<FormValues: FormValuesShape> = {
subscription?: FormSubscription,
decorators?: Decorator<FormValues>[],
form?: FormApi<FormValues>,
initialValuesEqual?: (?Object, ?Object) => boolean
} & Config<FormValues> &
RenderableProps<FormRenderProps<FormValues>>
Expand Down
1 change: 1 addition & 0 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface FormProps<FormValues = object>
RenderableProps<FormRenderProps<FormValues>> {
subscription?: FormSubscription;
decorators?: Decorator[];
form?: FormApi<FormValues>;
initialValuesEqual?: (a?: object, b?: object) => boolean;
}

Expand Down

0 comments on commit c3912b0

Please sign in to comment.