Skip to content

[Typescript] Button do not accept custom component #16887

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

Closed
michael-land opened this issue Aug 5, 2019 · 9 comments
Closed

[Typescript] Button do not accept custom component #16887

michael-land opened this issue Aug 5, 2019 · 9 comments
Labels
duplicate This issue or pull request already exists typescript

Comments

@michael-land
Copy link
Contributor

michael-land commented Aug 5, 2019

  • [ x] The issue is present in the latest release.
  • [ x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

              <Button component={Link} variant="outlined" size="small">
                Sign in
              </Button>

image

image

image

Expected Behavior 🤔

It should be able to pass component
image

Steps to Reproduce 🕹

Steps:

Context 🔦

Your Environment 🌎

//tsconfig.json
{
  "compilerOptions": {
    "allowJs": true,
    "baseUrl": ".",
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "module": "esnext",
    "skipLibCheck": true,
    "moduleResolution": "node",
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "paths": {
      "@/*": ["./*"]
    },
    "removeComments": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "strictPropertyInitialization": false,
    "target": "esnext",
    "isolatedModules": true
  }
}
Tech Version
Material-UI v4.3.1
React 16.8.6
Browser
TypeScript 3.5.3
etc.
@eps1lon
Copy link
Member

eps1lon commented Aug 5, 2019

Please include where Button and Link come from.

@eps1lon eps1lon added the status: waiting for author Issue with insufficient information label Aug 5, 2019
@vtrphan
Copy link

vtrphan commented Aug 5, 2019

i also have this problem currently, after upgrading to MUI 4. My Button component comes from "@material-ui/core" and Link is my custom component. Please advise

@eps1lon
Copy link
Member

eps1lon commented Aug 5, 2019

Please show the full source.

@vtrphan
Copy link

vtrphan commented Aug 5, 2019

sure

export class Link extends React.PureComponent<LinkProps, {}> {
...
return (
      <a {...others} className={cssClass}>
        {icon && this.renderIcon()}
        <span>{children}</span>
      </a>
    );
}
const DownloadLink = (props: any) => <Link href={this.getDownloadLink()} underline={false} {...props} />;

<Button
              disabled={!isValid || isSubmitting}
              component={DownloadLink}
              onClick={() => this.setState({ isSubmitting: true }, () => setTimeout(this.close, 1000))}
              color="secondary"
 >

TypeScript 3.0.3
MUI: latest
Error:

Property 'component' does not exist on type 'IntrinsicAttributes & { action?: ((instance: ButtonBaseActions | null) => void) | RefObject<ButtonBaseActions> | null | undefined; buttonRef?: ((instance: unknown) => void) | RefObject<unknown> | null | undefin
ed; ... 7 more ...; TouchRippleProps?: Partial<...> | undefined; } & { ...; } & CommonProps<...> & Pick<...>'.

@eps1lon
Copy link
Member

eps1lon commented Aug 5, 2019

This should trigger runtime warnings. https://material-ui.com/components/buttons/#ButtonRouter.tsx shows an example how to properly create a component for the runtime and how to type it.

@eps1lon eps1lon closed this as completed Aug 5, 2019
@vtrphan
Copy link

vtrphan commented Aug 5, 2019

How is that different from my implementation, except for the "forwardRef" ? Is it generally required or only required if i use react-router?

@vtrphan
Copy link

vtrphan commented Aug 5, 2019

There's a comment here from another open but similar issue:
#16846 (comment)
Apparently implementing with the forwardRef solves the issue, but will not work with styled component?

@khryshyn
Copy link

khryshyn commented Aug 5, 2019

The solution is not working if you're using the styled-components

@vishalrajole
Copy link

vishalrajole commented Jul 22, 2021

I confirm same issue with Button and styled components in version

"@emotion/react": "11.4.0",
"@emotion/styled": "11.3.0",
"@material-ui/core": "5.0.0-alpha.37",
"@material-ui/icons": "5.0.0-alpha.37",
"styled-components": "5.2.3",
"typescript": "4.2.4"

I am consuming styled button as below

<S.ProfileButton
      aria-describedby={id}
      component="span"                // <=== _typescript errors out here for component prop_
      onClick={handleClick}
      endIcon={<ExpandMoreProfileIcon />}>
      .......other code
</S.ProfileButton>

Button wrapped as Styled component

import { Button } from '@material-ui/core';
........ other code
export const ProfileButton = styled(Button)`
    width: 100%;
    border-radius: 0;
    background-color: ${Colors.black};
    height: 72px;
}`;

Error


No overload matches this call.
  Overload 1 of 2, '(props: Omit<Omit<{ children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | "error" | "secondary" | "primary" | "success" | "info" | "warning" | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...>, never> & Partial<...>, "theme"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Type '{ children: Element; "aria-describedby": string | undefined; component: string; onClick: (event: MouseEvent<HTMLElement, MouseEvent>) => void; endIcon: Element; }' is not assignable to type 'IntrinsicAttributes & Omit<Omit<{ children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | ... 6 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...>, never> & Partial<...>, "theme"> & { ...; } & { ...; }'.
      Property 'component' does not exist on type 'IntrinsicAttributes & Omit<Omit<{ children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | ... 6 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...>, never> & Partial<...>, "theme"> & { ...; } & { ...; }'.
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<ExtendButtonBase<ButtonTypeMap<{}, "button">>, any, {}, never, ExtendButtonBase<ButtonTypeMap<{}, "button">>, ExtendButtonBase<...>>): ReactElement<...>', gave the following error.
    Type '{ children: Element; "aria-describedby": string | undefined; component: string; onClick: (event: MouseEvent<HTMLElement, MouseEvent>) => void; endIcon: Element; }' is not assignable to type 'IntrinsicAttributes & Omit<Omit<{ children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | ... 6 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...>, never> & Partial<...>, "theme"> & { ...; } & { ...; }'.
      Property 'component' does not exist on type 'IntrinsicAttributes & Omit<Omit<{ children?: ReactNode; classes?: Partial<ButtonClasses> | undefined; color?: "inherit" | ... 6 more ... | undefined; ... 9 more ...; variant?: "text" | ... 2 more ... | undefined; } & Omit<...> & CommonProps & Omit<...>, never> & Partial<...>, "theme"> & { ...; } & { ...; }'.ts(2769)

Here is code sandbox replicating same issue. https://codesandbox.io/s/mui-styled-button-vliel?file=/src/App.tsx
@oliviertassinari appreciate your help here again as this is blocking 👏

Screenshot 2021-07-22 at 9 50 33 AM

@oliviertassinari oliviertassinari added duplicate This issue or pull request already exists and removed status: waiting for author Issue with insufficient information labels Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists typescript
Projects
None yet
Development

No branches or pull requests

6 participants