forked from reactstrap/reactstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccordionBody.js
65 lines (57 loc) · 1.56 KB
/
AccordionBody.js
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
import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { mapToCssModules, tagPropType } from './utils';
import Collapse from './Collapse';
import { AccordionContext } from './AccordionContext';
const propTypes = {
/** Unique key used to control item's collapse/expand */
accordionId: PropTypes.string.isRequired,
/** To add custom class */
className: PropTypes.string,
children: PropTypes.node,
/** Change existing base class name with a new class name */
cssModule: PropTypes.object,
/** Pass ref to the component */
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.func,
]),
/** Set a custom element for this component */
tag: tagPropType,
};
const defaultProps = {
tag: 'div',
};
function AccordionBody(props) {
const {
className,
cssModule,
tag: Tag,
innerRef = ref,
children,
accordionId,
...attributes
} = props;
const { open } = useContext(AccordionContext);
const classes = mapToCssModules(
classNames(className, 'accordion-collapse'),
cssModule,
);
return (
<Collapse
{...attributes}
className={classes}
ref={innerRef}
isOpen={
Array.isArray(open) ? open.includes(accordionId) : open === accordionId
}
>
<Tag className="accordion-body">{children}</Tag>
</Collapse>
);
}
AccordionBody.propTypes = propTypes;
AccordionBody.defaultProps = defaultProps;
export default React.forwardRef((props, ref) => <AccordionBody innerRef={ref} {...props} />);