|
| 1 | +import { ButtonHTMLAttributes, PropsWithChildren } from 'react' |
| 2 | +import { Link, LinkProps } from 'react-router-dom' |
| 3 | +import { Progressing } from '@Common/Progressing' |
| 4 | +import { Tooltip } from '@Common/Tooltip' |
| 5 | +import { ComponentSizeType } from '@Shared/constants' |
| 6 | +import { ButtonComponentType, ButtonProps, ButtonStyleType, ButtonVariantType } from './types' |
| 7 | +import { BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP, BUTTON_SIZE_TO_LOADER_SIZE_MAP } from './constants' |
| 8 | +import { getButtonDerivedClass } from './utils' |
| 9 | +import './button.scss' |
| 10 | + |
| 11 | +const ButtonElement = ({ |
| 12 | + component = ButtonComponentType.button, |
| 13 | + linkProps, |
| 14 | + buttonProps, |
| 15 | + onClick, |
| 16 | + ...props |
| 17 | +}: PropsWithChildren< |
| 18 | + Omit< |
| 19 | + ButtonProps, |
| 20 | + | 'text' |
| 21 | + | 'variant' |
| 22 | + | 'size' |
| 23 | + | 'style' |
| 24 | + | 'startIcon' |
| 25 | + | 'endIcon' |
| 26 | + | 'showTooltip' |
| 27 | + | 'tooltipProps' |
| 28 | + | 'dataTestId' |
| 29 | + | 'isLoading' |
| 30 | + > & { |
| 31 | + className: string |
| 32 | + 'data-testid': ButtonProps['dataTestId'] |
| 33 | + } |
| 34 | +>) => { |
| 35 | + if (component === ButtonComponentType.link) { |
| 36 | + return ( |
| 37 | + <Link |
| 38 | + {...linkProps} |
| 39 | + {...props} |
| 40 | + // Added the specific class to ensure that the link override is applied |
| 41 | + className={`${props.className} button__link ${props.disabled ? 'dc__disable-click' : ''}`} |
| 42 | + onClick={onClick as LinkProps['onClick']} |
| 43 | + /> |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <button |
| 49 | + {...buttonProps} |
| 50 | + {...props} |
| 51 | + // eslint-disable-next-line react/button-has-type |
| 52 | + type={buttonProps?.type || 'button'} |
| 53 | + onClick={onClick as ButtonHTMLAttributes<HTMLButtonElement>['onClick']} |
| 54 | + /> |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Generic component for Button. |
| 60 | + * Should be used in combination of variant, size and style. |
| 61 | + * |
| 62 | + * @example Default button |
| 63 | + * ```tsx |
| 64 | + * <Button text="Hello World" /> |
| 65 | + * ``` |
| 66 | + * |
| 67 | + * @example Custom variant |
| 68 | + * ```tsx |
| 69 | + * <Button text="Hello World" variant={ButtonVariantType.secondary} /> |
| 70 | + * ``` |
| 71 | + * |
| 72 | + * @example Custom size |
| 73 | + * ```tsx |
| 74 | + * <Button text="Hello World" size={ComponentSizeType.medium} /> |
| 75 | + * ``` |
| 76 | + * |
| 77 | + * @example Custom style |
| 78 | + * ```tsx |
| 79 | + * <Button text="Hello World" style={ButtonStyleType.positive} /> |
| 80 | + * ``` |
| 81 | + * |
| 82 | + * @example Disabled state |
| 83 | + * ```tsx |
| 84 | + * <Button text="Hello World" disabled /> |
| 85 | + * ``` |
| 86 | + * |
| 87 | + * @example Loading state |
| 88 | + * ```tsx |
| 89 | + * <Button text="Hello World" isLoading /> |
| 90 | + * ``` |
| 91 | + * |
| 92 | + * @example With start icon |
| 93 | + * ```tsx |
| 94 | + * <Button text="Hello World" startIcon={<ICCube />} /> |
| 95 | + * ``` |
| 96 | + * |
| 97 | + * @example With end icon |
| 98 | + * ```tsx |
| 99 | + * <Button text="Hello World" endIcon={<ICCube />} /> |
| 100 | + * ``` |
| 101 | + * |
| 102 | + * @example With tippy |
| 103 | + * ```tsx |
| 104 | + * <Button text="Hello World" showTippy tippyContent="Tippy content" /> |
| 105 | + * ``` |
| 106 | + * |
| 107 | + * @example With onClick |
| 108 | + * ```tsx |
| 109 | + * <Button text="Hello World" onClick={noop} /> |
| 110 | + * ``` |
| 111 | + * |
| 112 | + * @example Link component |
| 113 | + * ```tsx |
| 114 | + * <Button component={ButtonComponentType.link} linkProps={{ to: '#' }} /> |
| 115 | + * ``` |
| 116 | + */ |
| 117 | +const Button = ({ |
| 118 | + dataTestId, |
| 119 | + text, |
| 120 | + variant = ButtonVariantType.primary, |
| 121 | + size = ComponentSizeType.large, |
| 122 | + style = ButtonStyleType.default, |
| 123 | + startIcon = null, |
| 124 | + endIcon = null, |
| 125 | + disabled = false, |
| 126 | + isLoading = false, |
| 127 | + showTooltip = false, |
| 128 | + tooltipProps = {}, |
| 129 | + ...props |
| 130 | +}: ButtonProps) => { |
| 131 | + const isDisabled = disabled || isLoading |
| 132 | + const iconClass = `dc__no-shrink flex dc__fill-available-space ${BUTTON_SIZE_TO_ICON_CLASS_NAME_MAP[size]}` |
| 133 | + |
| 134 | + return ( |
| 135 | + <Tooltip {...tooltipProps} alwaysShowTippyOnHover={showTooltip && !!tooltipProps?.content}> |
| 136 | + <div> |
| 137 | + <ButtonElement |
| 138 | + {...props} |
| 139 | + disabled={isDisabled} |
| 140 | + className={`br-4 flex cursor dc__mnw-100 dc__tab-focus dc__position-rel dc__capitalize ${getButtonDerivedClass({ size, variant, style, isLoading })} ${isDisabled ? 'dc__disabled' : ''}`} |
| 141 | + data-testid={dataTestId} |
| 142 | + > |
| 143 | + {startIcon && <span className={iconClass}>{startIcon}</span>} |
| 144 | + <span className="dc__mxw-150 dc__align-left dc__truncate">{text}</span> |
| 145 | + {endIcon && <span className={iconClass}>{endIcon}</span>} |
| 146 | + {isLoading && <Progressing size={BUTTON_SIZE_TO_LOADER_SIZE_MAP[size]} />} |
| 147 | + </ButtonElement> |
| 148 | + </div> |
| 149 | + </Tooltip> |
| 150 | + ) |
| 151 | +} |
| 152 | + |
| 153 | +export default Button |
0 commit comments