UNPKG

1.56 kBJavaScriptView Raw
1import React, { useMemo } from 'react';
2import PropTypes from 'prop-types';
3import classNames from 'classnames';
4import { mapToCssModules, tagPropType } from './utils';
5import { AccordionContext } from './AccordionContext';
6
7const propTypes = {
8 children: PropTypes.node,
9 /** Add custom class */
10 className: PropTypes.string,
11 /** Change existing className with a new className */
12 cssModule: PropTypes.object,
13 /** Render accordions edge-to-edge with their parent container */
14 flush: PropTypes.bool,
15 innerRef: PropTypes.oneOfType([
16 PropTypes.object,
17 PropTypes.string,
18 PropTypes.func,
19 ]),
20 /** The current active key that corresponds to the currently expanded card */
21 open: PropTypes.oneOfType([PropTypes.array, PropTypes.string]).isRequired,
22 /** Set a custom element for this component */
23 tag: tagPropType,
24 /** Function that's triggered on clicking `AccordionHeader` */
25 toggle: PropTypes.func.isRequired,
26};
27
28function Accordion(props) {
29 const {
30 flush,
31 open,
32 toggle,
33 className,
34 cssModule,
35 tag: Tag = 'div',
36 innerRef,
37 ...attributes
38 } = props;
39 const classes = mapToCssModules(
40 classNames(className, 'accordion', {
41 'accordion-flush': flush,
42 }),
43 cssModule,
44 );
45
46 const accordionContext = useMemo(() => ({
47 open,
48 toggle,
49 }));
50
51 return (
52 <AccordionContext.Provider value={accordionContext}>
53 <Tag {...attributes} className={classes} ref={innerRef} />
54 </AccordionContext.Provider>
55 );
56}
57
58Accordion.propTypes = propTypes;
59
60export default Accordion;