UNPKG

2.68 kBJavaScriptView Raw
1import _extends from 'babel-runtime/helpers/extends';
2import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
3import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
4import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
5import _inherits from 'babel-runtime/helpers/inherits';
6import classNames from 'classnames';
7import React, { cloneElement } from 'react';
8import elementType from 'prop-types-extra/lib/elementType';
9
10import ListGroupItem from './ListGroupItem';
11import { bsClass, getClassSet, splitBsProps } from './utils/bootstrapUtils';
12import ValidComponentChildren from './utils/ValidComponentChildren';
13
14var propTypes = {
15 /**
16 * You can use a custom element type for this component.
17 *
18 * If not specified, it will be treated as `'li'` if every child is a
19 * non-actionable `<ListGroupItem>`, and `'div'` otherwise.
20 */
21 componentClass: elementType
22};
23
24function getDefaultComponent(children) {
25 if (!children) {
26 // FIXME: This is the old behavior. Is this right?
27 return 'div';
28 }
29
30 if (ValidComponentChildren.some(children, function (child) {
31 return child.type !== ListGroupItem || child.props.href || child.props.onClick;
32 })) {
33 return 'div';
34 }
35
36 return 'ul';
37}
38
39var ListGroup = function (_React$Component) {
40 _inherits(ListGroup, _React$Component);
41
42 function ListGroup() {
43 _classCallCheck(this, ListGroup);
44
45 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
46 }
47
48 ListGroup.prototype.render = function render() {
49 var _props = this.props,
50 children = _props.children,
51 _props$componentClass = _props.componentClass,
52 Component = _props$componentClass === undefined ? getDefaultComponent(children) : _props$componentClass,
53 className = _props.className,
54 props = _objectWithoutProperties(_props, ['children', 'componentClass', 'className']);
55
56 var _splitBsProps = splitBsProps(props),
57 bsProps = _splitBsProps[0],
58 elementProps = _splitBsProps[1];
59
60 var classes = getClassSet(bsProps);
61
62 var useListItem = Component === 'ul' && ValidComponentChildren.every(children, function (child) {
63 return child.type === ListGroupItem;
64 });
65
66 return React.createElement(
67 Component,
68 _extends({}, elementProps, { className: classNames(className, classes) }),
69 useListItem ? ValidComponentChildren.map(children, function (child) {
70 return cloneElement(child, { listItem: true });
71 }) : children
72 );
73 };
74
75 return ListGroup;
76}(React.Component);
77
78ListGroup.propTypes = propTypes;
79
80export default bsClass('list-group', ListGroup);
\No newline at end of file