eit*_*ido 14 typescript reactjs material-ui
我想使用打字稿从 Material-UI 扩展 Button 组件的道具,以便能够将其他道具传递给它的孩子。
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>
Run Code Online (Sandbox Code Playgroud)
我尝试在 ./app/types/material_ui.d.ts 中添加以下声明:
declare module 'material-ui' {
interface Button {
activeClassName?: any;
exact?: boolean;
to?: string;
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发错误“TS2693:'Button' 仅指一种类型,但在此处用作值。”。
我也试过声明:
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> { }
}
Run Code Online (Sandbox Code Playgroud)
这会引发错误“TS2323:无法重新声明导出的变量 'Button'”。
我的 tsconfig.json 看起来像这样:
{
"compilerOptions": {
...
"paths": {
"history": ["./node_modules/@types/history/index"],
"redux": ["./node_modules/@types/redux/index"],
"react": ["./node_modules/@types/react/index"],
"*": ["./app/types/*", "*"]
},
},
...
}
Run Code Online (Sandbox Code Playgroud)
最后是 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> {}
Run Code Online (Sandbox Code Playgroud)
我正在使用 material-ui 1.0.0-beta.8 和 react 15.6.1、react-router-dom 4.2.2 和 typescript 2.5.2。
SeP*_*PeF 25
有点晚了,但这是另一个解决方案。另请参阅 MUI 文档组件 prop 的用法 。以下按钮使用链接组件。它还使用 Link 的特定 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'}>
Run Code Online (Sandbox Code Playgroud)
以下代码对我有用
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> {
}
}
Run Code Online (Sandbox Code Playgroud)
我没有问题,"TS2693: 'Button' only refers to a type, but is being used as a value here.我也在使用 Typescript 2.5.2
小智 6
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;
Run Code Online (Sandbox Code Playgroud)
这是扩展 Material UI 按钮的简约示例(未经测试)。扩展字段将简单地附加在按钮标签之后。
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>);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8812 次 |
| 最近记录: |