Skip to content

Commit 6da4a97

Browse files
authored
Refactor Accordion and AccordionItem components (#988)
* Refactor Accordion to enable nodes in title prop * Move docs-specific logic to docs
1 parent 9af46b8 commit 6da4a97

File tree

5 files changed

+64
-46
lines changed

5 files changed

+64
-46
lines changed

docs/static/docs.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ span.hljs-meta {
266266
font-size: 2rem;
267267
}
268268

269+
.dbcd-main h2.accordion-header {
270+
margin-top: 0;
271+
margin-bottom: 0;
272+
}
273+
269274
.dbcd-main h3 {
270275
margin-top: 1rem;
271276
margin-bottom: 0.5rem;

src/components/accordion/Accordion.js

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import PropTypes from 'prop-types';
33
import {omit} from 'ramda';
44
import RBAccordion from 'react-bootstrap/Accordion';
55

6-
import {
7-
parseChildrenToArray,
8-
resolveChildProps,
9-
stringifyId
10-
} from '../../private/util';
6+
import {parseChildrenToArray, resolveChildProps} from '../../private/util';
7+
import {AccordionContext} from '../../private/AccordionContext';
118

129
/**
1310
* A self contained Accordion component. Build up the children using the
@@ -62,42 +59,11 @@ const Accordion = props => {
6259
children &&
6360
children.map((child, idx) => {
6461
const childProps = resolveChildProps(child);
65-
const {
66-
title,
67-
item_id,
68-
loading_state,
69-
class_name,
70-
className,
71-
id,
72-
...otherProps
73-
} = childProps;
74-
const itemID = item_id || 'item-' + idx;
62+
const itemID = childProps.item_id || `item-${idx}`;
7563
return (
76-
<RBAccordion.Item
77-
id={stringifyId(id)}
78-
key={itemID}
79-
eventKey={itemID}
80-
className={class_name || className}
81-
{...omit(
82-
['setProps', 'persistence', 'persistence_type', 'persisted_props'],
83-
otherProps
84-
)}
85-
data-dash-is-loading={
86-
(loading_state && loading_state.is_loading) || undefined
87-
}
88-
>
89-
<RBAccordion.Header
90-
onClick={() => {
91-
toggle(itemID);
92-
}}
93-
// .dbcd-main h2 has margins defined on it - we need to make
94-
// sure to overwrite them
95-
style={{marginTop: '0rem', marginBottom: '0rem'}}
96-
>
97-
{title}
98-
</RBAccordion.Header>
99-
<RBAccordion.Body>{child}</RBAccordion.Body>
100-
</RBAccordion.Item>
64+
<AccordionContext.Provider key={itemID} value={{toggle, idx}}>
65+
{child}
66+
</AccordionContext.Provider>
10167
);
10268
});
10369

src/components/accordion/AccordionItem.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
1-
import React from 'react';
1+
import React, {useContext} from 'react';
22
import PropTypes from 'prop-types';
3+
import {omit} from 'ramda';
4+
import RBAccordion from 'react-bootstrap/Accordion';
5+
6+
import {stringifyId} from '../../private/util';
7+
import {AccordionContext} from '../../private/AccordionContext';
38

49
/**
510
* A component to build up the children of the accordion.
611
*/
7-
const AccordionItem = props => {
8-
return <>{props.children}</>;
12+
const AccordionItem = ({
13+
title,
14+
item_id,
15+
loading_state,
16+
class_name,
17+
className,
18+
id,
19+
children,
20+
...otherProps
21+
}) => {
22+
const {toggle, idx} = useContext(AccordionContext);
23+
const itemID = item_id || `item-${idx}`;
24+
return (
25+
<RBAccordion.Item
26+
id={stringifyId(id)}
27+
key={itemID}
28+
eventKey={itemID}
29+
className={class_name || className}
30+
{...omit(
31+
['setProps', 'persistence', 'persistence_type', 'persisted_props'],
32+
otherProps
33+
)}
34+
data-dash-is-loading={
35+
(loading_state && loading_state.is_loading) || undefined
36+
}
37+
>
38+
<RBAccordion.Header
39+
onClick={() => {
40+
toggle(itemID);
41+
}}
42+
>
43+
{title}
44+
</RBAccordion.Header>
45+
<RBAccordion.Body>{children}</RBAccordion.Body>
46+
</RBAccordion.Item>
47+
);
948
};
1049

1150
AccordionItem.propTypes = {
@@ -41,7 +80,7 @@ AccordionItem.propTypes = {
4180
/**
4281
* The title on display in the collapsed accordion item.
4382
*/
44-
title: PropTypes.string,
83+
title: PropTypes.node,
4584

4685
/**
4786
* Optional identifier for item used for determining which item is visible

src/components/placeholder/__tests__/Placeholder.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ describe('Placeholder', () => {
136136
</Placeholder>
137137
);
138138

139-
console.log(placeholderStyle.firstChild.style);
140-
141139
expect(placeholderStyle.firstChild).toHaveStyle({
142140
width: '5rem',
143141
height: '5rem'

src/private/AccordionContext.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
3+
/**
4+
* AccordionContext
5+
* {
6+
* toggle: PropTypes.func.isRequired,
7+
* idx: PropTypes.number.isRequired
8+
* }
9+
*/
10+
export const AccordionContext = React.createContext({});

0 commit comments

Comments
 (0)