From 08d09d3bb4fc1925b14b82db8a9de103c6a3d747 Mon Sep 17 00:00:00 2001 From: ariellalgilmore Date: Thu, 14 Nov 2024 11:25:02 -0800 Subject: [PATCH 1/5] feat(tag): new decorator prop --- .../__snapshots__/PublicAPI-test.js.snap | 7 +- packages/react/src/components/Tag/Tag-test.js | 12 +++- .../react/src/components/Tag/Tag.stories.js | 65 +++++++++++++++++-- packages/react/src/components/Tag/Tag.tsx | 59 +++++++++++++---- packages/styles/scss/components/tag/_tag.scss | 11 +++- 5 files changed, 133 insertions(+), 21 deletions(-) diff --git a/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap b/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap index b02fb2f6af77..9c22f56f7a4c 100644 --- a/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap +++ b/packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap @@ -8430,6 +8430,9 @@ Map { "className": Object { "type": "string", }, + "decorator": Object { + "type": "node", + }, "disabled": Object { "type": "bool", }, @@ -8461,9 +8464,7 @@ Map { ], "type": "oneOf", }, - "slug": Object { - "type": "node", - }, + "slug": [Function], "title": [Function], "type": Object { "args": Array [ diff --git a/packages/react/src/components/Tag/Tag-test.js b/packages/react/src/components/Tag/Tag-test.js index 36ee644e9281..efca3383745f 100644 --- a/packages/react/src/components/Tag/Tag-test.js +++ b/packages/react/src/components/Tag/Tag-test.js @@ -90,12 +90,22 @@ describe('Tag', () => { expect(screen.getByTestId('test')).toBeInTheDocument(); }); - it('should respect slug prop', () => { + it('should respect decorator prop', () => { + render(} />); + + expect( + screen.getByRole('button', { name: 'AI - Show information' }) + ).toBeInTheDocument(); + }); + + it('should respect deprecated slug prop', () => { + const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}); render(} />); expect( screen.getByRole('button', { name: 'AI - Show information' }) ).toBeInTheDocument(); + spy.mockRestore(); }); describe('Selectable Tag', () => { diff --git a/packages/react/src/components/Tag/Tag.stories.js b/packages/react/src/components/Tag/Tag.stories.js index ddef8f4f2902..d14e4eb9286b 100644 --- a/packages/react/src/components/Tag/Tag.stories.js +++ b/packages/react/src/components/Tag/Tag.stories.js @@ -13,6 +13,8 @@ import Button from '../Button'; import { AILabel, AILabelContent, AILabelActions } from '../AILabel'; import { IconButton } from '../IconButton'; import '../AILabel/ailabel-story.scss'; +import { Tooltip } from '../Tooltip'; +import { Information } from '@carbon/icons-react'; import mdx from './Tag.mdx'; export default { @@ -217,13 +219,60 @@ const aiLabel = ( export const withAILabel = () => (
- + + {'Tag'} + + + + {'Tag'} + + + + {'Tag'} + + + + {'Tag'} + + + + + } + className="some-class" + type="red" + title="Clear Filter"> {'Tag'} + + + } className="some-class" type="purple" title="Clear Filter"> @@ -232,7 +281,11 @@ export const withAILabel = () => ( + + + } className="some-class" type="blue" title="Clear Filter"> @@ -242,7 +295,11 @@ export const withAILabel = () => ( + + + } className="some-class" type="green" title="Clear Filter"> diff --git a/packages/react/src/components/Tag/Tag.tsx b/packages/react/src/components/Tag/Tag.tsx index 115fade0449b..09f5ad6fe3cd 100644 --- a/packages/react/src/components/Tag/Tag.tsx +++ b/packages/react/src/components/Tag/Tag.tsx @@ -56,6 +56,11 @@ export interface TagBaseProps { */ className?: string; + /** + * **Experimental:** Provide a `decorator` component to be rendered inside the `Tag` component + */ + decorator?: ReactNode; + /** * Specify if the `Tag` is disabled */ @@ -89,6 +94,7 @@ export interface TagBaseProps { size?: keyof typeof SIZES; /** + * @deprecated please use `decorator` instead. * **Experimental:** Provide a `Slug` component to be rendered inside the `Tag` component */ slug?: ReactNode; @@ -113,6 +119,7 @@ const Tag = React.forwardRef(function Tag( { children, className, + decorator, id, type, filter, // remove filter in next major release - V12 @@ -168,13 +175,22 @@ const Tag = React.forwardRef(function Tag( } }; - // Slug is always size `md` and `inline` - let normalizedSlug; - if (slug && slug['type']?.displayName === 'AILabel' && !isInteractiveTag) { - normalizedSlug = React.cloneElement(slug as React.ReactElement, { - size: 'sm', - kind: 'inline', - }); + // AILabel is always size `sm` and `inline` + let normalizedDecorator = React.isValidElement(slug ?? decorator) + ? slug ?? decorator + : null; + if ( + normalizedDecorator && + normalizedDecorator['type']?.displayName === 'AILabel' && + !isInteractiveTag + ) { + normalizedDecorator = React.cloneElement( + normalizedDecorator as React.ReactElement, + { + size: 'sm', + kind: 'inline', + } + ); } if (filter) { @@ -194,7 +210,15 @@ const Tag = React.forwardRef(function Tag( className={`${prefix}--tag__label`}> {children !== null && children !== undefined ? children : typeText} - {normalizedSlug} + {slug ? ( + normalizedDecorator + ) : decorator ? ( +
+ {normalizedDecorator} +
+ ) : ( + '' + )}
); From b0411744bf4f2a5cb28d999131379a2415da7f76 Mon Sep 17 00:00:00 2001 From: ariellalgilmore Date: Mon, 18 Nov 2024 08:30:36 -0800 Subject: [PATCH 3/5] fix(tag): format and remove filter updates only add to dissmissible --- packages/react/src/components/Tag/Tag.tsx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/react/src/components/Tag/Tag.tsx b/packages/react/src/components/Tag/Tag.tsx index 09f5ad6fe3cd..30c8e5aa59de 100644 --- a/packages/react/src/components/Tag/Tag.tsx +++ b/packages/react/src/components/Tag/Tag.tsx @@ -210,15 +210,7 @@ const Tag = React.forwardRef(function Tag( className={`${prefix}--tag__label`}> {children !== null && children !== undefined ? children : typeText} - {slug ? ( - normalizedDecorator - ) : decorator ? ( -
- {normalizedDecorator} -
- ) : ( - '' - )} + {normalizedDecorator}