Reputation: 293
I would like to extend the props of the Button component from Material-UI using typescript to be able pass additional props to it's children.
import { NavLink } from 'react-router-dom';
import { Button } from 'material-ui';
<Button
component={NavLink}
// NavLink props that needs to be added to typescript declarations
activeClassName={classes.activeButton}
exact={true}
to={to}
>
{title}
</Button>
I've tried to add the following declarations in ./app/types/material_ui.d.ts:
declare module 'material-ui' {
interface Button {
activeClassName?: any;
exact?: boolean;
to?: string;
}
}
Which throws the error "TS2693: 'Button' only refers to a type, but is being used as a value here.".
I've also tried the declaration:
import * as React from 'react';
import { PropTypes, StyledComponent } from 'material-ui';
import { ButtonBaseProps } from 'material-ui/ButtonBase';
declare module 'material-ui' {
export interface ButtonProps extends ButtonBaseProps {
color?: PropTypes.Color | 'contrast';
component?: React.ReactNode;
dense?: boolean;
disabled?: boolean;
disableFocusRipple?: boolean;
disableRipple?: boolean;
fab?: boolean;
href?: string;
raised?: boolean;
type?: string;
activeClassName?: any;
exact?: boolean;
to?: string;
}
export class Button extends StyledComponent<ButtonProps> { }
}
Which throws the error "TS2323: Cannot redeclare exported variable 'Button'".
My tsconfig.json looks like this:
{
"compilerOptions": {
...
"paths": {
"history": ["./node_modules/@types/history/index"],
"redux": ["./node_modules/@types/redux/index"],
"react": ["./node_modules/@types/react/index"],
"*": ["./app/types/*", "*"]
},
},
...
}
Finally the original type definition from Material-UI:
import * as React from 'react';
import { StyledComponent, PropTypes } from '..';
import { ButtonBaseProps } from '../ButtonBase';
export interface ButtonProps extends ButtonBaseProps {
color?: PropTypes.Color | 'contrast';
component?: React.ReactNode;
dense?: boolean;
disabled?: boolean;
disableFocusRipple?: boolean;
disableRipple?: boolean;
fab?: boolean;
href?: string;
raised?: boolean;
type?: string;
}
export default class Button extends StyledComponent<ButtonProps> {}
I am using material-ui 1.0.0-beta.8 with react 15.6.1, react-router-dom 4.2.2 and typescript 2.5.2.
Upvotes: 29
Views: 38483
Reputation: 1985
It depends what you want to extend. For example for variant of chips in place of createTheme u need to declare:
declare module '@mui/material/Chip' {
interface ChipPropsVariantOverrides {
dot: boolean
}
}
and if you want to add new property for example to Badge component u need to use BadgeOwnProps (I think <COMPONENT_NAME>OwnProps in general)
declare module '@mui/material/Badge' {
interface BadgeOwnProps {
removable: boolean
}
}
It depends on component u need to check source code of MUI. For example in case of Chip: https://github.com/mui/material-ui/blob/v5.x/packages/mui-material/src/Chip/Chip.d.ts#L8
and in case of Badge: https://github.com/mui/material-ui/blob/v5.x/packages/mui-material/src/Badge/Badge.d.ts
Upvotes: 0
Reputation: 905
A bit late, but here is another solution. See also the MUI docs Usage of component prop . The following button uses the Link component. It allso uses the Link's specific props with their types. It allso adds its own sesific props.
import React from 'react'
import MuiButton, {ButtonProps} from '@material-ui/core/Button'
interface ButtonOptions {
// your custom options with their types
}
const Button = <C extends React.ElementType>(props: ButtonProps<C, {component?: C}> & ButtonOptions ) => {
return <MuiButton {...props}>{props.children}</MuiButton>
}
export default Button
// use like this:
import {Link} from 'react-router-dom'
<Button component={Link} to={'/somelocation'}>
Upvotes: 26
Reputation: 269
import React, { FC } from 'react';
import { Button, ButtonProps } from '@material-ui/core';
interface IProps extends ButtonProps {} // your custom props
const ButtonHco: FC<IProps> = ({
variant,
color,
children,
size,
disabled
}) => {
return (
<Button variant={variant} color={color} size={size} disabled={disabled}>
{children}
</Button>
);
};
export default ButtonHco;
Upvotes: 14
Reputation: 2552
Here is a minimalistic sample of extending a Material UI Button (untested). The extended field will be simply appended after the button label.
import * as React from 'react';
import { ButtonProps } from '@material-ui/core/Button';
import { Button } from '@material-ui/core';
interface Props{
extendedField: string;
}
export class MyExtendedButton extends React.Component<ButtonProps & Props>{
render(){
const {
extendedField,
children,
...buttonProps,
} = this.props;
return (<Button {...buttonProps}>
{children}
: <span style={{color: 'red'}}>
{extendedField}
</span>
</Button>);
}
}
Upvotes: 5
Reputation: 2911
The following code works for me
import { Button, StyledComponent } from 'material-ui';
import { ButtonProps } from 'material-ui/Button';
declare module 'material-ui' {
export interface MyProps {
exact?: boolean;
to?: string;
}
export class Button extends StyledComponent<ButtonProps & MyProps> {
}
}
I don't have the problem with "TS2693: 'Button' only refers to a type, but is being used as a value here.
and I'm also using Typescript 2.5.2
Upvotes: 9