Code Monkey home page Code Monkey logo

Comments (11)

danielweinmann avatar danielweinmann commented on May 28, 2024 1

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.

bvangraafeiland avatar bvangraafeiland commented on May 28, 2024

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.

danielweinmann avatar danielweinmann commented on May 28, 2024

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

danielweinmann avatar danielweinmann commented on May 28, 2024

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.

bvangraafeiland avatar bvangraafeiland commented on May 28, 2024

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.

I'm not seeing those props? This is what I see when logging the props of Input
Screenshot 2022-12-07 at 14 11 35

And this for Button:
Screenshot 2022-12-07 at 14 12 27

For the submitting state I was indeed using Remix's useTransition

from remix-forms.

danielweinmann avatar danielweinmann commented on May 28, 2024

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

bvangraafeiland avatar bvangraafeiland commented on May 28, 2024

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's transition and formState.isValid
  • While submitting, the button's content should be a loading spinner - there is the pendingButtonLabel prop, but it's of type string. I guess this could be changed to ReactNode 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 on createForm

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.

danielweinmann avatar danielweinmann commented on May 28, 2024

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.

bvangraafeiland avatar bvangraafeiland commented on May 28, 2024

Yeah that would definitely solve my use case 👍 If it helps, I could try opening a PR for it?

from remix-forms.

danielweinmann avatar danielweinmann commented on May 28, 2024

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.

danielweinmann avatar danielweinmann commented on May 28, 2024

Released on v1.3.0. Thanks for the PR, @bvangraafeiland 💪🏼 🎉

from remix-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.