Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add watch to trigger hook #526

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions src/lib/form/useWatchToTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect } from 'react';

import { FieldPath, FieldValues, UseFormReturn } from 'react-hook-form';

/**
* Use this hook to subscribe to fields and listen for changes to revalidate the
* form.
*
* Using the form "onBlur" validation will validate the field you just updated.
* But imagine a field that has to validate itself based on an another field update.
* That's the point of this hook.
*
* Example: imagine those fields: `min`, `default`, `max`. The `min` should be
* lower than the `default` and the `default` should lower than the `max`.
* `min` is equal to 2, `default` is equal to 4 and `max` is equal to 6.
* You update the `min` so the value is 5, the form (using superRefine and
* custom issues) will tell you that the `min` should be lower than the default.
* You update the `default` so the new value is 5.5. Without this hook, the
* field `min` will not revalidate. With this hook, if you gave the name, it will.
yoannfleurydev marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* field `min` will not revalidate. With this hook, if you gave the name, it will.
* field `min` will not revalidate. With this hook, if you give the field name, it will.

*/
export const useWatchToTrigger = <
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(params: {
form: Pick<UseFormReturn<TFieldValues>, 'watch' | 'trigger'>;
names: Array<TName>;
}) => {
const { watch, trigger } = params.form;
useEffect(() => {
const subscription = watch((_, { name }) => {
if (name && params.names.includes(name as TName)) {
trigger(params.names);
}
});
return () => subscription.unsubscribe();
}, [watch, trigger, params.names]);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My solution would have probably been a useEffect which is triggered everytime a value from a field in params.names is changed ( using watch with an array of names to get the values and having the values as a dependency for the useEffect)

If I understand right, in your case you subscribe to the form in a useEffect, and everytime any value changes in the form, it call the watch callback, and this callback checks if it's one of the field we want to actually watch and calls the trigger function accordingly ?

This is not a change request, just checking if i got it right :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You understand correctly, think of the useEffect as a "onMounted" / "onUnmount" hook here :)

Loading