From 9de52cef1cfb1c0289abdd03c4fa7ff35c0811bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pet=C5=99=C3=ADk?= Date: Tue, 20 Jun 2023 16:09:51 +0200 Subject: [PATCH 1/5] feat(Label) add option to make label clickable --- .../react-core/src/components/Label/Label.tsx | 9 +- .../components/Label/__tests__/Label.test.tsx | 21 + .../components/Label/examples/LabelFilled.tsx | 388 +++++++------ .../Label/examples/LabelOutline.tsx | 516 ++++++++++-------- 4 files changed, 538 insertions(+), 396 deletions(-) diff --git a/packages/react-core/src/components/Label/Label.tsx b/packages/react-core/src/components/Label/Label.tsx index 7f15a1a56ac..5bdd1398103 100644 --- a/packages/react-core/src/components/Label/Label.tsx +++ b/packages/react-core/src/components/Label/Label.tsx @@ -59,6 +59,8 @@ export interface LabelProps extends React.HTMLProps { href?: string; /** Flag indicating if the label is an overflow label */ isOverflowLabel?: boolean; + /** On click callback. If present, label will be clickable. */ + onLabelClick?: (event: React.MouseEvent) => void; /** Forwards the label content and className to rendered function. Use this prop for react router support.*/ render?: ({ className, @@ -94,6 +96,7 @@ export const Label: React.FunctionComponent = ({ tooltipPosition, icon, onClose, + onLabelClick, onEditCancel, onEditComplete, closeBtn, @@ -235,7 +238,7 @@ export const Label: React.FunctionComponent = ({ let LabelComponentChildElement = 'span'; if (href) { LabelComponentChildElement = 'a'; - } else if (isEditable) { + } else if (isEditable || onLabelClick) { LabelComponentChildElement = 'button'; } @@ -254,7 +257,9 @@ export const Label: React.FunctionComponent = ({ }; let labelComponentChild = ( - {content} + + {content} + ); if (render) { diff --git a/packages/react-core/src/components/Label/__tests__/Label.test.tsx b/packages/react-core/src/components/Label/__tests__/Label.test.tsx index be3c3605986..293bed344a0 100644 --- a/packages/react-core/src/components/Label/__tests__/Label.test.tsx +++ b/packages/react-core/src/components/Label/__tests__/Label.test.tsx @@ -109,4 +109,25 @@ describe('Label', () => { expect(screen.queryByRole('button', { name: 'Something' })).toBeNull(); expect(asFragment()).toMatchSnapshot(); }); + + test('clickable label', () => { + const fn = jest.fn(); + + render(); + + expect(screen.getByRole(`button`)).toBeVisible(); + }); + + test('clickable label calls passed callback on click', async () => { + const mockCallback = jest.fn(); + const user = userEvent.setup(); + + render(); + + const label = screen.getByRole('button'); + + await user.click(label); + + expect(mockCallback).toBeCalledTimes(1); + }); }); diff --git a/packages/react-core/src/components/Label/examples/LabelFilled.tsx b/packages/react-core/src/components/Label/examples/LabelFilled.tsx index ff0e0cca989..21868abef06 100644 --- a/packages/react-core/src/components/Label/examples/LabelFilled.tsx +++ b/packages/react-core/src/components/Label/examples/LabelFilled.tsx @@ -2,171 +2,223 @@ import React from 'react'; import { Label } from '@patternfly/react-core'; import InfoCircleIcon from '@patternfly/react-icons/dist/esm/icons/info-circle-icon'; -export const LabelFilled: React.FunctionComponent = () => ( - - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
-
- -
-
-); +export const LabelFilled: React.FunctionComponent = () => { + const logColor = (color: string) => { + // eslint-disable-next-line no-console + console.log(color, 'label clicked'); + }; + return ( + + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+
+ +
+
+ ); +}; diff --git a/packages/react-core/src/components/Label/examples/LabelOutline.tsx b/packages/react-core/src/components/Label/examples/LabelOutline.tsx index cfddb5adb4a..5b44bdc4c8b 100644 --- a/packages/react-core/src/components/Label/examples/LabelOutline.tsx +++ b/packages/react-core/src/components/Label/examples/LabelOutline.tsx @@ -2,229 +2,293 @@ import React from 'react'; import { Label } from '@patternfly/react-core'; import InfoCircleIcon from '@patternfly/react-icons/dist/esm/icons/info-circle-icon'; -export const LabelOutline: React.FunctionComponent = () => ( - - {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-
- {' '} - {' '} - {' '} - {' '} - {' '} - - -
-); +export const LabelOutline: React.FunctionComponent = () => { + const logColor = (color: string) => { + // eslint-disable-next-line no-console + console.log(color, 'label clicked'); + }; + return ( + + {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+
+ {' '} + {' '} + {' '} + {' '} + {' '} + + {' '} + + +
+ ); +}; From 6e010151eadd93df11510ca14f04b73c4f668123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pet=C5=99=C3=ADk?= Date: Wed, 21 Jun 2023 09:59:27 +0200 Subject: [PATCH 2/5] add type to label button --- packages/react-core/src/components/Label/Label.tsx | 6 +++++- .../Label/__tests__/__snapshots__/Label.test.tsx.snap | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/react-core/src/components/Label/Label.tsx b/packages/react-core/src/components/Label/Label.tsx index 5bdd1398103..2a554d3c76f 100644 --- a/packages/react-core/src/components/Label/Label.tsx +++ b/packages/react-core/src/components/Label/Label.tsx @@ -257,7 +257,11 @@ export const Label: React.FunctionComponent = ({ }; let labelComponentChild = ( - + {content} ); diff --git a/packages/react-core/src/components/Label/__tests__/__snapshots__/Label.test.tsx.snap b/packages/react-core/src/components/Label/__tests__/__snapshots__/Label.test.tsx.snap index f876111ced5..c8c126cb8d6 100644 --- a/packages/react-core/src/components/Label/__tests__/__snapshots__/Label.test.tsx.snap +++ b/packages/react-core/src/components/Label/__tests__/__snapshots__/Label.test.tsx.snap @@ -7,6 +7,7 @@ exports[`Label editable label 1`] = ` >