Skip to content

Commit

Permalink
Merge pull request react-bootstrap#558 from cdock1029/master
Browse files Browse the repository at this point in the history
[fixed] ListGroup work with single child contained in array
  • Loading branch information
mtscout6 committed Apr 26, 2015
2 parents 5baa816 + 22da8f9 commit 22e0da1
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 22e0da1

Please sign in to comment.