Skip to content

Commit 3dcc142

Browse files
authored
Support arbitrary Accordion children (#718)
* Support arbitrary children in AccordionItem * Add non-text components to docs example
1 parent 3f25ccc commit 3dcc142

File tree

7 files changed

+57
-50
lines changed

7 files changed

+57
-50
lines changed

docs/components_page/components/accordion/simple.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ accordion <- htmlDiv(
66
dbcAccordion(
77
list(
88
dbcAccordionItem(
9-
"This is the content of the first section",
9+
list(
10+
htmlP("This is the content of the first section"),
11+
dbcButton("Click here")
12+
),
1013
title = "Item 1"
1114
),
1215
dbcAccordionItem(
13-
"This is the content of the second section",
16+
list(
17+
htmlP("This is the content of the second section"),
18+
dbcButton("Don't click me!", color = "danger")
19+
),
1420
title = "Item 2"
1521
),
1622
dbcAccordionItem(

docs/components_page/components/accordion/simple.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ using DashBootstrapComponents, DashCoreComponents, DashHtmlComponents
22

33
accordion = html_div(
44
dbc_accordion([
5-
dbc_accordionitem("This is the content of the first section", title = "Item 1"),
6-
dbc_accordionitem("This is the content of the second section", title = "Item 2"),
5+
dbc_accordionitem(
6+
[html_p("This is the content of the first section"), dbc_button("Click here")],
7+
title = "Item 1",
8+
),
9+
dbc_accordionitem(
10+
[
11+
html_p("This is the content of the second section"),
12+
dbc_button("Don't click me!", color = "danger"),
13+
],
14+
title = "Item 2",
15+
),
716
dbc_accordionitem("This is the content of the third section", title = "Item 3"),
817
],),
918
)

docs/components_page/components/accordion/simple.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
dbc.Accordion(
66
[
77
dbc.AccordionItem(
8-
"This is the content of the first section",
8+
[
9+
html.P("This is the content of the first section"),
10+
dbc.Button("Click here"),
11+
],
912
title="Item 1",
1013
),
1114
dbc.AccordionItem(
12-
"This is the content of the second section",
15+
[
16+
html.P("This is the content of the second section"),
17+
dbc.Button("Don't click me!", color="danger"),
18+
],
1319
title="Item 2",
1420
),
1521
dbc.AccordionItem(

src/components/accordion/Accordion.js

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

6-
const resolveChildProps = child => {
7-
// This may need to change in the future if https://github.com/plotly/dash-renderer/issues/84 is addressed
8-
if (
9-
child.props._dashprivate_layout &&
10-
child.props._dashprivate_layout.props
11-
) {
12-
// props are coming from Dash
13-
return child.props._dashprivate_layout.props;
14-
} else {
15-
// else props are coming from React (e.g. Demo.js, or Tabs.test.js)
16-
return child.props;
17-
}
18-
};
6+
import {parseChildrenToArray, resolveChildProps} from '../../private/util';
197

208
/**
219
* A self contained Accordion component. Build up the children using the
2210
* AccordionItem component.
2311
*/
2412
const Accordion = props => {
25-
const {
13+
let {
2614
children,
2715
active_item,
2816
start_collapsed,
@@ -33,6 +21,7 @@ const Accordion = props => {
3321
className,
3422
...otherProps
3523
} = props;
24+
children = parseChildrenToArray(children);
3625

3726
// if active_item not set initially, choose first item
3827
useEffect(() => {
@@ -89,7 +78,7 @@ const Accordion = props => {
8978
>
9079
{title}
9180
</RBAccordion.Header>
92-
<RBAccordion.Body>{children}</RBAccordion.Body>
81+
<RBAccordion.Body>{child}</RBAccordion.Body>
9382
</RBAccordion.Item>
9483
);
9584
});

src/components/accordion/AccordionItem.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
55
* A component to build up the children of the accordion.
66
*/
77
const AccordionItem = props => {
8-
return <></>;
8+
return <>{props.children}</>;
99
};
1010

1111
AccordionItem.propTypes = {

src/components/tabs/Tabs.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,8 @@ import {omit} from 'ramda';
44
import classnames from 'classnames';
55
import RBNav from 'react-bootstrap/Nav';
66
import RBTab from 'react-bootstrap/Tab';
7-
import {isNil} from 'ramda';
87

9-
const resolveChildProps = child => {
10-
// This may need to change in the future if https://github.com/plotly/dash-renderer/issues/84 is addressed
11-
if (
12-
// disabled is a defaultProp (so it's always set)
13-
// meaning that if it's not set on child.props, the actual
14-
// props we want are lying a bit deeper - which means they
15-
// are coming from Dash
16-
isNil(child.props.disabled) &&
17-
child.props._dashprivate_layout &&
18-
child.props._dashprivate_layout.props
19-
) {
20-
// props are coming from Dash
21-
return child.props._dashprivate_layout.props;
22-
} else {
23-
// else props are coming from React (e.g. Demo.js, or Tabs.test.js)
24-
return child.props;
25-
}
26-
};
27-
28-
const parseChildrenToArray = children => {
29-
if (children && !Array.isArray(children)) {
30-
// if dcc.Tabs.children contains just one single element, it gets passed as an object
31-
// instead of an array - so we put in in a array ourselves!
32-
return [children];
33-
}
34-
return children;
35-
};
8+
import {parseChildrenToArray, resolveChildProps} from '../../private/util';
369

3710
/**
3811
* Create Bootstrap styled tabs. Use the `active_tab` property to set, or get

src/private/util.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const parseChildrenToArray = children => {
2+
if (children && !Array.isArray(children)) {
3+
// if children contains just one single element, it gets passed as an object
4+
// instead of an array - so we put in in a array ourselves!
5+
return [children];
6+
}
7+
return children;
8+
};
9+
10+
const resolveChildProps = child => {
11+
// This may need to change in the future if https://github.com/plotly/dash-renderer/issues/84 is addressed
12+
if (
13+
child.props._dashprivate_layout &&
14+
child.props._dashprivate_layout.props
15+
) {
16+
// props are coming from Dash
17+
return child.props._dashprivate_layout.props;
18+
} else {
19+
// else props are coming from React (e.g. Demo.js, or Tabs.test.js)
20+
return child.props;
21+
}
22+
};
23+
24+
export {parseChildrenToArray, resolveChildProps};

0 commit comments

Comments
 (0)