import cx from "classnames"; import React from "react"; export type ButtonSize = "small" | "large"; export type ButtonType = "primary" | "fill" | "ghost"; interface CommonProps { elemRef?: React.Ref; htmlType?: "button" | "submit" | "reset"; isActive?: boolean; isDefault?: boolean; isDisabled?: boolean; isSquare?: boolean; size?: ButtonSize; type?: ButtonType; className?: string; children?: React.ReactNode; preserveWidth?: boolean; } type AnchorProps = CommonProps & Omit< React.AnchorHTMLAttributes, keyof CommonProps | "type" | "ref" > & { href: string; }; type NativeButtonProps = CommonProps & Omit< React.ButtonHTMLAttributes, keyof CommonProps | "href" | "ref" > & { href?: undefined; }; export type ButtonProps = AnchorProps | NativeButtonProps; const CLASS_ROOT = "btn"; const Button = React.forwardRef((props, ref) => { const { className, children, htmlType = "button", href, isActive, isDisabled, size, isSquare, type, elemRef, ...other } = props; const classes = cx( CLASS_ROOT, { [`${CLASS_ROOT}--square`]: isSquare, [`${CLASS_ROOT}--${size}`]: size, [`${CLASS_ROOT}--${type}`]: type, [`${CLASS_ROOT}--preserve-width`]: props.preserveWidth, "is-active": isActive, }, className, ); if (href) { // Anchor element const anchorProps = other as React.AnchorHTMLAttributes; return ( {children} ); } else { // Button element const buttonProps = other as React.ButtonHTMLAttributes; return ( ); } }); Button.displayName = "Button"; export { Button };