-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
AccordionCollapse.tsx
62 lines (54 loc) · 1.74 KB
/
AccordionCollapse.tsx
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
import classNames from 'classnames';
import * as React from 'react';
import { useContext } from 'react';
import PropTypes from 'prop-types';
import { Transition } from 'react-transition-group';
import { useBootstrapPrefix } from './ThemeProvider';
import Collapse, { CollapseProps } from './Collapse';
import AccordionContext, { isAccordionItemSelected } from './AccordionContext';
import { BsPrefixRefForwardingComponent, BsPrefixProps } from './helpers';
export interface AccordionCollapseProps extends BsPrefixProps, CollapseProps {
eventKey: string;
}
const propTypes = {
/** Set a custom element for this component */
as: PropTypes.elementType,
/**
* A key that corresponds to the toggler that triggers this collapse's expand or collapse.
*/
eventKey: PropTypes.string.isRequired,
/** Children prop should only contain a single child, and is enforced as such */
children: PropTypes.element.isRequired,
};
const AccordionCollapse: BsPrefixRefForwardingComponent<
'div',
AccordionCollapseProps
> = React.forwardRef<Transition<any>, AccordionCollapseProps>(
(
{
as: Component = 'div',
bsPrefix,
className,
children,
eventKey,
...props
},
ref,
) => {
const { activeEventKey } = useContext(AccordionContext);
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-collapse');
return (
<Collapse
ref={ref}
in={isAccordionItemSelected(activeEventKey, eventKey)}
{...props}
className={classNames(className, bsPrefix)}
>
<Component>{React.Children.only(children)}</Component>
</Collapse>
);
},
) as any;
AccordionCollapse.propTypes = propTypes;
AccordionCollapse.displayName = 'AccordionCollapse';
export default AccordionCollapse;