-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[FormControl] Cannot render FormControl
as React.Fragment
#38094
Comments
One possible (but silly) workaround: const Noop: React.FC<React.PropsWithChildren> = ({ children }) => (
<>{children}</>
);
const MyAwesomeFormLine: React.FC = () => {
<FormControl component={Noop}>
// Label + Input
</FormControl>
}; So basically |
@thany I'm pretty sure this cannot work as
Can you share more about your use-case or the layout you are trying to achieve? |
FormControl
as React.Fragment
FormControl
as React.Fragment
Each FormControl contains a label and an input control. I (we & our designer) despise the abel-inside-or-above-the-control layout, and we have to deliver a classic but proven layout, labels on the left, fields on the right. This is impossible to achieve if each FormControl produces a Currently, having multiple FormControl components for a form, produces roughly this (pseudo code):
Obne cannot apply CSS Grid on this and expect the labels and inputs to magically become the grid cells. CSS Grid just simply doesn't work like that. The only correct way to get this to render correct as a grid, is to eliminate those divs:
So that the labels and inputs become children of the form, which is the CSS Grid. Now, to be brutally honest, I don't see why FormControl would absolutely need to render itself as a div. What for? The way I see it, FormControl is merely a empty component to provide some functionality to its contained form element components. It doesn't need to render anything itself. It current does (or tries to anyway 😒) but to what end? I don't need its classnames to exist in the HTML structure, and I certainly don't like to be limited to having each label+input combination wrapped in another element - I need and want the freedom not to require this. To put this more generally - a UI framework should not dictate what HTML elements should be rendered outside its components. It should never render any HTML elements that only it needs, but the browser doesn't. Any wrapping functional-only component should not render any HTML, but should only provide the intended functionality to its child components. That's a lot of "should" so feel free to disagree, as long as we can solve the problem described above 😉 Now, if there's any other component than FormControl I should be using instead, while still providing states and whatnot to its child input components, I'm all ears to hear about it 🙂 Edit:
Btw if I take that literally - FormControl can still take all the props in the world without rendering anything. Recieving props has nothing to do with whatever a component returns as. I'm sure you know this, but I just wanted to make sure we're on the same page. The issue is that FormControl, being able to be told what component to render as, blindly assumes that everything it passes to that component, it can receive on its turn, which is clearly an unsafe assumption. |
@mj12albert, I think we should be able to allow this. The FormControl doesn't set or rely on event handlers. We could either allow |
I would rather like the latter, because it's entirely possible to build a component that also cannot take a |
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example:
https://codesandbox.io/s/zealous-benz-3zmg7x?file=/src/App.tsx
Steps:
FormControl
component anywhereFragment
Current behavior 😯
It produces an error in the console:
Expected behavior 🤔
No error.
Context 🔦
I'm doing this because I need FormControl not to produce extra elements around my form elements, because of CSS Grid.
Applying
display: contents
is not sufficient for a CSS Grid to work, andsubgrid
is sadly still not widely enough supported.Your environment 🌎
npx @mui/envinfo
I'm using Firefox 115, just in case it matters.
The text was updated successfully, but these errors were encountered: