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

Conversation

yoannfleurydev
Copy link
Member

Describe your changes

add a hook to watch fields and trigger validation

Checklist

  • I performed a self review of my code
  • I ensured that everything is written in English
  • I tested the feature or fix on my local environment
  • I ran the pnpm storybook command and everything is working

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
start-ui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 12, 2024 0:46am

* 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.
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.

Comment on lines 28 to 37
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 :)

Copy link

sonarcloud bot commented Sep 12, 2024

@DecampsRenan DecampsRenan merged commit 76deb85 into master Sep 12, 2024
7 checks passed
@DecampsRenan DecampsRenan deleted the feat/useWatchAndTrigger branch September 12, 2024 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants