Skip to content

Commit b396820

Browse files
committed
Added examples and external submit faq
1 parent bf00586 commit b396820

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ const MyForm = () => (
121121
* [Field Arrays](#field-arrays)
122122
* [Calculated Fields](#calculated-fields)
123123
* [Field Warnings](#field-warnings)
124+
* [Reusable Field Groups](#reusable-field-groups)
125+
* [External Submit](#external-submit)
124126
* [Rendering](#rendering)
125127
* [API](#api)
126128
* [`Field : React.ComponentType<FieldProps>`](#field--reactcomponenttypefieldprops)
@@ -265,6 +267,16 @@ Demonstrates how the power of subscriptions and mutators can be used to build a
265267
warning engine: logic to display a message next to each field that is _not_ an
266268
error that prevents form submission.
267269

270+
### [Reusable Field Groups](https://codesandbox.io/s/8z5jm6x80)
271+
272+
Demonstrates how fields can be grouped into reusable components.
273+
274+
### [External Submit](https://codesandbox.io/s/1y7noyrlmq)
275+
276+
Demonstrates how you can use `document.getElementById()` or a closure to trigger
277+
a submit from outside of the form. For more information, see
278+
[How can I trigger a submit from outside the form?](docs/faq.md#how-can-i-trigger-a-submit-from-outside-my-form)
279+
268280
## Rendering
269281

270282
There are three ways to tell `<Form/>` and `<Field/>` what to render:

docs/faq.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
* [Why not Redux-Form or Formik?](#why-not-redux-form-or-formik)
1010
* [Why no HOC?](#why-no-hoc)
11+
* [How can I trigger a submit from outside my form?](#how-can-i-trigger-a-submit-from-outside-my-form)
12+
* [Via `document.getElementById()`](#via-documentgetelementbyid)
13+
* [Via Closure](#via-closure)
14+
* [Via Redux Dead Drop](#via-redux-dead-drop)
1115

1216
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1317

@@ -21,8 +25,8 @@ depends on your requirements and what trade-offs you wish to make. Here is a
2125

2226
The only benefit that higher order components provide over render props is
2327
access to the injected props from within component lifecycle methods. Plus, it
24-
only takes a single line of code to transform a component with a `render`
25-
(or `component`) prop into a HOC. If you really want a HOC, you can write your own:
28+
only takes a single line of code to transform a component with a `render` (or
29+
`component`) prop into a HOC. If you really want a HOC, you can write your own:
2630

2731
```jsx
2832
import { Form, Field } from 'react-final-form'
@@ -46,3 +50,60 @@ Doing a HOC
4650
[properly](https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/withRouter.js),
4751
as a library should, with hoisted statics and `displayName` and `ref`, etc., is
4852
a hassle and would add unnecessary bulk.
53+
54+
## How can I trigger a submit from outside my form?
55+
56+
This is a common question I see from people migrating from `redux-form`. There
57+
are three possible solutions:
58+
59+
### Via `document.getElementById()`
60+
61+
You can use the DOM to get a reference to your `<form>` element and dispatch a
62+
submit event on it. Note that you cannot just call `submit()`, as this will not
63+
trigger React's event handlers.
64+
65+
```jsx
66+
<button onClick={() => {
67+
document.getElementById('myForm').submit() //
68+
}}>Submit</button>
69+
70+
<button onClick={() => {
71+
document.getElementById('myForm').dispatchEvent(new Event('submit')) //
72+
}}>Submit</button>
73+
74+
<form id="myForm" onSubmit={handleSubmit}>
75+
...fields go here...
76+
</form>
77+
```
78+
79+
See [Sandbox Example](https://codesandbox.io/s/1y7noyrlmq).
80+
81+
### Via Closure
82+
83+
If you define a variable outside of your form, you can then set the value of
84+
that variable to the `handleSubmit` function that 🏁 React Final Form gives you,
85+
and then you can call that function from outside of the form.
86+
87+
```jsx
88+
let submit
89+
return (
90+
<div>
91+
<button onClick={submit}>Submit</button> // ❌ Not overwritten closure value
92+
<button onClick={event => submit(event)}>Submit</button> //
93+
<Form onSubmit={onSubmit} render={({ handleSubmit })=> {
94+
submit = handleSubmit
95+
return <form>
96+
...fields go here...
97+
</form>
98+
}}>
99+
</div>
100+
)
101+
```
102+
103+
See [Sandbox Example](https://codesandbox.io/s/1y7noyrlmq).
104+
105+
### Via Redux Dead Drop
106+
107+
If you're already using Redux, you could potentially use the same mechanism that
108+
`redux-form` uses:
109+
[Redux Dead Drop](https://medium.com/@erikras/redux-dead-drop-1b9573705bec).

0 commit comments

Comments
 (0)