Skip to content

Commit

Permalink
[added] react-bootstrap#460 ListGroupItem outputs <button> when an on…
Browse files Browse the repository at this point in the history
…Click handler is set.
  • Loading branch information
apkiernan committed Aug 17, 2015
1 parent 537f52c commit 459ab0c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
8 changes: 7 additions & 1 deletion docs/examples/ListGroupLinked.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
function alertClicked () {
alert('You clicked the third ListGroupItem');
}

const listgroupInstance = (
<ListGroup>
<ListGroupItem href='#link1'>Link 1</ListGroupItem>
<ListGroupItem href='#link2'>Link 2</ListGroupItem>
<ListGroupItem href='#linkN'>...</ListGroupItem>
<ListGroupItem onClick={alertClicked}>
Trigger an alert
</ListGroupItem>
</ListGroup>
);

Expand Down
14 changes: 6 additions & 8 deletions src/ListGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,26 @@ class ListGroup extends React.Component {
(item, index) => cloneElement(item, { key: item.key ? item.key : index })
);

let childrenAnchors = false;
let shouldRenderDiv = false;

if (!this.props.children) {
return this.renderDiv(items);
shouldRenderDiv = true;
} else {
React.Children.forEach(this.props.children, (child) => {
if (this.isAnchor(child.props)) {
childrenAnchors = true;
if (this.isAnchorOrButton(child.props)) {
shouldRenderDiv = true;
}

});

}

if (childrenAnchors){
if (shouldRenderDiv){
return this.renderDiv(items);
} else {
return this.renderUL(items);
}
}

isAnchor(props){
isAnchorOrButton(props){
return (props.href || props.onClick);
}

Expand Down
20 changes: 16 additions & 4 deletions src/ListGroupItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { cloneElement } from 'react';
import BootstrapMixin from './BootstrapMixin';
import classNames from 'classnames';
import SafeAnchor from './SafeAnchor';

const ListGroupItem = React.createClass({
mixins: [BootstrapMixin],
Expand Down Expand Up @@ -32,8 +31,10 @@ const ListGroupItem = React.createClass({
classes.active = this.props.active;
classes.disabled = this.props.disabled;

if (this.props.href || this.props.onClick) {
if (this.props.href) {
return this.renderAnchor(classes);
} else if (this.props.onClick) {
return this.renderButton(classes);
} else if (this.props.listItem) {
return this.renderLi(classes);
} else {
Expand All @@ -52,12 +53,23 @@ const ListGroupItem = React.createClass({

renderAnchor(classes) {
return (
<SafeAnchor
<a
{...this.props}
className={classNames(this.props.className, classes)}
>
{this.props.header ? this.renderStructuredContent() : this.props.children}
</SafeAnchor>
</a>
);
},

renderButton(classes) {
return (
<button
type='button'
{...this.props}
className={classNames(this.props.className, classes)}>
{this.props.children}
</button>
);
},

Expand Down
9 changes: 9 additions & 0 deletions test/ListGroupItemSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ describe('ListGroupItem', function () {
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group-item'));
});

it('Should output a "button" if an "onClick" handler is set', function () {
let noop = function () {};
let instance = ReactTestUtils.renderIntoDocument(
<ListGroupItem onClick={noop}>Button</ListGroupItem>
);
assert.equal(React.findDOMNode(instance).nodeName, 'BUTTON');
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group-item'));
});

it('Should output an "li" if "listItem" prop is set', function () {
let instance = ReactTestUtils.renderIntoDocument(
<ListGroupItem listItem>Item 1</ListGroupItem>
Expand Down
2 changes: 1 addition & 1 deletion test/ListGroupSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('ListGroup', function () {
</ListGroup>
);
assert.equal(React.findDOMNode(instance).nodeName, 'DIV');
assert.equal(React.findDOMNode(instance).firstChild.nodeName, 'A');
assert.equal(React.findDOMNode(instance).firstChild.nodeName, 'BUTTON');
assert.equal(React.findDOMNode(instance).lastChild.nodeName, 'SPAN');
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'list-group'));
});
Expand Down

0 comments on commit 459ab0c

Please sign in to comment.