react-toolbox/components/list/ListItem.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-10-19 03:38:25 +03:00
import React from 'react';
import ListItemContent from './ListItemContent';
import ListItemLayout from './ListItemLayout';
2015-12-07 04:34:12 +03:00
import Ripple from '../ripple';
2015-10-19 03:38:25 +03:00
import style from './style';
2015-10-22 02:31:17 +03:00
class ListItem extends React.Component {
static propTypes = {
2015-12-07 03:13:59 +03:00
children: React.PropTypes.any,
className: React.PropTypes.string,
2015-10-19 03:38:25 +03:00
disabled: React.PropTypes.bool,
onClick: React.PropTypes.func,
2015-10-19 03:38:25 +03:00
ripple: React.PropTypes.bool,
to: React.PropTypes.string
};
2015-10-19 03:38:25 +03:00
static defaultProps = {
disabled: false,
ripple: false
};
2015-10-19 03:38:25 +03:00
2015-10-22 02:31:17 +03:00
handleClick = (event) => {
2015-10-19 03:38:25 +03:00
if (this.props.onClick && !this.props.disabled) {
this.props.onClick(event);
}
2015-10-22 02:31:17 +03:00
};
2015-10-19 03:38:25 +03:00
groupChildren () {
const children = {
leftActions: [],
rightActions: [],
ignored: []
};
2015-10-19 03:38:25 +03:00
React.Children.forEach(this.props.children, (child, i) => {
if (!React.isValidElement(child)) {
return;
}
if (child.props.listItemIgnore) {
children.ignored.push(child);
return;
}
if (child.type === ListItemContent) {
children.itemContent = child;
return;
}
const bucket = children.itemContent ? 'rightActions' : 'leftActions';
children[bucket].push({...child, key: i});
});
return children;
}
render () {
2016-04-10 12:39:04 +03:00
const {className, onMouseDown, to, onClick, ripple, ...other} = this.props; //eslint-disable-line no-unused-vars
const children = this.groupChildren();
const content = <ListItemLayout {...children} {...other}/>;
2016-04-10 12:39:04 +03:00
let finalClassName = style.listItem;
if (className) finalClassName += ` ${className}`;
return (
2016-04-10 12:39:04 +03:00
<li className={finalClassName} onClick={this.handleClick} onMouseDown={onMouseDown}>
{to ? <a href={this.props.to}>{content}</a> : content}
{children.ignored}
2015-10-19 03:38:25 +03:00
</li>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
2015-12-07 04:34:12 +03:00
export default Ripple({
2015-12-07 03:13:59 +03:00
className: style.ripple,
centered: false,
listItemIgnore: true
2015-12-07 03:13:59 +03:00
})(ListItem);
2016-01-22 16:12:53 +03:00
export {ListItem as RawListItem};