Skip to content
This repository was archived by the owner on Oct 19, 2021. It is now read-only.

Commit 4319174

Browse files
authored
feat(Accordion): support div for the title (#1318)
* feat(Accordion): support div for the title This change also support non-`<button>` element for the expando button, via `renderExpando` prop. Fixes #1178. * chore(AccordionItem): test fix
1 parent 5fbbc98 commit 4319174

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed
Binary file not shown.

src/components/AccordionItem/AccordionItem-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('AccordionItem', () => {
109109
const wrapper = mount(
110110
<AccordionItem onClick={onClick} onHeadingClick={onHeadingClick} />
111111
);
112-
const heading = wrapper.find('.bx--accordion__heading');
112+
const heading = wrapper.find('button.bx--accordion__heading');
113113

114114
it('should call onClick', () => {
115115
wrapper.simulate('click');
@@ -126,7 +126,7 @@ describe('AccordionItem', () => {
126126
const toggler = mount(
127127
<AccordionItem title="A heading">Lorem ipsum.</AccordionItem>
128128
);
129-
const heading = toggler.find('.bx--accordion__heading');
129+
const heading = toggler.find('button.bx--accordion__heading');
130130

131131
it('should set state to open when clicked', () => {
132132
expect(toggler.state().open).toEqual(false);

src/components/AccordionItem/AccordionItem.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import classnames from 'classnames';
44
import { iconChevronRight } from 'carbon-icons';
55
import Icon from '../Icon';
66

7+
const defaultRenderExpando = props => <button {...props} />;
8+
79
export default class AccordionItem extends Component {
810
state = {};
911

@@ -23,6 +25,12 @@ export default class AccordionItem extends Component {
2325
*/
2426
title: PropTypes.node,
2527

28+
/**
29+
* The callback function to render the expando button.
30+
* Can be a React component class.
31+
*/
32+
renderExpando: PropTypes.func,
33+
2634
/**
2735
* The description of the expando icon.
2836
*/
@@ -46,6 +54,7 @@ export default class AccordionItem extends Component {
4654

4755
static defaultProps = {
4856
title: 'title',
57+
renderExpando: defaultRenderExpando,
4958
iconDescription: 'Expand/Collapse',
5059
open: false,
5160
onClick: () => {},
@@ -85,6 +94,7 @@ export default class AccordionItem extends Component {
8594
const {
8695
className,
8796
title,
97+
renderExpando: Expando,
8898
iconDescription,
8999
children,
90100
onClick, // eslint-disable-line no-unused-vars
@@ -106,7 +116,7 @@ export default class AccordionItem extends Component {
106116
onKeyPress={this.handleKeyPress}
107117
role="presentation"
108118
{...other}>
109-
<button
119+
<Expando
110120
type="button"
111121
className="bx--accordion__heading"
112122
role="tab"
@@ -116,8 +126,8 @@ export default class AccordionItem extends Component {
116126
icon={iconChevronRight}
117127
description={iconDescription}
118128
/>
119-
<p className="bx--accordion__title">{title}</p>
120-
</button>
129+
<div className="bx--accordion__title">{title}</div>
130+
</Expando>
121131
<div className="bx--accordion__content">{children}</div>
122132
</li>
123133
);

0 commit comments

Comments
 (0)