首页 > 其他分享 >react18-学习笔记42-button组件分析

react18-学习笔记42-button组件分析

时间:2023-02-12 21:32:49浏览次数:43  
标签:btnType button 42 ButtonType disabled export react18 children size


import React from "react";
import classNames from "classnames"
export enum ButtonSize{
Large='lg',
Small='sm'
}

export enum ButtonType{
Primary="primary",
Default="default",
Danger='danger',
Link='link',

}

interface BaseButtonProps{
className?:string,
disabled?:boolean,
size?:ButtonSize,
btnType?:ButtonType;
children:React.ReactNode;
href?:string;
}

const Button:React.FC<BaseButtonProps>=(props)=>{
const {
btnType,
disabled,
size,
children,
href
}=props

const classes=classNames("btn",{
[`btn-${btnType}`]:btnType,
[`btn-${size}`]:size,
'disabled':(btnType===ButtonType.Link)&&disabled
})
if(btnType===ButtonType.Link&&href){
return(
<a className={classes} href={href}>{children}</a>
)
}else{
return (
<button className={classes} disabled={disabled}>
{children}
</button>
)
}
}
Button.defaultProps={
disabled:false,
btnType:ButtonType.Default
}
export default Button

标签:btnType,button,42,ButtonType,disabled,export,react18,children,size
From: https://blog.51cto.com/u_15460007/6052200

相关文章