Skip to content

Commit c3912b0

Browse files
authored
Allowed swappable final-form APIs (#520)
1 parent 9fe62c5 commit c3912b0

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/ReactFinalForm.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
4242
debug,
4343
decorators,
4444
destroyOnUnregister,
45+
form: alternateFormApi,
4546
initialValues,
4647
initialValuesEqual,
4748
keepDirtyOnReinitialize,
@@ -65,7 +66,7 @@ function ReactFinalForm<FormValues: FormValuesShape>({
6566
}
6667

6768
const form: FormApi<FormValues> = useConstant(() => {
68-
const f = createForm<FormValues>(config)
69+
const f = alternateFormApi || createForm<FormValues>(config)
6970
f.pauseValidation()
7071
return f
7172
})

src/ReactFinalForm.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { render, fireEvent, cleanup } from 'react-testing-library'
33
import 'jest-dom/extend-expect'
44
import deepEqual from 'fast-deep-equal'
55
import { ErrorBoundary, Toggle, wrapWith } from './testUtils'
6+
import { createForm } from 'final-form'
67
import Form from './ReactFinalForm'
78
import Field from './Field'
89

@@ -907,4 +908,27 @@ describe('ReactFinalForm', () => {
907908
expect(recordSubmitting).toHaveBeenCalledTimes(3)
908909
expect(recordSubmitting.mock.calls[2][0]).toBe(false)
909910
})
911+
912+
it('should allow an alternative form api to be passed in', () => {
913+
const onSubmit = jest.fn()
914+
const form = createForm({ onSubmit: onSubmit })
915+
const formMock = jest.spyOn(form, 'registerField')
916+
render(
917+
<Form form={form}>
918+
{({ handleSubmit }) => (
919+
<form onSubmit={handleSubmit}>
920+
<Field name="name" component="input" />
921+
</form>
922+
)}
923+
</Form>
924+
)
925+
expect(formMock).toHaveBeenCalled()
926+
927+
// called once on first render to get initial state, and then again to subscribe
928+
expect(formMock).toHaveBeenCalledTimes(2)
929+
expect(formMock.mock.calls[0][0]).toBe('name')
930+
expect(formMock.mock.calls[0][2].active).toBe(true) // default subscription
931+
expect(formMock.mock.calls[1][0]).toBe('name')
932+
expect(formMock.mock.calls[1][2].active).toBe(true) // default subscription
933+
})
910934
})

src/types.js.flow

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export type RenderableProps<T> = {
6868
export type FormProps<FormValues: FormValuesShape> = {
6969
subscription?: FormSubscription,
7070
decorators?: Decorator<FormValues>[],
71+
form?: FormApi<FormValues>,
7172
initialValuesEqual?: (?Object, ?Object) => boolean
7273
} & Config<FormValues> &
7374
RenderableProps<FormRenderProps<FormValues>>

typescript/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface FormProps<FormValues = object>
6060
RenderableProps<FormRenderProps<FormValues>> {
6161
subscription?: FormSubscription;
6262
decorators?: Decorator[];
63+
form?: FormApi<FormValues>;
6364
initialValuesEqual?: (a?: object, b?: object) => boolean;
6465
}
6566

0 commit comments

Comments
 (0)