Skip to content

Commit 9b32cee

Browse files
committed
feat(Badge): handle links
Updated tests Updated docs
1 parent fd59d37 commit 9b32cee

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

docs/lib/Components/BadgePage.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const BadgePillsExampleSource = require('!!raw!../examples/BadgePills');
1515
import BadgeVariationsExample from '../examples/BadgeVariations';
1616
const BadgeVariationsExampleSource = require('!!raw!../examples/BadgeVariations');
1717

18+
import BadgeLinksExample from '../examples/BadgeLinks';
19+
const BadgeLinksExampleSource = require('!!raw!../examples/BadgeLinks');
20+
1821
export default class BadgesPage extends React.Component {
1922
render() {
2023
return (
@@ -58,6 +61,16 @@ export default class BadgesPage extends React.Component {
5861
{BadgePillsExampleSource}
5962
</PrismCode>
6063
</pre>
64+
<h3>Links</h3>
65+
<p>Adding the <code>href</code> prop (without specifying a <code>tag</code> prop) will default the badge to a link.</p>
66+
<div className="docs-example">
67+
<BadgeLinksExample />
68+
</div>
69+
<pre>
70+
<PrismCode className="language-jsx">
71+
{BadgeLinksExampleSource}
72+
</PrismCode>
73+
</pre>
6174
</div>
6275
);
6376
}

docs/lib/examples/BadgeLinks.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { Badge } from 'reactstrap';
3+
4+
export default class Example extends React.Component {
5+
render() {
6+
return (
7+
<div>
8+
<Badge href="#" color="primary">Primary</Badge>
9+
<Badge href="#" color="secondary">Secondary</Badge>
10+
<Badge href="#" color="success">Success</Badge>
11+
<Badge href="#" color="danger">Danger</Badge>
12+
<Badge href="#" color="warning">Warning</Badge>
13+
<Badge href="#" color="info">Info</Badge>
14+
<Badge href="#" color="light">Light</Badge>
15+
<Badge href="#" color="dark">Dark</Badge>
16+
</div>
17+
);
18+
}
19+
}

src/Badge.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const propTypes = {
1313
};
1414

1515
const defaultProps = {
16-
color: 'default',
16+
color: 'secondary',
1717
pill: false,
1818
tag: 'span'
1919
};
2020

2121
const Badge = (props) => {
22-
const {
22+
let {
2323
className,
2424
cssModule,
2525
color,
@@ -35,6 +35,10 @@ const Badge = (props) => {
3535
pill ? 'badge-pill' : false
3636
), cssModule);
3737

38+
if (attributes.href && Tag === 'span') {
39+
Tag = 'a';
40+
}
41+
3842
return (
3943
<Tag {...attributes} className={classes} />
4044
);

src/__tests__/Badge.spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@ import { shallow } from 'enzyme';
33
import { Badge } from '../';
44

55
describe('Badge', () => {
6+
it('should render a span by default', () => {
7+
const wrapper = shallow(<Badge>Yo!</Badge>);
8+
9+
expect(wrapper.type()).toBe('span');
10+
});
11+
12+
it('should render an anchor when when href is provided', () => {
13+
const wrapper = shallow(<Badge href="#">Yo!</Badge>);
14+
15+
expect(wrapper.type()).toBe('a');
16+
});
17+
18+
it('should render a custom tag when provided', () => {
19+
const wrapper = shallow(<Badge tag="main">Yo!</Badge>);
20+
21+
expect(wrapper.type()).toBe('main');
22+
});
23+
624
it('should render children', () => {
725
const wrapper = shallow(<Badge>Yo!</Badge>);
826

927
expect(wrapper.text()).toBe('Yo!');
1028
});
1129

12-
it('should render badges with default color', () => {
30+
it('should render badges with secondary color', () => {
1331
const wrapper = shallow(<Badge>Default Badge</Badge>);
1432

15-
expect(wrapper.hasClass('badge-default')).toBe(true);
33+
expect(wrapper.hasClass('badge-secondary')).toBe(true);
1634
});
1735

1836
it('should render Badges with other colors', () => {

0 commit comments

Comments
 (0)