Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tag): support click handler for filter tag close icon #5470

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

@abbeyhrt abbeyhrt Mar 4, 2020

Choose a reason for hiding this comment

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

For my own edification, why did you pull this out? I see it done a lot throughout our styles and I just wanted to learn why, thanks! 😄

Copy link
Member Author

@emyarod emyarod Mar 4, 2020

Choose a reason for hiding this comment

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

are you referring to the class (.bx--tag--filter to .bx--tag__close-icon)? or the > selector?

}

.#{$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