Skip to content

Commit

Permalink
feat: add tooltip to button
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmichel committed May 29, 2023
1 parent 7a5ed22 commit 19ce0a7
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
import { useBootstrapPrefix } from './ThemeProvider';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
import { ButtonVariant } from './types';
import OverlayTrigger from './OverlayTrigger';
import Tooltip from './Tooltip';
import { v4 as uuid } from 'uuid';

const faAsterisk = {
prefix: 'fal',
Expand All @@ -35,6 +38,7 @@ export interface ButtonProps
animateIcon?: boolean;
animateInfiniteIcon?: boolean;
animateIconClass?: any;
tooltip?: any;
}

export type CommonButtonProps = 'href' | 'size' | 'variant' | 'disabled';
Expand Down Expand Up @@ -106,6 +110,8 @@ const propTypes = {
type: PropTypes.oneOf(['button', 'reset', 'submit', null]),

as: PropTypes.elementType,

tooltip: PropTypes.any,
};

const defaultProps = {
Expand Down Expand Up @@ -139,6 +145,7 @@ export const Button: BsPrefixRefForwardingComponent<'button', ButtonProps> =
animateIcon,
animateIconClass,
animateInfiniteIcon,
tooltip = null,
...props
},
ref
Expand Down Expand Up @@ -170,7 +177,7 @@ export const Button: BsPrefixRefForwardingComponent<'button', ButtonProps> =
animateIcon &&
animateIconClass &&
`animate__animated ${animateIconClass}`,
animateInfiniteIcon && 'animate__infinite'
animateInfiniteIcon && 'animate__infinite'
);

if (animateIcon) {
Expand Down Expand Up @@ -198,7 +205,11 @@ export const Button: BsPrefixRefForwardingComponent<'button', ButtonProps> =
);
};

return (
const renderTooltip = () => (
<Tooltip id={`button-tooltip-${uuid()}`}>{tooltip}</Tooltip>
);

const RenderButton = () => (
<Component
{...buttonProps}
{...props}
Expand Down Expand Up @@ -226,6 +237,15 @@ export const Button: BsPrefixRefForwardingComponent<'button', ButtonProps> =
)}
</Component>
);
if (!tooltip) {
return <RenderButton />;
}

return (
<OverlayTrigger placement="top" overlay={renderTooltip}>
<RenderButton />
</OverlayTrigger>
);
}
);

Expand Down

0 comments on commit 19ce0a7

Please sign in to comment.