From 7cbc35470d04b3fe7682128e5542ad1e080ee797 Mon Sep 17 00:00:00 2001 From: bill Date: Sat, 25 Apr 2026 19:38:12 +1000 Subject: [PATCH 01/11] feat: setValues --- src/components/Menu/MenuLinks.ts | 1 + src/content/docs/useform/setvalues.mdx | 130 +++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 src/content/docs/useform/setvalues.mdx diff --git a/src/components/Menu/MenuLinks.ts b/src/components/Menu/MenuLinks.ts index e7391d385..3d725ce6b 100644 --- a/src/components/Menu/MenuLinks.ts +++ b/src/components/Menu/MenuLinks.ts @@ -114,6 +114,7 @@ export const apiLinks: Pages = [ { pathname: "/docs/useform/seterror", name: "setError" }, { pathname: "/docs/useform/clearerrors", name: "clearErrors" }, { pathname: "/docs/useform/setvalue", name: "setValue" }, + { pathname: "/docs/useform/setvalues", name: "setValues" }, { pathname: "/docs/useform/setfocus", name: "setFocus" }, { pathname: "/docs/useform/getvalues", name: "getValues" }, { pathname: "/docs/useform/getfieldstate", name: "getFieldState" }, diff --git a/src/content/docs/useform/setvalues.mdx b/src/content/docs/useform/setvalues.mdx new file mode 100644 index 000000000..729528287 --- /dev/null +++ b/src/content/docs/useform/setvalues.mdx @@ -0,0 +1,130 @@ +--- +title: setValues +description: Update multiple field values +sidebar: apiLinks +--- + +## \ `setValues:` (value: Partial\ | ((data: TFieldValues) => Partial\), options?: SetValueConfig) => void + +This function allows you to dynamically set multiple field values at once and have the options to validate and update the form state. It also accepts a callback function that receives the current form values, making it easy to derive the next state from the existing one. + +### Props + +--- + +| Name | | Description | +| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `value`
Partial\ \| (data: TFieldValues) => Partial\ | | An object containing the field values to update, or a callback function that receives the current form values and returns the updated values. This argument is required. | +| `options` | `shouldValidate`
boolean | | +| | `shouldDirty`
boolean | | +| | `shouldTouch`
boolean | Whether to set the updated inputs to be touched. | + + + +- When using the callback form, the function receives a snapshot of the current form values and should return only the fields you want to update. + +- This method does not reset the form. Use [`reset`](/docs/useform/reset) if you need to reinitialize `defaultValues`. + + + +### Examples + +--- + +**Basic** + +```javascript +import { useForm } from "react-hook-form" + +const App = () => { + const { register, setValues } = useForm({ + defaultValues: { + firstName: "", + lastName: "", + }, + }) + + return ( +
+ + + +
+ ) +} +``` + +**Callback (derive next state from current values)** + +```javascript +import { useForm } from "react-hook-form" + +const App = () => { + const { register, setValues } = useForm({ + defaultValues: { + name: "", + count: 0, + }, + }) + + return ( +
+ + + +
+ ) +} +``` + +**With options** + +```javascript +import { useForm } from "react-hook-form" + +const App = () => { + const { register, setValues } = useForm({ + defaultValues: { + firstName: "", + lastName: "", + }, + }) + + return ( +
+ + + +
+ ) +} +``` From 77e017d076df0ba879e035cbc13cd915776a316c Mon Sep 17 00:00:00 2001 From: bill Date: Sat, 25 Apr 2026 19:42:41 +1000 Subject: [PATCH 02/11] fxi format --- src/content/docs/useform/setvalues.mdx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/content/docs/useform/setvalues.mdx b/src/content/docs/useform/setvalues.mdx index 729528287..c791173d5 100644 --- a/src/content/docs/useform/setvalues.mdx +++ b/src/content/docs/useform/setvalues.mdx @@ -12,12 +12,12 @@ This function allows you to dynamically set multiple field values at once and ha --- -| Name | | Description | -| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `value`
Partial\ \| (data: TFieldValues) => Partial\ | | An object containing the field values to update, or a callback function that receives the current form values and returns the updated values. This argument is required. | -| `options` | `shouldValidate`
boolean | | -| | `shouldDirty`
boolean | | -| | `shouldTouch`
boolean | Whether to set the updated inputs to be touched. | +| Name | | Description | +| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `value`
Partial\ \| (data: TFieldValues) => Partial\ | | An object containing the field values to update, or a callback function that receives the current form values and returns the updated values. This argument is required. | +| `options` | `shouldValidate`
boolean | | +| | `shouldDirty`
boolean | | +| | `shouldTouch`
boolean | Whether to set the updated inputs to be touched. | @@ -50,9 +50,7 @@ const App = () => { From 34f1d186019363f42385b6619af3bad006428c5b Mon Sep 17 00:00:00 2001 From: bill Date: Sat, 25 Apr 2026 19:51:28 +1000 Subject: [PATCH 03/11] update to use field array mdx --- src/components/UseFieldArray.tsx | 31 -- src/components/UseFieldArrayContent.tsx | 297 -------------- src/content/docs/usefieldarray.mdx | 522 ++++++++++++++++++++++++ src/pages/docs/usefieldarray.tsx | 12 - 4 files changed, 522 insertions(+), 340 deletions(-) delete mode 100644 src/components/UseFieldArray.tsx delete mode 100644 src/components/UseFieldArrayContent.tsx create mode 100644 src/content/docs/usefieldarray.mdx delete mode 100644 src/pages/docs/usefieldarray.tsx diff --git a/src/components/UseFieldArray.tsx b/src/components/UseFieldArray.tsx deleted file mode 100644 index 23bc17140..000000000 --- a/src/components/UseFieldArray.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import typographyStyles from "../styles/typography.module.css" -import Footer from "./Footer" -import containerStyles from "../styles/container.module.css" -import UseFieldArrayContent from "./UseFieldArrayContent" -import StarRepo from "./StarRepo" -import { Menu, apiLinks } from "./Menu" - -export default function UseFieldArray() { - return ( -
-

- useFieldArray -

-

React hooks for Field Array

- -
- - -
-
- - - -
- -
-
-
-
- ) -} diff --git a/src/components/UseFieldArrayContent.tsx b/src/components/UseFieldArrayContent.tsx deleted file mode 100644 index 3271b3a5c..000000000 --- a/src/components/UseFieldArrayContent.tsx +++ /dev/null @@ -1,297 +0,0 @@ -import generic from "../data/generic" -import api from "../data/api" -import CodeArea from "./CodeArea" -import useFieldArray from "./codeExamples/useFieldArray" -import typographyStyles from "../styles/typography.module.css" -import tableStyles from "../styles/table.module.css" -import TabGroup from "./TabGroup" -import useFieldArrayConditional from "./codeExamples/useFieldArrayConditional" -import useFieldArrayTS from "./codeExamples/useFieldArrayTS" -import useFieldArrayFocus from "./codeExamples/useFieldArrayFocus" -import Link from "next/link" -import useFieldArrayPreview from "./codeExamples/useFieldArrayPreview" - -export default function UseFieldArrayContent() { - return ( - <> - -

- useFieldArray:{" "} - - - UseFieldArrayProps - - -

-
- - {api.useFieldArray.description} - -

Return

- -
- - - - - - - - {api.useFieldArray.table} - -
{generic.name}{generic.type}{generic.description}
-
- -

- Rules -

- -
    -
  • -

    - useFieldArray automatically generates a unique - identifier named id which is used for key{" "} - prop. For more information why this is required:{" "} - - https://react.dev/learn/rendering-lists - -

    -

    - The field.id (and not index) must be added - as the component key to prevent re-renders breaking the fields: - )} - -// ❌ incorrect: -{fields.map((field, index) => )} -`} - /> -

    -
  • -
  • -

    It's recommended to not stack actions one after another.

    - { - append({ test: 'test' }); - remove(0); -}} - -// ✅ Better solution: the remove action is happened after the second render -React.useEffect(() => { - remove(0); -}, [remove]) - -onClick={() => { - append({ test: 'test' }); -}} - `} - /> -
  • -
  • -

    - Each useFieldArray is unique and has its own state - update, which means you should not have multiple useFieldArray with - the same name. -

    -
  • -
  • -

    - Each input name needs to be unique, if you need to build checkbox or - radio with the same name then use it with useController{" "} - or Controller. -

    -
  • -
  • -

    Does not support flat field array.

    -
  • -
  • -

    - When you append, prepend, insert and update the field array, the obj - can't be empty object {} rather need to supply all your - input's defaultValues. -

    - -
  • -
- -

TypeScript

- -
    -
  • -

    - when register input name, you will have to cast them as{" "} - const -

    - `} - /> -
  • -
  • -

    - we do not support circular reference. Refer to this{" "} - - Github issue - {" "} - for more detail. -

    -
  • -
  • -

    - for nested field array, you will have to cast the field array by its - name. -

    - -
  • -
- -

- Examples -

- - - - - - - - -

Video

-

- The following video explains the basic usage of{" "} - useFieldArray. -

- -