Skip to content

Commit f40439e

Browse files
feat(AccordionCollapse): add as prop support (react-bootstrap#5991)
* Add As Prop in AccordionCollapse Component * Added test case for AccordionCollapse --should output h1
1 parent a2a4125 commit f40439e

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/AccordionCollapse.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import { Transition } from 'react-transition-group';
66
import { useBootstrapPrefix } from './ThemeProvider';
77
import Collapse, { CollapseProps } from './Collapse';
88
import AccordionContext from './AccordionContext';
9-
import { BsPrefixRefForwardingComponent, BsPrefixOnlyProps } from './helpers';
9+
import { BsPrefixRefForwardingComponent, BsPrefixProps } from './helpers';
1010

11-
export interface AccordionCollapseProps
12-
extends BsPrefixOnlyProps,
13-
CollapseProps {
11+
export interface AccordionCollapseProps extends BsPrefixProps, CollapseProps {
1412
eventKey: string;
1513
}
1614

1715
const propTypes = {
16+
/** Set a custom element for this component */
17+
as: PropTypes.elementType,
18+
1819
/**
1920
* A key that corresponds to the toggler that triggers this collapse's expand or collapse.
2021
*/
@@ -28,7 +29,17 @@ const AccordionCollapse: BsPrefixRefForwardingComponent<
2829
'div',
2930
AccordionCollapseProps
3031
> = React.forwardRef<Transition<any>, AccordionCollapseProps>(
31-
({ bsPrefix, className, children, eventKey, ...props }, ref) => {
32+
(
33+
{
34+
as: Component = 'div',
35+
bsPrefix,
36+
className,
37+
children,
38+
eventKey,
39+
...props
40+
},
41+
ref,
42+
) => {
3243
const { activeEventKey } = useContext(AccordionContext);
3344
bsPrefix = useBootstrapPrefix(bsPrefix, 'accordion-collapse');
3445

@@ -39,7 +50,7 @@ const AccordionCollapse: BsPrefixRefForwardingComponent<
3950
{...props}
4051
className={classNames(className, bsPrefix)}
4152
>
42-
<div>{React.Children.only(children)}</div>
53+
<Component>{React.Children.only(children)}</Component>
4354
</Collapse>
4455
);
4556
},

test/AccordionSpec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mount } from 'enzyme';
22

33
import Accordion from '../src/Accordion';
4+
import AccordionCollapse from '../src/AccordionCollapse';
45
import Dropdown from '../src/Dropdown';
56
import ListGroup from '../src/ListGroup';
67
import Nav from '../src/Nav';
@@ -14,6 +15,19 @@ describe('<Accordion>', () => {
1415
mount(<Accordion flush />).assertSingle('.accordion.accordion-flush');
1516
});
1617

18+
it('should output a h1', () => {
19+
const wrapper = mount(
20+
<Accordion>
21+
<Accordion.Button>Hi</Accordion.Button>
22+
<AccordionCollapse as="h1" eventKey="0">
23+
<span>hidden Data</span>
24+
</AccordionCollapse>
25+
</Accordion>,
26+
);
27+
28+
wrapper.find('AccordionCollapse').assertSingle('h1');
29+
});
30+
1731
it('should only have second item collapsed', () => {
1832
const wrapper = mount(
1933
<Accordion defaultActiveKey="0">

0 commit comments

Comments
 (0)