react-toolbox/components/list/List.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-05-29 21:59:54 +03:00
import React, { Component, PropTypes } from 'react';
2016-05-22 20:08:47 +03:00
import classnames from 'classnames';
import { themr } from 'react-css-themr';
2017-01-26 20:05:32 +03:00
import { LIST } from '../identifiers';
import InjectListItem from './ListItem';
2015-10-19 03:38:25 +03:00
2016-11-18 21:15:13 +03:00
const mergeProp = (propName, child, parent) => (
child[propName] !== undefined
? child[propName]
: parent[propName]
);
2016-05-29 21:59:54 +03:00
const factory = (ListItem) => {
class List extends Component {
static propTypes = {
children: PropTypes.node,
className: PropTypes.string,
theme: PropTypes.shape({
2017-01-26 20:05:32 +03:00
list: 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: '',
ripple: false,
2017-01-26 20:05:32 +03:00
selectable: false,
2016-05-29 21:59:54 +03:00
};
2015-10-19 03:38:25 +03:00
2017-01-26 20:05:32 +03:00
renderItems() {
2016-05-29 21:59:54 +03:00
return React.Children.map(this.props.children, (item) => {
if (item === null || item === undefined) {
return item;
} else if (item.type === ListItem) {
2016-11-18 21:15:13 +03:00
const selectable = mergeProp('selectable', item.props, this.props);
const ripple = mergeProp('ripple', item.props, this.props);
return React.cloneElement(item, { selectable, ripple });
2016-05-29 21:59:54 +03:00
}
2017-01-26 20:05:32 +03:00
return React.cloneElement(item);
2016-05-29 21:59:54 +03:00
});
}
2015-10-19 03:38:25 +03:00
2017-01-26 20:05:32 +03:00
render() {
2016-05-29 21:59:54 +03:00
return (
2017-01-26 20:05:32 +03:00
<ul data-react-toolbox="list" className={classnames(this.props.theme.list, this.props.className)}>
2016-05-29 21:59:54 +03:00
{this.renderItems()}
</ul>
);
}
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 List;
};
const List = factory(InjectListItem);
export default themr(LIST)(List);
export { factory as listFactory };
export { List };