import { Button as MuiButton, type ButtonProps as MuiButtonProps } from '@mui/material'; import { type ReactElement, type ReactNode, useMemo } from 'react'; import type { AriaLabelingAttributes } from '../../@types/aria-attributes'; import type { DataTestAttributes } from '../../@types/data-attributes'; const muiVariantMap: Record< Required['variant'], 'text' | 'outlined' | 'contained' > = { ghost: 'text', primary: 'contained', secondary: 'outlined' }; export type ButtonProps = AriaLabelingAttributes & DataTestAttributes & Omit & { children: ReactNode; className?: string; disabled?: boolean; icon?: string | ReactNode; iconVariant?: 'none' | 'start' | 'end'; isDanger?: boolean; onClick?: (e: React.MouseEvent) => void; ref?: React.Ref; size?: 'small' | 'medium' | 'large'; type?: 'button' | 'submit' | 'reset'; variant?: 'primary' | 'secondary' | 'ghost'; }; const Button = ({ children, variant = 'primary', size = 'medium', iconVariant = 'none', icon, type = 'button', disabled = false, onClick, isDanger = false, className = '', ...attr }: ButtonProps): ReactElement => { const MuiOverrideProps = useMemo( () => ({ color: 'primary' as const, ...(iconVariant === 'start' && { startIcon: icon }), ...(iconVariant === 'end' && { endIcon: icon }) }), [icon, iconVariant] ); return ( onClick?.(e)} size={size} type={type} variant={muiVariantMap[variant]} {...MuiOverrideProps} {...attr} > {children} ); }; export { Button };