UNPKG

1.46 kBJavaScriptView Raw
1import React, { useContext } from 'react';
2import PropTypes from 'prop-types';
3import classNames from 'classnames';
4import { mapToCssModules, tagPropType } from './utils';
5import Collapse from './Collapse';
6import { AccordionContext } from './AccordionContext';
7
8const propTypes = {
9 /** Unique key used to control item's collapse/expand */
10 accordionId: PropTypes.string.isRequired,
11 /** To add custom class */
12 className: PropTypes.string,
13 children: PropTypes.node,
14 /** Change existing base class name with a new class name */
15 cssModule: PropTypes.object,
16 /** Pass ref to the component */
17 innerRef: PropTypes.oneOfType([
18 PropTypes.object,
19 PropTypes.string,
20 PropTypes.func,
21 ]),
22 /** Set a custom element for this component */
23 tag: tagPropType,
24};
25
26function AccordionBody(props) {
27 const {
28 className,
29 cssModule,
30 tag: Tag = 'div',
31 innerRef,
32 children,
33 accordionId,
34 ...attributes
35 } = props;
36
37 const { open } = useContext(AccordionContext);
38
39 const classes = mapToCssModules(
40 classNames(className, 'accordion-collapse'),
41 cssModule,
42 );
43
44 return (
45 <Collapse
46 {...attributes}
47 className={classes}
48 ref={innerRef}
49 isOpen={
50 Array.isArray(open) ? open.includes(accordionId) : open === accordionId
51 }
52 >
53 <Tag className="accordion-body">{children}</Tag>
54 </Collapse>
55 );
56}
57
58AccordionBody.propTypes = propTypes;
59
60export default AccordionBody;