Open ImADrafter opened 4 years ago
This is unfortunately expected since this only works because you've wrapped the MenuItem
s in Select
. That component removes the value
via cloneElement
.
We'll fix this in v5 where we will have a dedicated component for Select
options.
This is unfortunately expected since this only works because you've wrapped the
MenuItem
s inSelect
. That component removes thevalue
viacloneElement
.We'll fix this in v5 where we will have a dedicated component for
Select
options.
First of all, thank you for taking the time to answer, and adding this issue to the milestone. I really appreciate it.
Your thought makes sense, but I've tried replacing the select with a fragment and the compilation still broken for the same cases. Is this still related to the underneath cloneElement
?
I'm not sure if what @eps1lon mentions is exactly the reason. As @ImADrafter says, it seems more a Typescript compile error than an implementation bug per se. In the JS version of the same code, there is not even an execution error or warning.
Basically, I've followed the Material-UI docs and examples to use the "component" prop in Typescript with the MenuItem + Link (react-router), and the result is that same compile error. So, I guess this could be one of the cases stated in the TS guide, where it says:
Not every component fully supports any component type you pass in. If you encounter a component that rejects its component props in TypeScript please open an issue. There is an ongoing effort to fix this by making component props generic.
What do you think?
I think I'm hitting the same issue with NextJs's Link:
import { forwardRef, Ref } from "react";
import Link, { LinkProps } from "next/link";
import { MenuItem, MenuItemProps } from "@material-ui/core";
type Props = MenuItemProps & Pick<LinkProps, "href" | "as" | "prefetch" | "locale">;
const LinkMenuItem = ({ href, as, prefetch, locale, ...props }: Props, ref: Ref<HTMLLIElement>) => (
<Link href={href} as={as} prefetch={prefetch} locale={locale} passHref>
{/* @ts-ignore https://github.com/mui-org/material-ui/issues/21210 probably going to be fixed in mui v5 */}
<MenuItem component="a" ref={ref} {...props} />
</Link>
);
export default forwardRef<HTMLLIElement, Props>(LinkMenuItem);
This code gives me Property 'component' does not exist on type...
Although it does render and work correctly, and doesnt throw anything in the console.
here we are in v5.2.4 and it doesn't seem like Select
's have their own option components. docs still using <MenuItem/>
's: https://mui.com/components/selects/
MenuItem docs make it look like <MenuItem component="" />
should be possible: https://mui.com/api/menu-item/
Combination of both prop
component
+ propvalue
seems incompatible.Current Behavior 😯
value
, a valid prop forMenuItem
(inherit fromli
native element) seems to break compilation, if provided along withcomponent
prop. No matter ifcomponent
is a native HTML element, a custom component or MUI component.Expected Behavior 🤔
You can use both props w/o typescript compilation errors.
Steps to Reproduce 🕹
As you can see in the demo, every combination should be acceptable. As you can see in the second case,
component="Typography"
lets you use any Typography prop along with MenuItem props. You can comment line 18 to check this out. But when it comes tovalue
, Typescript complains for line 15I've check out how MenuItem is basically a ListItem with aditional props, and the problem persists as well using this component. Probably is something with OverridableComponent interface or BaseButton, im not sure.
Context 🔦
This "value" prop, not only an acceptable prop for native element, but it is indirectly being supported as MUI documentation for Select allows you to use MenuItem as children, but I couldn't find a demo with, also, a prop component.
Your Environment 🌎