2016-05-29 22:11:04 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-05-22 20:08:47 +03:00
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-29 21:59:54 +03:00
|
|
|
import { LIST } from '../identifiers.js';
|
|
|
|
import InjectListItemContent from './ListItemContent.js';
|
|
|
|
import InjectListItemLayout from './ListItemLayout.js';
|
|
|
|
import rippleFactory from '../ripple/Ripple.js';
|
2015-10-19 03:38:25 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
const factory = (ripple, ListItemLayout, ListItemContent) => {
|
2016-05-29 22:11:04 +03:00
|
|
|
class ListItem extends Component {
|
2016-05-29 21:59:54 +03:00
|
|
|
static propTypes = {
|
2016-05-29 22:11:04 +03:00
|
|
|
children: PropTypes.any,
|
|
|
|
className: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
ripple: PropTypes.bool,
|
|
|
|
theme: PropTypes.shape({
|
2016-06-04 00:44:33 +03:00
|
|
|
listItem: PropTypes.string
|
2016-05-29 21:59:54 +03:00
|
|
|
}),
|
2016-05-29 22:11:04 +03:00
|
|
|
to: PropTypes.string
|
2016-05-29 21:59:54 +03:00
|
|
|
};
|
2015-10-19 03:38:25 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
|
|
|
disabled: false,
|
|
|
|
ripple: false
|
2016-01-26 16:59:57 +03:00
|
|
|
};
|
2015-10-19 03:38:25 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
handleClick = (event) => {
|
|
|
|
if (this.props.onClick && !this.props.disabled) {
|
|
|
|
this.props.onClick(event);
|
2016-01-26 16:59:57 +03:00
|
|
|
}
|
2016-05-29 21:59:54 +03:00
|
|
|
};
|
2016-01-26 16:59:57 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
groupChildren () {
|
|
|
|
const children = {
|
|
|
|
leftActions: [],
|
|
|
|
rightActions: [],
|
|
|
|
ignored: []
|
|
|
|
};
|
|
|
|
|
|
|
|
React.Children.forEach(this.props.children, (child, i) => {
|
|
|
|
if (!React.isValidElement(child)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-04 23:03:57 +03:00
|
|
|
|
|
|
|
const { listItemIgnore, ...rest } = child.props;
|
|
|
|
const strippedChild = { ...child, ...{ props: rest } };
|
|
|
|
|
|
|
|
if (listItemIgnore) {
|
|
|
|
children.ignored.push(strippedChild);
|
2016-05-29 21:59:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (child.type === ListItemContent) {
|
2016-07-04 23:03:57 +03:00
|
|
|
children.itemContent = strippedChild;
|
2016-05-29 21:59:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const bucket = children.itemContent ? 'rightActions' : 'leftActions';
|
2016-07-04 23:03:57 +03:00
|
|
|
children[bucket].push({...strippedChild, key: i});
|
2016-05-29 21:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
2015-10-29 03:04:59 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
render () {
|
2016-08-23 18:06:45 +03:00
|
|
|
const {className, onMouseDown, onTouchStart, to, onClick, ripple: hasRipple, theme, ...other} = this.props; //eslint-disable-line no-unused-vars
|
2016-05-29 21:59:54 +03:00
|
|
|
const children = this.groupChildren();
|
2016-07-07 20:39:02 +03:00
|
|
|
const content = <ListItemLayout theme={theme} {...children} {...other}/>;
|
2016-05-29 21:59:54 +03:00
|
|
|
return (
|
2016-08-23 18:06:45 +03:00
|
|
|
<li className={`${theme.listItem} ${className}`} onClick={this.handleClick} onMouseDown={onMouseDown} onTouchStart={onTouchStart}>
|
2016-05-29 21:59:54 +03:00
|
|
|
{to ? <a href={this.props.to}>{content}</a> : content}
|
|
|
|
{children.ignored}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
2015-10-19 03:38:25 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
return ripple(ListItem);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ripple = rippleFactory({ centered: false, listItemIgnore: true });
|
|
|
|
const ListItem = factory(ripple, InjectListItemLayout, InjectListItemContent);
|
2016-05-22 20:08:47 +03:00
|
|
|
|
2016-05-29 21:59:54 +03:00
|
|
|
export default themr(LIST)(ListItem);
|
|
|
|
export { factory as listItemFactory };
|
|
|
|
export { ListItem };
|