基于ant design组件做二次开发的例子,最好用Button举例,要有类型的继承,要用tsx
下面是一个基于 ant design Button 组件的二次开发示例,使用了 TypeScript 和 JSX:
import React from 'react';
import { Button, ButtonProps } from 'antd';
// 定义一个新的按钮类型,继承自 antd ButtonProps
interface MyButtonProps extends ButtonProps {
// 新增一个属性 color,表示按钮颜色
color?: 'primary' | 'secondary';
}
// 定义新的按钮组件
const MyButton: React.FC<MyButtonProps> = ({ color, ...props }) => {
// 根据 color 属性设置按钮样式
const style = color === 'primary'
? { backgroundColor: 'blue', color: 'white' }
: { backgroundColor: 'gray', color: 'white' };
// 返回 antd Button 组件,并应用样式
return <Button style={style} {...props} />;
};
// 使用新的按钮组件
const Example = () => {
return (
<div>
<MyButton color="primary">Primary Button</MyButton>
<MyButton color="secondary">Secondary Button</MyButton>
</div>
);
};
在上面的例子中,我们首先定义了一个新的按钮类型 MyButtonProps,它继承自 antd ButtonProps,并新增了一个 color 属性,表示按钮颜色。
然后,我们定义了一个新的按钮组件 MyButton,它接收 MyButtonProps 类型的属性,并根据 color 属性设置按钮样式。最后,我们在 Example 组件中使用了新的按钮组件。
这样,我们就基于 antd Button 组件实现了一个具有新功能的按钮组件,并且可以在应用中方便地使用它。
原文地址: https://www.cveoy.top/t/topic/sy4 著作权归作者所有。请勿转载和采集!