react-toolbox/components/list/ListItemContent.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-10-19 03:38:25 +03:00
import React from 'react';
2016-04-10 12:39:04 +03:00
import classnames from 'classnames';
2016-05-22 20:08:47 +03:00
import { themr } from 'react-css-themr';
import ListItemText from './ListItemText';
2015-10-19 03:38:25 +03:00
const types = ['auto', 'normal', 'large'];
2015-10-19 03:38:25 +03:00
class ListItemContent extends React.Component {
static propTypes = {
caption: React.PropTypes.string,
children: React.PropTypes.any,
legend: React.PropTypes.string,
2016-05-22 20:08:47 +03:00
theme: React.PropTypes.shape({
itemContentRoot: React.PropTypes.string.isRequired,
large: React.PropTypes.string.isRequired
}),
type: React.PropTypes.oneOf(types)
};
2015-10-19 03:38:25 +03:00
getType () {
const {type, children, caption, legend} = this.props;
let count = React.Children.count(children);
2016-04-09 21:34:34 +03:00
[caption, legend].forEach(s => { count += s ? 1 : 0; });
const typeIndex = Math.min(count, types.length);
return type || types[typeIndex];
}
render () {
2016-05-22 20:08:47 +03:00
const {children, caption, legend, theme} = this.props;
const className = classnames(theme.itemContentRoot, {
[theme[this.getType()]]: theme[this.getType()]
2016-04-10 12:39:04 +03:00
});
return (
<span className={className}>
2016-04-10 12:39:04 +03:00
{caption && <ListItemText primary>{caption}</ListItemText>}
{legend && <ListItemText>{legend}</ListItemText>}
{children}
</span>
);
}
}
2016-05-22 20:08:47 +03:00
export default themr('ToolboxList')(ListItemContent);