Skip to content

Commit 0c87128

Browse files
committed
[fixed] ListGroup outputs <ul> or <div> depending on ListGroupItem (defaults to <ul> if no ListGroupItem). ListGroupItem outputs <li> or <a> if href prop is set.
1 parent 9a9d853 commit 0c87128

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

src/ListGroup.js

+28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ class ListGroup extends React.Component {
99
(item, index) => cloneElement(item, { key: item.key ? item.key : index })
1010
);
1111

12+
let child;
13+
14+
if (this.props.children) {
15+
if (Array.isArray(this.props.children)) {
16+
child = this.props.children[0];
17+
} else {
18+
child = this.props.children;
19+
}
20+
}
21+
22+
// If child has an href prop, it is an
23+
// 'anchor' tag and ListGroup should be a Div.
24+
if (child && child.props.href){
25+
return this.renderDiv(items);
26+
} else {
27+
return this.renderUL(items);
28+
}
29+
}
30+
31+
renderUL(items) {
32+
return (
33+
<ul className={classSet(this.props.className, 'list-group')}>
34+
{items}
35+
</ul>
36+
);
37+
}
38+
39+
renderDiv(items) {
1240
return (
1341
<div className={classSet(this.props.className, 'list-group')}>
1442
{items}

src/ListGroupItem.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const ListGroupItem = React.createClass({
1111
active: React.PropTypes.any,
1212
disabled: React.PropTypes.any,
1313
header: React.PropTypes.node,
14+
badge: React.PropTypes.node,
1415
onClick: React.PropTypes.func,
1516
eventKey: React.PropTypes.any,
1617
href: React.PropTypes.string,
@@ -32,15 +33,16 @@ const ListGroupItem = React.createClass({
3233
if (this.props.href || this.props.target || this.props.onClick) {
3334
return this.renderAnchor(classes);
3435
} else {
35-
return this.renderSpan(classes);
36+
return this.renderLi(classes);
3637
}
3738
},
3839

39-
renderSpan(classes) {
40+
renderLi(classes) {
4041
return (
41-
<span {...this.props} className={classSet(this.props.className, classes)}>
42+
<li
43+
{...this.props} className={classSet(this.props.className, classes)}>
4244
{this.props.header ? this.renderStructuredContent() : this.props.children}
43-
</span>
45+
</li>
4446
);
4547
},
4648

test/ListGroupItemSpec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import ListGroupItem from '../src/ListGroupItem';
44

55
describe('ListGroupItem', function () {
66

7-
it('Should output a "span" with the class "list-group-item"', function () {
7+
it('Should output a "li" with the class "list-group-item"', function () {
88
let instance = ReactTestUtils.renderIntoDocument(
99
<ListGroupItem>Text</ListGroupItem>
1010
);
11-
assert.equal(instance.getDOMNode().nodeName, 'SPAN');
11+
assert.equal(instance.getDOMNode().nodeName, 'LI');
1212
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group-item'));
1313
});
1414

15-
it('Should output a "anchor" if "href" prop is set', function () {
15+
it('Should output an "anchor" if "href" prop is set', function () {
1616
let instance = ReactTestUtils.renderIntoDocument(
1717
<ListGroupItem href='#test'>Achor</ListGroupItem>
1818
);
@@ -65,5 +65,4 @@ describe('ListGroupItem', function () {
6565
assert.equal(instance.getDOMNode().lastChild.innerText, 'Item text');
6666
assert.ok(instance.getDOMNode().lastChild.className.match(/\blist-group-item-text\b/));
6767
});
68-
6968
});

test/ListGroupSpec.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import ListGroupItem from '../src/ListGroupItem';
55

66
describe('ListGroup', function () {
77

8-
// TODO: Why div, shouldn't it be a ul?
9-
it('Should output a "div" with the class "list-group"', function () {
8+
it('Should output a "ul" with the class "list-group"', function () {
109
let instance = ReactTestUtils.renderIntoDocument(
11-
<ListGroup />
10+
<ListGroup/>
1211
);
13-
assert.equal(React.findDOMNode(instance).nodeName, 'DIV');
12+
assert.equal(React.findDOMNode(instance).nodeName, 'UL');
1413
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group'));
1514
});
1615

@@ -28,4 +27,28 @@ describe('ListGroup', function () {
2827
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[1], 'list-group-item'));
2928
});
3029

30+
it('Should output a "ul" when children are list items', function () {
31+
let instance = ReactTestUtils.renderIntoDocument(
32+
<ListGroup>
33+
<ListGroupItem>1st Child</ListGroupItem>
34+
<ListGroupItem>2nd Child</ListGroupItem>
35+
</ListGroup>
36+
);
37+
assert.equal(React.findDOMNode(instance).nodeName, 'UL');
38+
assert.equal(React.findDOMNode(instance).firstChild.nodeName, 'LI');
39+
});
40+
41+
42+
it('Should output a "div" when "ListGroupItem" children are anchors', function () {
43+
let instance = ReactTestUtils.renderIntoDocument(
44+
<ListGroup>
45+
<ListGroupItem href="#test">1st Child</ListGroupItem>
46+
<ListGroupItem href="#test">2nd Child</ListGroupItem>
47+
</ListGroup>
48+
);
49+
assert.equal(React.findDOMNode(instance).nodeName, 'DIV');
50+
assert.equal(React.findDOMNode(instance).firstChild.nodeName, 'A');
51+
assert.equal(React.findDOMNode(instance).lastChild.nodeName, 'A');
52+
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group'));
53+
});
3154
});

0 commit comments

Comments
 (0)