-
Notifications
You must be signed in to change notification settings - Fork 25
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
Wrap fields with context to expose a useField
hook
#133
Conversation
✅ Deploy Preview for remix-forms ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Not sure why checks are failing - the build succeeds when I run it locally. If there's anything I can do about it, please let me know (I can't see the Netlify logs). |
Thank you for the PR @bvangraafeiland , will check the logs ASAP |
@bvangraafeiland thanks again for the PR, I want to investigate an alternative implementation, thanks for the patience. |
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.
Please give me a day or two to play around with the implementation and then I'll add a proper review.
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.
Only a few things before we can merge this one:
- An example in
apps/web/app/routes/examples/forms
to show the use case. - An E2E test in
apps/web/tests/examples/forms
using the example and ensuring we can indeed render custom components that use this hook. - A rebase against the latest main branch.
@@ -279,6 +291,21 @@ function createField<Schema extends SomeZodObject>({ | |||
ref, | |||
) => { | |||
const value = fieldType === 'date' ? parseDate(rawValue) : rawValue | |||
const field = { |
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.
it would be great to reuse this constant to pass the childrenFn
parameters (line 320) for the sake of readability, it makes it clearer that we are talking about the same field
object.
Partial<Omit<Field<never>, 'name'>> | undefined | ||
>(undefined) | ||
|
||
export function useField() { |
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.
what do yo think of the name userFieldContext
? It seems to make it clearer that the hook is returning a context and creates a neat parallel with the useFormContext
provided by react-hook-form.
@danielweinmann thoughts?
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.
I don't like useFormContext and I think the only reason they named it like that is because useForm was already taken 😂
d51ce97
to
e9fc8c5
Compare
I've applied the feedback:
However, the test is failing. This happens because the Before I spend too much time figuring this out, I thought maybe one of you immediately knows why this is? If not, maybe we can update the test for now (adding a default value for first name), and then add a new issue along with a PR providing the failing test case. update: Actually it seems the |
@bvangraafeiland it's looking good. Good catch with the dirty missing in the What do you think of simplifying your example and test cases? We just need to show |
e9fc8c5
to
bb425c1
Compare
I've simplified the example and test, the test now also passes. The branch is no longer rebased to main but I believe you can do that when merging as well? |
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.
LGTM
bb425c1
to
61d4c27
Compare
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.
Great contribution @bvangraafeiland !
This hook can be used in any of the custom components, allowing those to change styling/behavior etc depending on the field's state. This is mostly useful for things like the required/error/dirty indicators (e.g. https://remix-forms.seasoned.cc/examples/render-field/required-indicator) which currently require the use of the
renderField
prop. See also #124.Using the prop makes things quite verbose because the structure of each field will have to be replicated, even if just a single component needs to be customized. Since the default structure is not the same for all field types, customizing components in this way requires some gnarly copy and paste from the package's source code (see https://remix-forms.seasoned.cc/examples/render-field/inline-checkboxes).
For now, the PR only contains the code required to make
useField
work. If the maintainers agree with this approach, I'll add some examples to the docs as well (perhaps even replacing the currentrenderField
section?), and tests where applicable.