如何使用样式化组件和打字稿覆盖按钮 Ant 设计?



我想用打字稿和样式组件覆盖按钮蚂蚁设计以个性化样式。但是当我尝试时,结果并不好。

我尝试了一些测试,但从来没有良好的显示。

我尝试这样 如何用样式化组件和打字稿包装 Ant 设计? 但没有结果。

但结果没有蚂蚁设计组件的风格,而是按钮继承了按钮蚂蚁设计和她的道具。

按钮样式组件

import { Button as AntButton } from "antd";
import { NativeButtonProps } from "antd/lib/button/button";
import * as React from "react";
import styledComponents from "styled-components";
export const Button = styledComponents<NativeButtonProps>(props => (
<AntButton {...props} />
))`
pType: string;
pSize: string;
pShape: string;
`;

按钮

import * as React from "react";
import { Button } from "./ButtonStyleComponent";
export interface ButtonProps {
pType?: "primary" | "secondary" | "danger";
pSize?: "small" | "medium" | "large";
pShape?: "round" | "rect";
}
class Button extends React.Component<ButtonProps> {
render() {
return (
<Button
{...this.props}
pType={this.props.pType}
pSize={this.props.pSize}
pShape={this.props.pShape}
>
{this.props.children}
</Button>
);
}
}

导出 { 按钮 };

问题是当我执行此代码时,我只看到一个丑陋的灰色按钮,而不是带有 Ant 设计按钮设计的按钮。

另一个问题是当我尝试调用 Ant Design Button 的方法时,该方法无法识别(例如:方法onClick (。

这是我使用antd做过的事情非常相似的一个例子

import { Button } from 'antd'
import { ButtonProps } from 'antd/lib/button'
import useStyles from 'isomorphic-style-loader-forked/useStyles'
import React from 'react'
import s from './CardButton.css'
interface CardButtonProps extends ButtonProps {
onClick?: () => void
}
const CardButton = ({ children, onClick, ...rest }: CardButtonProps) => {
useStyles(s)
return (
<Button ghost type="primary" className={s.root} onClick={onClick} {...rest}>
{children}
</Button>
)
}
export default CardButton

但是对于您的问题,也许这个答案会更有帮助 如何使用样式化组件和 TypeScript 总结 Ant 设计?

在setyled组件网站的常见问题解答中有答案。
下面我覆盖了 Antd 按钮的默认背景颜色。

import React from "react";
import "antd/dist/antd.css";
import { Button } from "antd";
import styled from "styled-components";
const MyButton = styled(Button)`
&&& {
background: red;
}
`;
export default function App() {
return (
<div className="App">
<MyButton>Click</MyButton>
</div>
);
}

最新更新