|
| 1 | +import type { ButtonProps, TooltipProps } from 'antd'; |
| 2 | +import { type CSSProperties } from 'react'; |
| 3 | + |
| 4 | +import SvgIcon from './SvgIcon'; |
| 5 | + |
| 6 | +interface Props extends Omit<ButtonProps, 'icon' | 'iconPosition'> { |
| 7 | + children?: React.ReactNode; |
| 8 | + /** Button class */ |
| 9 | + className?: string; |
| 10 | + /** Iconify icon name */ |
| 11 | + icon?: string; |
| 12 | + style?: CSSProperties; |
| 13 | + /** Tooltip content */ |
| 14 | + tooltipContent?: string; |
| 15 | + /** Tooltip placement */ |
| 16 | + tooltipPlacement?: TooltipProps['placement']; |
| 17 | + /** Trigger tooltip on parent */ |
| 18 | + triggerParent?: boolean; |
| 19 | + zIndex?: number; |
| 20 | +} |
| 21 | + |
| 22 | +/** - 动态计算class */ |
| 23 | +const computeClass = (className: string) => { |
| 24 | + let clsStr = className; |
| 25 | + |
| 26 | + if (!clsStr.includes('h-')) { |
| 27 | + clsStr += ' h-36px'; |
| 28 | + } |
| 29 | + |
| 30 | + if (!clsStr.includes('text-')) { |
| 31 | + clsStr += ' text-icon'; |
| 32 | + } |
| 33 | + |
| 34 | + return clsStr; |
| 35 | +}; |
| 36 | + |
| 37 | +/** - 生成复用的button */ |
| 38 | + |
| 39 | +const ButtonIcon: FC<Props> = memo( |
| 40 | + ({ |
| 41 | + children, |
| 42 | + className = 'h-36px text-icon', |
| 43 | + icon, |
| 44 | + style, |
| 45 | + tooltipContent, |
| 46 | + tooltipPlacement = 'bottom', |
| 47 | + triggerParent, |
| 48 | + zIndex = 98, |
| 49 | + ...rest |
| 50 | + }) => { |
| 51 | + const cls = computeClass(className); |
| 52 | + |
| 53 | + function getPopupContainer(triggerNode: HTMLElement) { |
| 54 | + return triggerParent ? triggerNode.parentElement! : document.body; |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <ATooltip |
| 59 | + getPopupContainer={getPopupContainer} |
| 60 | + placement={tooltipPlacement} |
| 61 | + title={tooltipContent} |
| 62 | + zIndex={zIndex} |
| 63 | + > |
| 64 | + <AButton |
| 65 | + className={cls} |
| 66 | + type="text" |
| 67 | + {...rest} |
| 68 | + > |
| 69 | + <div className="flex-center gap-8px"> |
| 70 | + {children || ( |
| 71 | + <SvgIcon |
| 72 | + icon={icon} |
| 73 | + style={style} |
| 74 | + /> |
| 75 | + )} |
| 76 | + </div> |
| 77 | + </AButton> |
| 78 | + </ATooltip> |
| 79 | + ); |
| 80 | + } |
| 81 | +); |
| 82 | + |
| 83 | +export default ButtonIcon; |
0 commit comments