UNPKG

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