From 3b4fc5959cadc90c6d4376b50c8d14791921c5d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Sl=C3=A1ma?= Date: Wed, 10 Jul 2024 16:39:20 +0200 Subject: [PATCH] [FX-5607] Allow to customize the color of icon (#4399) --- .changeset/modern-sheep-help.md | 8 ++ packages/base/Tag/src/Tag/Tag.tsx | 73 ++++++++++--------- .../Tag/src/Tag/__snapshots__/test.tsx.snap | 2 +- 3 files changed, 46 insertions(+), 37 deletions(-) create mode 100644 .changeset/modern-sheep-help.md diff --git a/.changeset/modern-sheep-help.md b/.changeset/modern-sheep-help.md new file mode 100644 index 0000000000..17f77d748a --- /dev/null +++ b/.changeset/modern-sheep-help.md @@ -0,0 +1,8 @@ +--- +'@toptal/picasso-tag': patch +'@toptal/picasso': patch +--- + +### Tag + +- allow to customize the color of an icon diff --git a/packages/base/Tag/src/Tag/Tag.tsx b/packages/base/Tag/src/Tag/Tag.tsx index b82b77a444..318058b9d9 100644 --- a/packages/base/Tag/src/Tag/Tag.tsx +++ b/packages/base/Tag/src/Tag/Tag.tsx @@ -40,6 +40,33 @@ export interface Props extends BaseProps, TextLabelProps, DivOrAnchorProps { endAdornment?: ReactNode } +type IconProps = { + className?: string + color?: string +} + +const DeleteIcon = ({ onClick }: { onClick: (event: MouseEvent) => void }) => ( + + + +) + +const cloneIcon = (icon: ReactNode, disabled?: boolean) => { + if (!icon || !React.isValidElement(icon)) { + return null + } + + return React.cloneElement(icon, { + color: disabled ? 'grey' : icon.props.color || 'darkGrey', + className: twMerge('flex items-center -mr-1 ml-3', icon.props.className), + }) +} + export const Tag = forwardRef(function Tag(props, ref) { const { as: Root = 'div', @@ -56,14 +83,12 @@ export const Tag = forwardRef(function Tag(props, ref) { role, ...rest } = props + const titleCase = useTitleCase(propsTitleCase) + const isInteractive = onDelete || onClick const handleDelete = (event: MouseEvent) => { - if (disabled) { - return - } - - if (onDelete) { + if (!disabled && onDelete) { event.preventDefault() /** @@ -72,44 +97,31 @@ export const Tag = forwardRef(function Tag(props, ref) { * options list. */ event.stopPropagation() - onDelete() } } - let iconCloned = null - - if (icon && React.isValidElement(icon)) { - iconCloned = React.cloneElement(icon as ReactElement, { - className: twMerge( - 'flex items-center -mr-1 ml-3', - disabled ? 'text-gray-500' : 'text-graphite-700', - (icon.props as BaseProps).className - ), - }) - } + const clonedIcon = cloneIcon(icon, disabled) return ( - {iconCloned} - + {clonedIcon} (function Tag(props, ref) { > {children} - {endAdornment} - - {onDelete && ( - - - - )} + {onDelete && } ) }) diff --git a/packages/base/Tag/src/Tag/__snapshots__/test.tsx.snap b/packages/base/Tag/src/Tag/__snapshots__/test.tsx.snap index e6e23612bb..418312a57c 100644 --- a/packages/base/Tag/src/Tag/__snapshots__/test.tsx.snap +++ b/packages/base/Tag/src/Tag/__snapshots__/test.tsx.snap @@ -133,7 +133,7 @@ exports[`Tag renders with connection and icon 1`] = ` data-testid="tag" >