Skip to content

Commit f42e3a2

Browse files
committed
Handle components in list.
1 parent 3fb5661 commit f42e3a2

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

@plotly/dash-test-components/src/components/ComponentAsProp.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import PropTypes from 'prop-types';
33

44

55
const ComponentAsProp = (props) => {
6-
const { element, id } = props;
6+
const { element, elements, id } = props;
77
return (
88
<div id={id}>
9-
{element}
9+
{elements || element}
1010
</div>
1111
)
1212
}
1313

1414
ComponentAsProp.propTypes = {
1515
id: PropTypes.string,
1616
element: PropTypes.node,
17+
18+
elements: PropTypes.arrayOf(PropTypes.node),
1719
}
1820

1921
export default ComponentAsProp;

dash/dash-renderer/src/TreeContainer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ class BaseTreeContainer extends Component {
205205
.map(childrenProp => {
206206
const node = _dashprivate_layout.props[childrenProp];
207207
if (node) {
208+
if (Array.isArray(node)) {
209+
return assoc(
210+
childrenProp,
211+
node.map((n, i) =>
212+
this.createContainer(
213+
this.props,
214+
n,
215+
concat(this.props._dashprivate_path, [
216+
'props',
217+
childrenProp,
218+
i
219+
])
220+
)
221+
)
222+
);
223+
}
208224
return assoc(
209225
childrenProp,
210226
this.createContainer(

dash/development/base_component.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __str__(self):
8181

8282
REQUIRED = _REQUIRED()
8383

84-
def __init__(self, **kwargs):
84+
def __init__(self, **kwargs): # pylint: disable=too-many-branches
8585
import dash # pylint: disable=import-outside-toplevel, cyclic-import
8686

8787
self._children_props = []
@@ -134,8 +134,14 @@ def __init__(self, **kwargs):
134134
f"\nAllowed arguments: {allowed_args}"
135135
)
136136

137-
if k != "children" and isinstance(v, Component):
138-
self._children_props.append(k)
137+
if k not in ("children", "id", "style", "className") and not k_in_wildcards:
138+
if isinstance(v, Component):
139+
self._children_props.append(k)
140+
if hasattr(v, "__iter__"):
141+
for item in v:
142+
if isinstance(item, Component):
143+
self._children_props.append(k)
144+
break
139145

140146
if k == "id":
141147
if isinstance(v, dict):

tests/integration/renderer/test_component_as_prop.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ def test_rdcap001_component_as_prop(dash_duo):
2828
Div(id="output-from-prop"),
2929
]
3030
),
31+
ComponentAsProp(
32+
id="elements",
33+
elements=[
34+
Div("one", id="list-one"),
35+
Div("two", id="list-two"),
36+
Div(id="list-output"),
37+
],
38+
),
39+
Div(
40+
[
41+
Button("click-list", id="to-list"),
42+
Div(id="output-from-list"),
43+
]
44+
),
3145
]
3246
)
3347

@@ -43,6 +57,16 @@ def from_as_prop(n_clicks):
4357
def send_nested(n_clicks):
4458
return f"Nested: {n_clicks}"
4559

60+
@app.callback(
61+
Output("output-from-list", "children"), [Input("list-two", "n_clicks")]
62+
)
63+
def send_list_output(n_clicks):
64+
return f"From list: {n_clicks}"
65+
66+
@app.callback(Output("list-output", "children"), [Input("to-list", "n_clicks")])
67+
def send_to_list(n_clicks):
68+
return f"To list: {n_clicks}"
69+
4670
dash_duo.start_server(app)
4771

4872
dash_duo.wait_for_text_to_equal("#as-props", "as-props")
@@ -54,3 +78,15 @@ def send_nested(n_clicks):
5478
nested = dash_duo.wait_for_element("#send-nested")
5579
nested.click()
5680
dash_duo.wait_for_text_to_equal("#nested-output", "Nested: 1")
81+
82+
elements = dash_duo.find_elements("#elements div")
83+
84+
assert len(elements) == 3
85+
86+
to_list = dash_duo.find_element("#to-list")
87+
to_list.click()
88+
dash_duo.wait_for_text_to_equal("#list-output", "To list: 1")
89+
90+
from_list = dash_duo.find_element("#list-two")
91+
from_list.click()
92+
dash_duo.wait_for_text_to_equal("#output-from-list", "From list: 1")

0 commit comments

Comments
 (0)