Skip to content

Adding Tag Component #225

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

Merged
merged 4 commits into from
Mar 5, 2020
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
2 changes: 2 additions & 0 deletions packages/react-components/source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Breadcrumb from './react/library/breadcrumb';
import ConfirmationModal from './react/library/confirmation-modal';
import Code from './react/library/code';
import RadioButton from './react/library/radiobutton';
import Tag from './react/library/tag';

export {
ActionSelect,
Expand Down Expand Up @@ -59,4 +60,5 @@ export {
ConfirmationModal,
Code,
RadioButton,
Tag,
};
36 changes: 36 additions & 0 deletions packages/react-components/source/react/library/tag/Tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { func, string } from 'prop-types';
import Button from '../button';
import Text from '../text';

const propTypes = {
/** Tag text or other content */
label: string.isRequired,
/** Callback function called when clode icon is clicked */
onClick: func,
};

const defaultProps = {
onClick: () => {},
};

const Tag = ({ label, onClick }) => {
return (
<div className="rc-tag">
<div className="rc-tag-label-background">
<Text className="rc-tag-text">{label}</Text>
</div>
<Button
className="rc-tag-remove-button"
onClick={() => onClick()}
icon="close"
aria-label={`${label} Remove tag`}
/>
</div>
);
};

Tag.propTypes = propTypes;
Tag.defaultProps = defaultProps;

export default Tag;
24 changes: 24 additions & 0 deletions packages/react-components/source/react/library/tag/Tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Overview

The `Tag` component was designed and developed to be used primarily alongside the data grid filtering feature, as a clear indication of what filters are actively applied. However the component itself may have further usecases. The `Tag` component has been built on top of the `Button` component in order to make sure it inherit's all its accessability features.

## Basic Use

All text can be passed to the `Tag` as a child, a callback function can be passed to the onClick prop to catch a users interactions.

```jsx
const onTagClick = () => {
console.log('The X was clicked');
};

<div>
<div>
<Tag label="Tag label" onClick={onTagClick}/>
</div>
</div>;
```

## Related

- [Badge](#/React%20Components/Badge)
- [Button](#/React%20Components/Button)
3 changes: 3 additions & 0 deletions packages/react-components/source/react/library/tag/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Tag from './Tag';

export default Tag;
24 changes: 24 additions & 0 deletions packages/react-components/source/scss/library/components/_tag.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.rc-tag {
background: $puppet-b500;
border-radius: $puppet-common-border-radius;
display: flex;
width: max-content;

.rc-icon {
height: 12px !important;
width: 12px !important;
}
}

.rc-tag-label-background {
align-items: center;
display: flex;

.rc-tag-text {
@include puppet-type-button();
color: $puppet-white;
height: fit-content;
padding-left: 15px;
padding-right: 4px;
}
}
1 change: 1 addition & 0 deletions packages/react-components/source/scss/library/ui.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
@import 'components/breadcrumb';
@import 'components/code';
@import 'components/radiobutton';
@import 'components/tag';
32 changes: 32 additions & 0 deletions packages/react-components/test/tag/tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import jsdom from 'mocha-jsdom';
import { shallow, mount } from 'enzyme';
import { expect } from 'chai';
import sinon from 'sinon';
import React from 'react';

import Tag from '../../source/react/library/tag/Tag';

describe('<Tag />', () => {
jsdom({ skipWindowCheck: true });

it('should render without blowing up', () => {
shallow(<Tag label="Text" />);
});

it('should display the correct label text', () => {
expect(
shallow(<Tag label="Text" />)
.find('Text.rc-tag-text')
.text(),
).to.have.string('Text');
});

it('should call onClick function when close button is clicked', () => {
const onClick = sinon.spy();
const wrapper2 = mount(<Tag label="Text" onClick={onClick} />);

wrapper2.find('button').simulate('click');

expect(onClick.called).to.equal(true);
});
});