Skip to content

Commit

Permalink
feat(Tag): support click handler for filter tag close icon (#5470)
Browse files Browse the repository at this point in the history
  • Loading branch information
emyarod authored Mar 10, 2020
1 parent b8187fa commit 57f48c0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
16 changes: 10 additions & 6 deletions packages/components/src/components/tag/_tag.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@
// tags used for filtering
.#{$prefix}--tag--filter {
@include tag-theme($inverse-02, $inverse-01);

cursor: pointer;
padding-right: rem(2px);

&:focus,
Expand All @@ -108,28 +106,34 @@
}
}

.#{$prefix}--tag--filter > svg {
.#{$prefix}--tag__close-icon {
flex-shrink: 0;
width: rem(20px);
height: rem(20px);
margin: 0 0 0 rem(4px);
padding: rem(2px);
border: 0;
fill: $inverse-01;
background-color: transparent;
border-radius: 50%;
cursor: pointer;

&:hover {
background-color: $inverse-hover-ui;
}
}

.#{$prefix}--tag--filter:focus > svg {
.#{$prefix}--tag__close-icon svg {
fill: $inverse-01;
}

.#{$prefix}--tag__close-icon:focus {
outline: none;
box-shadow: inset 0 0 0 2px $inverse-focus-ui;
border-radius: 50%;
}

.#{$prefix}--tag--filter.#{$prefix}--tag--disabled svg:hover {
.#{$prefix}--tag--filter.#{$prefix}--tag--disabled
.#{$prefix}--tag__close-icon:hover {
background-color: transparent;
}

Expand Down
8 changes: 5 additions & 3 deletions packages/components/src/components/tag/tag.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

<div>
{{#if filter}}
<button class="{{@root.prefix}}--tag {{@root.prefix}}--tag--filter" title="Clear filter">
<div class="{{@root.prefix}}--tag {{@root.prefix}}--tag--filter" title="Clear filter">
<span class="{{@root.prefix}}--tag__label">filter</span>
{{ carbon-icon 'Close16' }}
</button>
<button class="{{@root.prefix}}--tag__close-icon">
{{ carbon-icon 'Close16' }}
</button>
</div>
{{/if}}
</div>
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4724,6 +4724,9 @@ Map {
"filter": Object {
"type": "bool",
},
"onClose": Object {
"type": "func",
},
"title": Object {
"type": "string",
},
Expand Down
6 changes: 5 additions & 1 deletion packages/react/src/components/Tag/Tag-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const props = {
title: 'Clear Filter',
}),
filter() {
return { ...this.regular(), onClick: action('onClick') };
return {
...this.regular(),
onClick: action('onClick'),
onClose: action('onClose'),
};
},
};

Expand Down
27 changes: 23 additions & 4 deletions packages/react/src/components/Tag/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import React from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import { Close16 } from '@carbon/icons-react';
import setupGetInstanceId from '../../tools/setupGetInstanceId';

const { prefix } = settings;

const getInstanceId = setupGetInstanceId();
const TYPES = {
red: 'Red',
magenta: 'Magenta',
Expand All @@ -29,32 +30,45 @@ const TYPES = {
const Tag = ({
children,
className,
id,
type,
filter,
title,
disabled,
onClose,
...other
}) => {
const tagId = id || `tag-${getInstanceId()}`;
const tagClass = `${prefix}--tag--${type}`;
const tagClasses = classNames(`${prefix}--tag`, tagClass, className, {
[`${prefix}--tag--disabled`]: disabled,
[`${prefix}--tag--filter`]: filter,
});
const handleClose = event => {
event.stopPropagation();
onClose(event);
};
return filter ? (
<button
<div
className={tagClasses}
aria-label={
title !== undefined
? `${title} ${children}`
: `Clear filter ${children}`
}
id={tagId}
disabled={disabled}
{...other}>
<span className={`${prefix}--tag__label`}>
{children !== null && children !== undefined ? children : TYPES[type]}
</span>
<Close16 />
</button>
<button
className={`${prefix}--tag__close-icon`}
onClick={handleClose}
aria-labelledby={tagId}>
<Close16 />
</button>
</div>
) : (
<span className={tagClasses} {...other}>
{children !== null && children !== undefined ? children : TYPES[type]}
Expand Down Expand Up @@ -92,6 +106,11 @@ Tag.propTypes = {
* Text to show on clear filters
*/
title: PropTypes.string,

/**
* Click handler for filter tag close button.
*/
onClose: PropTypes.func,
};

export const types = Object.keys(TYPES);
Expand Down

0 comments on commit 57f48c0

Please sign in to comment.