Skip to content

Commit

Permalink
[fixed] ListGroup children array bugs. Fixes react-bootstrap#548
Browse files Browse the repository at this point in the history
  • Loading branch information
cdock1029 committed Apr 24, 2015
1 parent e75fea5 commit 22da8f9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ListGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ class ListGroup extends React.Component {

if (!this.props.children) {
return this.renderDiv(items);
} else if (React.Children.count(this.props.children) === 1) {
} else if (React.Children.count(this.props.children) === 1 && !Array.isArray(this.props.children)) {
let child = this.props.children;

childrenAnchors = child.props.href ? true : false;

} else {

childrenAnchors = Array.prototype.some.call(this.props.children, (child) => {
return child.props.href;
return !Array.isArray(child) ? child.props.href : Array.prototype.some.call(child, (subChild) => {
return subChild.props.href;
});
});

}
Expand Down
33 changes: 33 additions & 0 deletions test/ListGroupSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ describe('ListGroup', function () {
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[0], 'list-group-item'));
});

it('Should support a single "ListGroupItem" child contained in an array', function () {
let child = [<ListGroupItem key={42}>Only Child in array</ListGroupItem>];
let instance = ReactTestUtils.renderIntoDocument(
<ListGroup>
{child}
</ListGroup>
);

let items = ReactTestUtils.scryRenderedComponentsWithType(instance, ListGroupItem);

assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[0], 'list-group-item'));
});

it('Should output a "ul" when single "ListGroupItem" child is a list item', function () {
let instance = ReactTestUtils.renderIntoDocument(
<ListGroup>
Expand Down Expand Up @@ -61,6 +74,26 @@ describe('ListGroup', function () {
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[1], 'list-group-item'));
});

it('Should support multiple "ListGroupItem" children including a subset contained in an array', function () {
let itemArray = [
<ListGroupItem key={0}>2nd Child nested</ListGroupItem>,
<ListGroupItem key={1}>3rd Child nested</ListGroupItem>
];

let instance = ReactTestUtils.renderIntoDocument(
<ListGroup>
<ListGroupItem>1st Child</ListGroupItem>
{itemArray}
<ListGroupItem>4th Child</ListGroupItem>
</ListGroup>
);

let items = ReactTestUtils.scryRenderedComponentsWithType(instance, ListGroupItem);

assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[0], 'list-group-item'));
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(items[1], 'list-group-item'));
});

it('Should output a "ul" when children are list items', function () {
let instance = ReactTestUtils.renderIntoDocument(
<ListGroup>
Expand Down

0 comments on commit 22da8f9

Please sign in to comment.