Comments (11)
It took me some time, but I finally understood this issue 😅
If we always render a FormProvider inside our forms, it will enable the users to useFormContext anywhere inside any form. The solution is simple and looks harmless. I'll reopen the issue until we implement it.
from remix-forms.
Ran into this issue as well. In my case, I want to have more control over the rendered submit button based on the form state - while submitting or if the form is invalid, the button should be disabled, and the button text while submitting should be a spinner. I know this is possible to some extent currently, but the pending label can only be a string for starters, and custom disabled logic is also not possible (without repeating this for every form).
from remix-forms.
@wladiston @bvangraafeiland, I just added isLoading
to the button on our Chakra UI example to demonstrate how we can accomplish that without useFormContext
.
However, that solution will not work if you're using the fetcher
prop. Because of that, I created #122 to create an even more sustainable solution.
from remix-forms.
Regarding useFormContext
, all methods returned by it are already passed to the children of Form
, including formState
, which has the isSubmitting
value.
That said, I wouldn't use RHF's state to check for the pending state in Remix or React Router 6.4. I would useTransition, useNavigation, or useFetcher.
from remix-forms.
Regarding
useFormContext
, all methods returned by it are already passed to the children ofForm
, includingformState
, which has theisSubmitting
value.That said, I wouldn't use RHF's state to check for the pending state in Remix or React Router 6.4. I would useTransition, useNavigation, or useFetcher.
I'm not seeing those props? This is what I see when logging the props of Input
For the submitting state I was indeed using Remix's useTransition
from remix-forms.
@bvangraafeiland, here's how you access the UseFormReturn:
<Form schema={schema}>
{({ Field, Errors, Button, register, formState /* and everything in UseFormReturn, all type-safe to the schema */ }) => (<>/* Your markup */</>)}
</Form>
from remix-forms.
Yes, but that's meant to be used on a per-form basis, right? I think I'm not explaining this properly. What I'm looking for is a way to have the submit button:
- Have the
disabled
prop set based on a combination of Remix'stransition
andformState.isValid
- While submitting, the button's content should be a loading spinner - there is the
pendingButtonLabel
prop, but it's of typestring
. I guess this could be changed toReactNode
but in the end there is more flexibility if the custom component could access state through a hook. - This behavior should be enabled by default on all forms that are created through my
Form
component based oncreateForm
Currently I have this behavior for a single form like this:
{({ Field, Button, Errors, formState }) => (
<>
<Field name="email" />
<Field name="password" type="password" />
<Errors />
<Button disabled={!!transition.submission || (!formState.isValid && formState.submitCount > 0)}>
Log in
</Button>
</>
)}
But that would need to be copied for every form. Having useFormState
and useField
available in custom components would make this, but also things like styling for errors, a lot easier. Currently I'm (ab)using the aria attributes being put on the input components for errors with tailwind using aria-invalid:border-red-600
.
from remix-forms.
Yeah, that's the use case I had in mind when creating #122. Your idea of adding hooks would also work, and I'd love to explore both :) if you create issues for them we'll explore that path as well.
from remix-forms.
Yeah that would definitely solve my use case 👍 If it helps, I could try opening a PR for it?
from remix-forms.
Yeah that would definitely solve my use case 👍 If it helps, I could try opening a PR for it?
Definitely! We'd really appreciate a PR, @bvangraafeiland 💪🏼
from remix-forms.
Released on v1.3.0. Thanks for the PR, @bvangraafeiland 💪🏼 🎉
from remix-forms.
Related Issues (20)
- Attach errors to Fields that are dynamically generated? HOT 2
- Inconsistent display of selected option in select control HOT 4
- Custom Component typings HOT 2
- Error: Cannot submit element that is not <form>, <button>, or <input type="submit|image"> HOT 1
- Multiple Checkboxes HOT 3
- Define Input type through fieldTypes HOT 5
- Bring `errorMessagesForSchema` from `domain-functions` to this library HOT 1
- form with mode="onChange" button flashes when it hydrates HOT 2
- Could not resolve "@deepkit/type" HOT 3
- Overriding fields HOT 2
- <Field hidden /> is not passing `type="hidden"` to its input
- Validation error not shown when field is an array
- Can't trigger onSubmit from Form-component HOT 6
- Handle `z.object` HOT 1
- Can remix-forms function within a PWA / client side only context? HOT 4
- Zod schema array of objects HOT 1
- Remix forms site example no longer maintained? HOT 1
- [BUG] Field error(s) not showing if using additional component HOT 1
- Input ID's not always unique when multiple forms are rendered
- Example of how to do crud correctly?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remix-forms.