|
1 | | -# Fetch Forms for react |
2 | | -### Fetch Forms - build forms everywhere |
| 1 | +# Fetch Forms for React |
3 | 2 |
|
4 | | -Docs go here |
| 3 | +### What is Fetch Forms? |
| 4 | +Fetch Forms is a headless forms solution and form builder designed to help developers build forms and connect data. |
| 5 | + |
| 6 | +### Prerequisites |
| 7 | +1. [Configure a form](https://www.fetchforms.io/docs/configuring-a-form) |
| 8 | +2. [Create an API token](https://www.fetchforms.io/account/api-details) |
| 9 | + |
| 10 | +## Documentation |
| 11 | +- [Examples](https://github.com/fetchforms/react-client/tree/main/src/examples) |
| 12 | +- [The Fetch Form object](https://www.fetchforms.io/docs/fetch-form-object) |
| 13 | +- [Storing forms in source code](https://www.fetchforms.io/docs/build-a-form) |
| 14 | + |
| 15 | +### Install the client |
| 16 | +```sh |
| 17 | +npm install @fetchforms/react |
| 18 | +``` |
| 19 | +```sh |
| 20 | +yarn add @fetchforms/react |
| 21 | +``` |
| 22 | + |
| 23 | +### Quick start |
| 24 | +Using the `<FetchForm />` component is the fastest way to see Fetch Forms in action. This component will handle client-side validation, data parsing, and saving to the cloud if your form has `cloudSave` set. |
| 25 | + |
| 26 | +```jsx |
| 27 | +import { FetchForm, FetchFormsProvider } from "@fetchforms/react"; |
| 28 | + |
| 29 | +const MyFetchForm = () => { |
| 30 | + const onSubmit = async (values) => { |
| 31 | + console.log('values', values); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <FetchFormsProvider permission="API_TOKEN"> |
| 36 | + <FetchForm |
| 37 | + slug="FORM_SLUG" |
| 38 | + onSubmit={onSubmit} |
| 39 | + /> |
| 40 | + </FetchFormsProvider> |
| 41 | + ); |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +### Using hooks |
| 46 | +The `useFetchForms` hook allows you to display your forms in more complex layouts. See the [hook form example](https://github.com/fetchforms/react-client/tree/main/src/examples/hookform) to see an implementation using Ant.Design form element. |
| 47 | + |
| 48 | +```jsx |
| 49 | +import { useFetchForms, FetchFormsProvider } from "@fetchforms/react"; |
| 50 | + |
| 51 | +const CustomFormLayout = () => { |
| 52 | + const [fetchForm, loading, error] = useFetchForms('FORM_SLUG'); |
| 53 | + |
| 54 | + const onFinish = (values) => { |
| 55 | + console.log('values', values); |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + {error && <div>Error: {error}</div>} |
| 61 | + {loading ? ( |
| 62 | + <div>Loading...</div> |
| 63 | + ) : ( |
| 64 | + fetchForm && ( |
| 65 | + <form |
| 66 | + name='HookForm' |
| 67 | + onSubmit={onFinish} |
| 68 | + > |
| 69 | + {fetchForm.formItems.map((item, i) => { |
| 70 | + if(item.fieldType === 'select') { |
| 71 | + return ( |
| 72 | + <div key={item.fieldHtml.id}> |
| 73 | + <label for={item.fieldHtml.name}>{item.label}</label> |
| 74 | + <select {...item.fieldHtml}> |
| 75 | + {item.options.map((opt) => ( |
| 76 | + <option value={opt.value} key={opt.value}>{opt.label}</option> |
| 77 | + ))} |
| 78 | + </select> |
| 79 | + </div> |
| 80 | + ); |
| 81 | + } else if (item.fieldType === 'checkbox') { |
| 82 | + return ( |
| 83 | + <div key={item.fieldHtml.id}> |
| 84 | + <input {...item.fieldHtml} /> |
| 85 | + <label for={item.fieldHtml.name}>{item.label}</label> |
| 86 | + </div> |
| 87 | + ); |
| 88 | + {/* ...other field types */} |
| 89 | + } else { |
| 90 | + return ( |
| 91 | + <div key={item.fieldHtml.id}> |
| 92 | + <input {...item.fieldHtml} /> |
| 93 | + <label for={item.fieldHtml.name}>{item.label}</label> |
| 94 | + </div> |
| 95 | + ); |
| 96 | + } |
| 97 | + })} |
| 98 | + <button type='submit'> |
| 99 | + {fetchForm.submitText} |
| 100 | + </button> |
| 101 | + </form> |
| 102 | + ) |
| 103 | + )} |
| 104 | + </> |
| 105 | + ) |
| 106 | +} |
| 107 | + |
| 108 | +const App = () => { |
| 109 | + return ( |
| 110 | + <FetchFormsProvider permission="API_TOKEN"> |
| 111 | + <CustomFormLayout /> |
| 112 | + </FetchFormsProvider> |
| 113 | + ); |
| 114 | +} |
| 115 | +``` |
0 commit comments