|
1 | | - |
2 | 1 | # Formbold-react — The easiest way to configure and use Form in React/Next.js |
3 | 2 | React package for [FormBold](https://formbold.com/) which simplifies the integration of FormBold with React/Next.js projects. |
4 | 3 |
|
5 | 4 | FormBold is a multipurpose form API and serverless backend solution compatible with all hosting, SSG, and frameworks. It allows you to receive form submissions directly in your email, slack, telegram, notion, and more. It's ready for use with any Static, Jamstack, and SSG sites, such as HTML, React, Next.js, Gatsby, Vue, Nuxt, Hugo, and Jekyll. |
6 | 5 |
|
7 | 6 | It offers a wide range of form fields, advanced features like conditional logic, and seamless integration with other tools. |
8 | 7 |
|
| 8 | +It helps you wire a form to FormBold with a small API, built-in validation for required fields, custom error messages, and basic submission state. |
| 9 | + |
9 | 10 | ## Installation |
10 | 11 |
|
11 | 12 | ```bash |
12 | 13 | npm install formbold-react |
13 | 14 | ``` |
14 | 15 |
|
15 | | -or |
16 | | - |
17 | 16 | ```bash |
18 | 17 | yarn add formbold-react |
19 | 18 | ``` |
20 | 19 |
|
21 | | -## Usages |
22 | | - |
23 | | -To use it you have to import it in your Form component. Then call it with the form_id. |
| 20 | +## Usage |
24 | 21 |
|
25 | | -And at the end, attach the handleSubmit function to the onSubmit event. |
| 22 | +Import `useForm`, pass your FormBold `formId`, and attach the returned `handleSubmit` function to your form. |
26 | 23 |
|
27 | | -```typescript |
| 24 | +```tsx |
28 | 25 | import { useForm } from "formbold-react"; |
29 | 26 |
|
30 | | -function Form() { |
| 27 | +function ContactForm() { |
31 | 28 | const [state, handleSubmit] = useForm("form_id"); |
32 | 29 |
|
33 | 30 | if (state.succeeded) { |
34 | | - return <div>Form submitted successfully</div>; |
| 31 | + return <div>Form submitted successfully.</div>; |
35 | 32 | } |
36 | 33 |
|
37 | 34 | return ( |
38 | | - <> |
39 | | - <h1>Home Page</h1> |
40 | | - <form onSubmit={handleSubmit}> |
41 | | - <label htmlFor="email">Email Address</label> |
42 | | - <input id="email" type="email" name="email" required /> |
43 | | - <textarea id="message" name="message" required /> |
44 | | - <button type="submit">{state.submitting ? "Submitting..." : "Submit"}</button> |
45 | | - |
46 | | - <div> |
47 | | - {state.error && state.error.message} |
48 | | - </div> |
49 | | - </form> |
50 | | - </> |
| 35 | + <form onSubmit={handleSubmit}> |
| 36 | + <label htmlFor="email">Email Address</label> |
| 37 | + <input id="email" type="email" name="email" required /> |
| 38 | + |
| 39 | + <label htmlFor="message">Message</label> |
| 40 | + <textarea id="message" name="message" required /> |
| 41 | + |
| 42 | + <button type="submit"> |
| 43 | + {state.submitting ? "Submitting..." : "Submit"} |
| 44 | + </button> |
| 45 | + |
| 46 | + {state.error.status && <p>{state.error.message}</p>} |
| 47 | + </form> |
51 | 48 | ); |
52 | 49 | } |
53 | 50 |
|
54 | | -export default Form; |
| 51 | +export default ContactForm; |
55 | 52 | ``` |
56 | 53 |
|
57 | | -## Required fields |
| 54 | +## API |
58 | 55 |
|
59 | | -To make certain fields mandatory in your form, you can use the `requiredFields` option when using the `useForm` hook. In the example below, the `email` field is set as a required field: |
| 56 | +### `useForm(formId, config?)` |
60 | 57 |
|
61 | | -```typescript |
62 | | -const [state, handleSubmit] = useForm("form_id", { requiredFields: ["email"] }); |
| 58 | +```ts |
| 59 | +const [state, handleSubmit] = useForm("form_id", config); |
63 | 60 | ``` |
64 | | -This ensures that the form cannot be submitted unless the `email` field is filled out by the user. |
65 | 61 |
|
| 62 | +Returns: |
| 63 | + |
| 64 | +```ts |
| 65 | +[ |
| 66 | + { |
| 67 | + error: { message: string; status: boolean }; |
| 68 | + succeeded: boolean; |
| 69 | + submitting: boolean; |
| 70 | + }, |
| 71 | + (event, recaptchaRef?) => void |
| 72 | +] |
| 73 | +``` |
| 74 | + |
| 75 | +### `config` |
66 | 76 |
|
67 | | -## Custom error messages |
68 | | -You can customize the error messages displayed when certain fields are not filled out in your form. By using the `errorMessages` option in the `useForm` hook, you can provide custom error messages for different scenarios. |
| 77 | +```ts |
| 78 | +type Config = { |
| 79 | + requiredFields?: string[]; |
| 80 | + errorMessages?: { |
| 81 | + empty?: string; |
| 82 | + required?: (fields: string[]) => string; |
| 83 | + }; |
| 84 | +}; |
| 85 | +``` |
69 | 86 |
|
70 | | -Here's an example of how you can set custom error messages for the `name` and `email` fields: |
| 87 | +Example: |
71 | 88 |
|
72 | | -```typescript |
| 89 | +```ts |
73 | 90 | const [state, handleSubmit] = useForm("form_id", { |
74 | 91 | requiredFields: ["name", "email"], |
75 | 92 | errorMessages: { |
76 | 93 | empty: "Please fill the form!", |
77 | 94 | required: fields => `Please fill the required fields: ${fields.join(", ")}`, |
78 | | - } |
| 95 | + }, |
| 96 | +}); |
| 97 | +``` |
| 98 | + |
| 99 | +### Required fields |
| 100 | + |
| 101 | +Use `requiredFields` when you want the hook to block submission until specific inputs are filled. |
| 102 | + |
| 103 | +```ts |
| 104 | +const [state, handleSubmit] = useForm("form_id", { |
| 105 | + requiredFields: ["email"], |
79 | 106 | }); |
80 | 107 | ``` |
81 | 108 |
|
82 | | -Feel free to customize the error messages according to your specific requirements. |
| 109 | +### reCAPTCHA support |
| 110 | + |
| 111 | +If you use reCAPTCHA, pass the ref as the second argument to the submit handler. |
| 112 | + |
| 113 | +```tsx |
| 114 | +<form onSubmit={e => handleSubmit(e, recaptchaRef)}> |
| 115 | +``` |
83 | 116 |
|
| 117 | +The hook will include `g-recaptcha-response` in the submitted payload when a ref is provided. |
| 118 | + |
| 119 | +## Behavior notes |
| 120 | + |
| 121 | +- Empty forms are rejected before the request is sent. |
| 122 | +- Required field validation runs before submission. |
| 123 | +- Non-2xx responses are treated as submission failures. |
| 124 | +- Repeated form fields are preserved as arrays in the request payload. |
| 125 | + |
| 126 | +## Types |
| 127 | + |
| 128 | +The package exports its public types from the root entry point: |
| 129 | + |
| 130 | +```ts |
| 131 | +import type { Config, ErrorState, CaptchaRef } from "formbold-react"; |
| 132 | +``` |
84 | 133 |
|
85 | | -## ****Useful Links and Information**** |
| 134 | +## Links |
86 | 135 |
|
87 | | -For more information visit the [documentation](https://formbold.com/docs). |
| 136 | +- [FormBold docs](https://formbold.com/docs) |
| 137 | +- [FormBold homepage](https://formbold.com/) |
0 commit comments