-
Notifications
You must be signed in to change notification settings - Fork 130
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
bd18b96
to
fb0e2a2
Compare
src/lib/form/useWatchToTrigger.ts
Outdated
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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. |
src/lib/form/useWatchToTrigger.ts
Outdated
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]); | ||
}; |
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 :)
fb0e2a2
to
075a1db
Compare
075a1db
to
1ba8374
Compare
Quality Gate passedIssues Measures |
Describe your changes
add a hook to watch fields and trigger validation
Checklist
pnpm storybook
command and everything is working