Skip to content

Commit

Permalink
fix(Tag): Mark tag as interactive when onClick is set (#7073)
Browse files Browse the repository at this point in the history
  • Loading branch information
jscheiny authored Nov 15, 2024
1 parent fbfcaca commit 1e6aef2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/core/src/components/tag/tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export interface TagProps
*/
htmlTitle?: string;

/**
* Whether the tag should visually respond to user interactions. If set to `true`, hovering over the
* tag will change its color and mouse cursor.
*
* Tags will be marked as interactive automatically if an onClick handler is provided and this prop is not.
*
* @default false
*/
interactive?: boolean;

/**
* Name of a Blueprint UI icon (or an icon element) to render on the left side of the tag,
* before the child nodes.
Expand Down Expand Up @@ -86,7 +96,7 @@ export const Tag: React.FC<TagProps> = React.forwardRef((props, ref) => {
fill,
icon,
intent,
interactive = false,
interactive,
large,
minimal,
multiline,
Expand All @@ -99,8 +109,9 @@ export const Tag: React.FC<TagProps> = React.forwardRef((props, ref) => {
} = props;

const isRemovable = Utils.isFunction(onRemove);
const isInteractive = interactive ?? htmlProps.onClick != null;

const [active, interactiveProps] = useInteractiveAttributes(interactive, props, ref, {
const [active, interactiveProps] = useInteractiveAttributes(isInteractive, props, ref, {
defaultTabIndex: 0,
disabledTabIndex: undefined,
});
Expand All @@ -111,7 +122,7 @@ export const Tag: React.FC<TagProps> = React.forwardRef((props, ref) => {
{
[Classes.ACTIVE]: active,
[Classes.FILL]: fill,
[Classes.INTERACTIVE]: interactive,
[Classes.INTERACTIVE]: isInteractive,
[Classes.LARGE]: large,
[Classes.MINIMAL]: minimal,
[Classes.ROUND]: round,
Expand All @@ -135,7 +146,6 @@ export const Tag: React.FC<TagProps> = React.forwardRef((props, ref) => {
Tag.defaultProps = {
active: false,
fill: false,
interactive: false,
large: false,
minimal: false,
round: false,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/test/tag/tagTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ describe("<Tag>", () => {
assert.isTrue(handleRemove.calledOnce);
});

it("should be interactive when onClick is provided", () => {
const wrapper = mount(<Tag onClick={spy()}>Hello</Tag>);
assert.lengthOf(wrapper.find(`.${Classes.INTERACTIVE}`), 1);
});

it("should not be interactive when interactive={false}", () => {
const wrapper = mount(
<Tag onClick={spy()} interactive={false}>
Hello
</Tag>,
);
assert.lengthOf(wrapper.find(`.${Classes.INTERACTIVE}`), 0);
});

it(`passes other props onto .${Classes.TAG} element`, () => {
const element = shallow(<Tag title="baz qux">Hello</Tag>).find("." + Classes.TAG);
assert.deepEqual(element.prop("title"), "baz qux");
Expand Down

1 comment on commit 1e6aef2

@svc-palantir-github
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix(Tag): Mark tag as interactive when onClick is set (#7073)

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

Please sign in to comment.