Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions origin-src/components/Menu/MenuLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ export const apiLinks: Pages = [
pathname: "/docs/useformstate/errormessage",
name: "ErrorMessage",
},
{
pathname: "/docs/useformstate/formstatesubscribe",
name: "FormStateSubscribe",
},
],
},
{
Expand Down
108 changes: 54 additions & 54 deletions origin-src/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,60 +43,6 @@ export default function Nav() {

return (
<>
<div className={styles.iconGroup}>
{show && (
<Search focus={isFocusOnSearch} setFocus={setIsFocusOnSearch} />
)}

{showLang && <Toggle />}
</div>

<div className={styles.gitHubButtonWrap}>
<span className={`${styles.icon} desktopOnly`}>
<a
href="https://discord.gg/yYv7GZ8"
target="_blank"
rel="noopener noreferrer"
title="React Hook Form Discord"
>
<svg viewBox="0 0 127.14 96.36">
<path d="M107.7,8.07A105.15,105.15,0,0,0,81.47,0a72.06,72.06,0,0,0-3.36,6.83A97.68,97.68,0,0,0,49,6.83,72.37,72.37,0,0,0,45.64,0,105.89,105.89,0,0,0,19.39,8.09C2.79,32.65-1.71,56.6.54,80.21h0A105.73,105.73,0,0,0,32.71,96.36,77.7,77.7,0,0,0,39.6,85.25a68.42,68.42,0,0,1-10.85-5.18c.91-.66,1.8-1.34,2.66-2a75.57,75.57,0,0,0,64.32,0c.87.71,1.76,1.39,2.66,2a68.68,68.68,0,0,1-10.87,5.19,77,77,0,0,0,6.89,11.1A105.25,105.25,0,0,0,126.6,80.22h0C129.24,52.84,122.09,29.11,107.7,8.07ZM42.45,65.69C36.18,65.69,31,60,31,53s5-12.74,11.43-12.74S54,46,53.89,53,48.84,65.69,42.45,65.69Zm42.24,0C78.41,65.69,73.25,60,73.25,53s5-12.74,11.44-12.74S96.23,46,96.12,53,91.08,65.69,84.69,65.69Z" />
</svg>
</a>
</span>

<span className={`${styles.icon} ${styles.donate}`}>
<a
href="https://opencollective.com/react-hook-form"
target="_blank"
rel="noreferrer noopener"
title="Donate to the project"
>
</a>
</span>

<span className={styles.icon}>
<a
href="https://twitter.com/HookForm"
target="_blank"
rel="noopener noreferrer"
title="React Hook Form Twitter"
>
<svg viewBox="0 0 1200 1227">
<path d="M714.163 519.284L1160.89 0H1055.03L667.137 450.887L357.328 0H0L468.492 681.821L0 1226.37H105.866L515.491 750.218L842.672 1226.37H1200L714.137 519.284H714.163ZM569.165 687.828L521.697 619.934L144.011 79.6944H306.615L611.412 515.685L658.88 583.579L1055.08 1150.3H892.476L569.165 687.854V687.828Z" />
</svg>
</a>
</span>

<GitHubButton
href="https://github.com/react-hook-form/react-hook-form"
data-size="large"
data-show-count
aria-label="Star react-hook-form on GitHub"
/>
</div>

{showMenu && (
<Animate play start={{ opacity: 0 }} end={{ opacity: 1 }}>
<nav
Expand Down Expand Up @@ -452,6 +398,60 @@ export default function Nav() {
</a>
</nav>
</div>

<div className={styles.iconGroup}>
{show && (
<Search focus={isFocusOnSearch} setFocus={setIsFocusOnSearch} />
)}

{showLang && <Toggle />}
</div>

<div className={styles.gitHubButtonWrap}>
<span className={`${styles.icon} desktopOnly`}>
<a
href="https://discord.gg/yYv7GZ8"
target="_blank"
rel="noopener noreferrer"
title="React Hook Form Discord"
>
<svg viewBox="0 0 127.14 96.36">
<path d="M107.7,8.07A105.15,105.15,0,0,0,81.47,0a72.06,72.06,0,0,0-3.36,6.83A97.68,97.68,0,0,0,49,6.83,72.37,72.37,0,0,0,45.64,0,105.89,105.89,0,0,0,19.39,8.09C2.79,32.65-1.71,56.6.54,80.21h0A105.73,105.73,0,0,0,32.71,96.36,77.7,77.7,0,0,0,39.6,85.25a68.42,68.42,0,0,1-10.85-5.18c.91-.66,1.8-1.34,2.66-2a75.57,75.57,0,0,0,64.32,0c.87.71,1.76,1.39,2.66,2a68.68,68.68,0,0,1-10.87,5.19,77,77,0,0,0,6.89,11.1A105.25,105.25,0,0,0,126.6,80.22h0C129.24,52.84,122.09,29.11,107.7,8.07ZM42.45,65.69C36.18,65.69,31,60,31,53s5-12.74,11.43-12.74S54,46,53.89,53,48.84,65.69,42.45,65.69Zm42.24,0C78.41,65.69,73.25,60,73.25,53s5-12.74,11.44-12.74S96.23,46,96.12,53,91.08,65.69,84.69,65.69Z" />
</svg>
</a>
</span>

<span className={`${styles.icon} ${styles.donate}`}>
<a
href="https://opencollective.com/react-hook-form"
target="_blank"
rel="noreferrer noopener"
title="Donate to the project"
>
</a>
</span>

<span className={styles.icon}>
<a
href="https://twitter.com/HookForm"
target="_blank"
rel="noopener noreferrer"
title="React Hook Form Twitter"
>
<svg viewBox="0 0 1200 1227">
<path d="M714.163 519.284L1160.89 0H1055.03L667.137 450.887L357.328 0H0L468.492 681.821L0 1226.37H105.866L515.491 750.218L842.672 1226.37H1200L714.137 519.284H714.163ZM569.165 687.828L521.697 619.934L144.011 79.6944H306.615L611.412 515.685L658.88 583.579L1055.08 1150.3H892.476L569.165 687.854V687.828Z" />
</svg>
</a>
</span>

<GitHubButton
href="https://github.com/react-hook-form/react-hook-form"
data-size="large"
data-show-count
aria-label="Star react-hook-form on GitHub"
/>
</div>
</>
)
}
5 changes: 4 additions & 1 deletion origin-src/components/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ a.active {
bottom: 105px;
font-size: 10px;
text-decoration: none;
line-height: 3;
min-height: 35px;
height: auto;
align-content: center;
line-height: 1.4;
}

.editPage:hover {
Expand Down
1 change: 1 addition & 0 deletions origin-src/content/docs/usecontroller/controller.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The following table contains information about the arguments for `Controller`.
| `shouldUnregister` | <TypeText>boolean = false`</TypeText> | | Input will be unregistered after unmount and defaultValues will be removed as well.<br/><br/>**Note:** this prop should be avoided when using with `useFieldArray` as `unregister` function gets called after input unmount/remount and reorder. |
| `disabled` | <TypeText>boolean = false`</TypeText> | | `disabled` prop will be returned from `field` prop. Controlled input will be disabled and its value will be omitted from the submission data. |
| `defaultValue` | <TypeText>unknown</TypeText> | | **Important:** Can not apply `undefined` to `defaultValue` or `defaultValues` at `useForm`. <ul><li>You need to either set `defaultValue` at the field-level or `useForm`'s `defaultValues`. If you used <code>defaultValues</code> at <code>useForm</code>, skip using this prop.</li><li>If your form will invoke `reset` with default values, you will need to provide `useForm` with `defaultValues`.</li><li>Calling `onChange` with `undefined` is not valid. You should use `null` or the empty string as your default/cleared value instead.</li></ul> |
| `exact` | <TypeText>boolean = false</TypeText> | | This prop will enable an exact match for input name subscriptions, default to true. |

### Return

Expand Down
36 changes: 36 additions & 0 deletions origin-src/content/docs/useform.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ sidebar: apiLinks
| [criteriaMode](#criteriaMode) | Display all validation errors or one at a time. |
| [shouldFocusError](#shouldFocusError) | Enable or disable built-in focus management. |
| [delayError](#delayError) | Delay error from appearing instantly. |
| [validate](#validate) | Form-level validation is limited to built-in validation methods. |
| [shouldUseNativeValidation](#shouldUseNativeValidation) | Use browser built-in form constraint API. |
| [shouldUnregister](#shouldUnregister) | Enable and disable input unregister after unmount. |
| [progressive](/docs/useform/form) | Enable progressive enhancement for native form submission when using the `Form` component. |
| [disabled](#disabled) | Disable the entire form with all associated inputs. |

**Schema validation props:**
Expand Down Expand Up @@ -277,6 +279,40 @@ When set to `true` (default), and the user submits a form that fails validation,
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| This configuration delays the display of error states to the end-user by a specified number of milliseconds. If the user corrects the error input, the error is removed instantly, and the delay is not applied. | <CodeSandbox url="https://codesandbox.io/s/useform-delayerror-q6c2d"/> |

#### validate: <TypeText>Function</TypeText> {#validate}

---

This example demonstrates how to use the **new `validate` API** in combination with `useForm` to perform **form-level validation** in a React application.

The `validate` function receives the entire form object and allows you to return a **structured error** that integrates with `formState.errors`.

**Examples:**

---

```javascript copy
const {
register,
formState: { errors },
} = useForm({
validate: async ({ formValues }: FormValidateResult) => {
if (formValues.test1.length > formValues.test.length) {
return {
type: "formError",
message: "something is wrong here",
}
}

if (formValue.test === "test") {
return "direct error message"
}

return true
},
})
```

#### shouldUnregister: <TypeText>boolean = false</TypeText> {#shouldUnregister}

---
Expand Down
4 changes: 2 additions & 2 deletions origin-src/content/docs/useform/clearerrors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ This function can manually clear errors in the form.

<Admonition type="important" title="Rules">

- This will not affect the validation rules attached to each inputs.
- This method doesn't affect validation rules or `isValid` formState.
- This method does not affect the validation rules attached to inputs and does not
impact <code>formState.isValid</code>.

</Admonition>

Expand Down
9 changes: 0 additions & 9 deletions origin-src/content/docs/useform/handlesubmit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ This function will receive the form data if form validation is successful.

<Admonition type="important" title="Rules">

- You can easily submit form asynchronously with handleSubmit.

```javascript copy
handleSubmit(onSubmit)()

// You can pass an async function for asynchronous validation.
handleSubmit(async (data) => await fetchAPI(data))
```

- `disabled` inputs will appear as `undefined` values in form values. If you want to prevent users from updating an input and wish to retain the form value, you can use `readOnly` or disable the entire &lt;fieldset /&gt;. Here is an [example](https://codesandbox.io/s/react-hook-form-disabled-inputs-oihxx).
- `handleSubmit` function will not swallow errors that occurred inside your onSubmit callback, so we recommend you to try and catch inside async request and handle those errors gracefully for your customers.

Expand Down
31 changes: 14 additions & 17 deletions origin-src/content/docs/useform/register.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -102,32 +102,29 @@ By selecting the register option, the API table below will get updated.

<ul>
<li>
Name is **required** and **unique** (except native radio and checkbox).
Input name supports both dot and bracket syntax, which allows you to easily
create nested form fields.
<strong>Name</strong> is <strong>required</strong> and must be <strong>unique</strong>
(except for native radio and checkbox inputs).
</li>
<li>
Name can neither start with a number nor use number as key name. Please
avoid special characters as well.
</li>
<li>
We are using dot syntax only for typescript usage consistency, so bracket `[]`
will not work for array form value.

Name must not start with a number or use numbers as standalone keys, and should avoid special characters.
For TypeScript consistency, only dot syntax is supported—bracket syntax (<code>[]</code>) will not work for array form values.

```javascript
register('test.0.firstName'); // ✅
register('test[0]firstName'); // ❌
register("test.0.firstName") // ✅
register("test[0].firstName") // ❌
```

</li>

<li>Disabled input will result in an undefined form value. If you want to prevent users from updating the input, you can use `readOnly` or `disable` the entire `fieldset`. Here is an [example](https://codesandbox.io/s/react-hook-form-disabled-inputs-oihxx).</li>

<li>To produce an array of fields, input names should be followed by a dot and number. For example: `test.0.data`</li>
<li> Disabled inputs return <code>undefined</code> as their form value.
If you need to prevent user edits while preserving the value, use
<code>readOnly</code> or disable the entire <code>fieldset</code>. Here is an [example](https://codesandbox.io/s/react-hook-form-disabled-inputs-oihxx).</li>

<li>Changing the name on each render will result in new inputs being registered. It's recommended to keep static names for each registered input.</li>
<li>Changing an input’s <code>name</code> on each render causes it to be re-registered as a new field.
To ensure consistent behavior, keep input names stable across renders.</li>

<li>Input value and reference will no longer gets removed based on unmount. You can invoke unregister to remove that value and reference.</li>
<li>Input values and references are not automatically removed on unmount.
Use <a href="/docs/useform/unregister"><code>unregister</code></a> to explicitly remove them when needed.</li>

<li>
Individual register option can't be removed by `undefined` or `{}`. You can update individual attribute instead.
Expand Down
22 changes: 2 additions & 20 deletions origin-src/content/docs/useform/reset.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,8 @@ Reset the entire form state, fields reference, and subscriptions. There are opti

<Admonition type="important" title="Rules">

- For controlled components you will need to pass `defaultValues` to `useForm` in order to `reset` the `Controller` components' value.
- When `defaultValues` is not supplied to `reset` API, then HTML native [reset](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset) API will be invoked to restore the form.
- Avoid calling `reset` before `useForm`'s `useEffect` is invoked, this is because `useForm`'s subscription needs to be ready before `reset` can send a signal to flush form state update.
- It's recommended to `reset` inside `useEffect` after submission.
```javascript
useEffect(() => {
reset({
data: "test",
})
}, [isSubmitSuccessful])
```
- It's fine to run `reset` without argument as long as you have provided a `defaultValues` at useForm.

```javascript
reset() // update form back to default values

reset({ test: "test" }) // update your defaultValues && form values

reset(undefined, { keepDirtyValues: true }) // reset other form state but keep defaultValues and form values
```
- It is recommended to always provide <code>defaultValues</code> when resetting a form
to ensure all inputs, especially controlled components, are restored correctly.

</Admonition>

Expand Down
10 changes: 4 additions & 6 deletions origin-src/content/docs/useform/seterror.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ The function allows you to manually set one or more errors.
setError("root.serverError", {
type: "400",
})
setError("root.random", {
type: "random",
})
```
- Can be useful in the `handleSubmit` method when you want to give error feedback to a user after async validation. (ex: API returns validation errors)
- `shouldFocus` doesn't work when an input has been disabled.
- This method will force set `isValid` formState to `false`. However, it's important to be aware that `isValid` will always be derived from the validation result of your input registration rules or schema result.
- There are certain keywords that need to be avoided to prevent conflicts with type checking. They are `type` and `types`.
- This method will force <code>formState.isValid</code> to <code>false</code>.
However, <code>isValid</code> is always derived from the validation results of your
registered inputs or schema.
- There are certain keywords that need to be avoided to prevent conflicts with type checking.

</Admonition>

Expand Down
16 changes: 0 additions & 16 deletions origin-src/content/docs/useform/setvalue.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,6 @@ This function allows you to dynamically set the value of a <strong>registered</s
setValue("array.0.test2", "2")
```

- The method will not create a new field when targeting a non-existing field.

```javascript
const { replace } = useFieldArray({ name: "test" })

// ❌ doesn't create new input
setValue("test.101.data")

// ✅ work on refresh entire field array
replace([{ data: "test" }])
```

- Only the following conditions will trigger a re-render:
- When an error is triggered or corrected by a value update.
- When `setValue` cause state update, such as dirty and touched.

- It's recommended to target the field's name rather than make the second argument a nested object.

```javascript
Expand Down
9 changes: 1 addition & 8 deletions origin-src/content/docs/useform/subscribe.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar: apiLinks

## `subscribe:` <TypeText>`UseFormSubscribe<TFieldValues extends FieldValues>`</TypeText>

Subscribe to [`formState`](/docs/useform/formState) changes and value updates. You can subscribe to individual fields or the entire form, while avoiding unnecessary re-renders caused by form changes.
Subscribe to [`formState`](/docs/useform/formstate) changes and value updates. You can subscribe to individual fields or the entire form, while avoiding unnecessary re-renders caused by form changes.

### Props

Expand All @@ -23,13 +23,6 @@ Subscribe to [`formState`](/docs/useform/formState) changes and value updates. Y
<Admonition type="important" title="Notes">

<ul>
<li>
<p>
This function is intended for subscribing to changes only; dispatching
state updates or triggering re-renders is not allowed. eg `setValue` or
`reset`
</p>
</li>
<li>
<p>
This function shares the same functionality as{" "}
Expand Down
6 changes: 5 additions & 1 deletion origin-src/content/docs/useformstate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ sidebar: apiLinks

<SelectNav
options={[
{
label: "FormStateSubscribe",
value: "/docs/useformstate/formstatesubscribe",
},
{
label: "ErrorMessage",
value: "/docs/errormessage",
value: "/docs/useformstate/errormessage",
},
]}
/>
Expand Down
Loading