-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dot keys messing up react-hook-form by reintroducing dot.ts
- Loading branch information
Showing
3 changed files
with
81 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/components/BankDetails/RecipientDetails/RecipientDetailsForm/dot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const DOT = "."; | ||
const DOLLAR_SIGN = "$"; | ||
|
||
/** | ||
* `react-hook-form` turns dot-field names into nested object fields, causing weird behavior when | ||
* accessing/assigning said fields if they're deeply nested. | ||
* | ||
* E.g. with an object like: | ||
* ``` | ||
* type FormValues = { | ||
* requirements: { | ||
* type: { | ||
* [key: string]: any | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* setting a field named `address.country` inside `requirements.type` can internally be converted into: | ||
* ``` | ||
* const formValues: FormValues = { | ||
* requirements: { | ||
* type: | ||
* address: { | ||
* country: "some_value" | ||
* } | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* This is the default behavior, see: | ||
* - https://github.com/react-hook-form/react-hook-form/issues/3351#issuecomment-721996499 | ||
* - https://react-hook-form.com/docs/useform/register | ||
* | ||
* The problem is that it is then difficult to access this `address.country` field, as `react-hook-form` | ||
* gets confused and has no idea how to access the field: | ||
* ``` | ||
* console.log(methods.getValues("requirements.type.address.country")) // returns undefined | ||
* ``` | ||
* | ||
* To solve, turn dots into some other character when creating the form, see: | ||
* https://github.com/react-hook-form/react-hook-form/issues/3755#issuecomment-943408807 | ||
* | ||
* @param key string | ||
* @returns same string with dot characters (.) replaced with dollar signs ($) | ||
* */ | ||
export const undot = (key: string): string => key.replace(DOT, DOLLAR_SIGN); | ||
|
||
/** | ||
* @see {@link undot} | ||
* @param key string | ||
* @returns same string with dollar signs ($) replaced with dot characters (.) | ||
*/ | ||
export const redot = (key: string): string => key.replace(DOLLAR_SIGN, DOT); |