Skip to content

Commit

Permalink
feat: 🎸 [IconButton] Added optional tooltip
Browse files Browse the repository at this point in the history
✅ Closes: 167
  • Loading branch information
luciob committed Jul 12, 2022
1 parent 1db86dd commit 14f1a1c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/components/IconButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { FC, useCallback } from "react";
import MUIIconButton from "@material-ui/core/IconButton";
import React, { FC, useCallback, useMemo } from "react";
import { IconButton as MUIIconButton, Tooltip as MUITooltip } from "@material-ui/core";

import { IIconButton } from "../../types/IconButton";
import { getComposedDataCy, suppressEvent } from "../../utils";
import { getComposedDataCy, slugify, suppressEvent } from "../../utils";
import IconWrapper from "../IconWrapper";

export const DATA_CY_DEFAULT = "icon-button";
Expand All @@ -21,6 +21,7 @@ const IconButton: FC<IIconButton> = ({
rotate = false,
size = "medium",
style,
tooltip,
}) => {
const onClickHandler = useCallback(
(event: any) => {
Expand All @@ -30,10 +31,23 @@ const IconButton: FC<IIconButton> = ({
[onClick]
);

const iconButton = useMemo(
() => (
<MUIIconButton color="inherit" data-cy={dataCy} disabled={disabled} onClick={onClickHandler} style={style}>
<IconWrapper dataCy={getComposedDataCy(dataCy, SUBPARTS_MAP.icon)} icon={icon} rotate={rotate} size={size} />
</MUIIconButton>
),
[dataCy, disabled, icon, onClickHandler, rotate, size, style]
);

if (!tooltip) {
return iconButton;
}

return (
<MUIIconButton color="inherit" data-cy={dataCy} disabled={disabled} onClick={onClickHandler} style={style}>
<IconWrapper dataCy={getComposedDataCy(dataCy, SUBPARTS_MAP.icon)} icon={icon} rotate={rotate} size={size} />
</MUIIconButton>
<MUITooltip aria-label={slugify(tooltip)} title={tooltip}>
<span>{iconButton}</span>
</MUITooltip>
);
};

Expand Down
7 changes: 6 additions & 1 deletion src/types/IconButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { IClickable } from "./Base";
import { IIcon, IIconUtilizer } from "./Icon";
import { IDisablableInput } from "./Input";

export interface IBaseIconButton extends IClickable, IIconUtilizer {}
export interface IBaseIconButton extends IClickable, IIconUtilizer {
/**
* Text of tooltip
*/
tooltip?: string;
}

export type IIconButton = IBaseIconButton & IDisablableInput & Pick<IIcon, "rotate" | "size">;

0 comments on commit 14f1a1c

Please sign in to comment.