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

[material-ui][Dialog] Compatibility between MUI's dialogs and React Hook Form with FormProvider #43732

Open
1 task done
michaelperrin opened this issue Sep 12, 2024 · 0 comments
Assignees
Labels
component: dialog This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@michaelperrin
Copy link

michaelperrin commented Sep 12, 2024

Search keywords

React Hook Form FormProvider Dialog

Latest version

  • I have tested the latest version

Summary

It is currently not possible to make use of React Hook Form context in Material UI dialogs as the <form> element is commonly encapsulated in a FormProvider component.
That FormProvider component is a context provider, and no style can be applied to is as a result.
This makes it impossible to pass a FormProvider instance to the component property on the PaperProps property of the MUI Dialog component.

ℹ️ That limitation is not restricted to the use of React Hook Form only, but to any use of context providers used as form parents.

Motivation

A MUI dialog can be configured to be used for forms, as described on Material UI's docs. Simple forms will work fine, but complex forms using React Hook Form with FormProvided can't be used.

Here's why 🙂

To put it in context, a dialog form has to encapsulate all the following children of the Dialog component:

  • DialogTitle
  • DialogContent
  • DialogActions

This allows to get form fields (which are part of the DialogContent children) to be submitted when clicking on the submit button as a child of the DialogActions component.

To make it work, the parent Dialog component can be configured so that its inner Paper child can be configured to be rendered as a form element with some specific properties:

<Dialog
  // ...
  PaperProps={{
    component: 'form',
    onSubmit: (event) => {
      event.preventDefault();
      // ...
    },
  }}
>

And here is the limitation I am facing. Complex forms using React Hook Form commonly encapsulate the <form> element in a <FormProvider> component so that form children components can get access to React Hook Form's context (see FormProvider documentation):

<FormProvider {...methods}>
  // pass all methods into the context
  <form onSubmit={methods.handleSubmit(onSubmit)}>
    <NestedInput />
    // ...
  </form>
</FormProvider>

It is currently not possible to define this FormProvider as a parent of the form element when using forms in MUI's dialogs.

@michaelperrin michaelperrin added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 12, 2024
@michaelperrin michaelperrin changed the title [material-ui][Dialog] Compatibility between React Hook Form with FormProvider and Dialogs [material-ui][Dialog] Compatibility between MUI's dialogs and React Hook Form with FormProvider Sep 12, 2024
@zannager zannager added the component: dialog This is the name of the generic UI component, not the React module! label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: dialog This is the name of the generic UI component, not the React module! status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

3 participants