forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListGroup.js
More file actions
71 lines (59 loc) · 1.74 KB
/
ListGroup.js
File metadata and controls
71 lines (59 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import classNames from 'classnames';
import React, { cloneElement } from 'react';
import elementType from 'react-prop-types/lib/elementType';
import ListGroupItem from './ListGroupItem';
import { bsClass, getClassSet, splitBsProps } from './utils/bootstrapUtils';
import ValidComponentChildren from './utils/ValidComponentChildren';
const propTypes = {
/**
* You can use a custom element type for this component.
*
* If not specified, it will be treated as `'li'` if every child is a
* non-actionable `<ListGroupItem>`, and `'div'` otherwise.
*/
componentClass: elementType,
};
function getDefaultComponent(children) {
if (!children) {
// FIXME: This is the old behavior. Is this right?
return 'div';
}
if (ValidComponentChildren.some(children, child => (
child.type !== ListGroupItem || child.props.href || child.props.onClick
))) {
return 'div';
}
return 'ul';
}
class ListGroup extends React.Component {
render() {
const {
children,
componentClass: Component = getDefaultComponent(children),
className,
...props
} = this.props;
const [bsProps, elementProps] = splitBsProps(props);
const classes = getClassSet(bsProps);
const useListItem =
Component === 'ul' &&
ValidComponentChildren.every(children, child => (
child.type === ListGroupItem
));
return (
<Component
{...elementProps}
className={classNames(className, classes)}
>
{useListItem ?
ValidComponentChildren.map(children, child => (
cloneElement(child, { listItem: true })
)) :
children
}
</Component>
);
}
}
ListGroup.propTypes = propTypes;
export default bsClass('list-group', ListGroup);