Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required children prop #2218

Merged
merged 19 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,12 @@
import React from 'react';
import { RequiredChildrenComponentProps } from "../props";


const RequiredChildrenComponent = (props: RequiredChildrenComponentProps) => {
const {children} = props;
siner308 marked this conversation as resolved.
Show resolved Hide resolved
return <div>
{children}
</div>
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved
}

export default RequiredChildrenComponent;
Expand Up @@ -6,6 +6,7 @@ import WrappedHTML from './components/WrappedHTML';
import FCComponent from './components/FCComponent';
import EmptyComponent from './components/EmptyComponent';
import MixedComponent from './components/MixedComponent';
import RequiredChildrenComponent from './components/RequiredChildrenComponent';

export {
TypeScriptComponent,
Expand All @@ -16,4 +17,5 @@ export {
FCComponent,
EmptyComponent,
MixedComponent,
RequiredChildrenComponent,
};
Expand Up @@ -48,3 +48,7 @@ export type WrappedHTMLProps = {
children?: React.ReactNode;
id?: string;
} & Pick<React.ButtonHTMLAttributes<any>, 'autoFocus'>

export type RequiredChildrenComponentProps = {
children: React.ReactNode;
}
38 changes: 29 additions & 9 deletions dash/development/_py_components_generation.py
@@ -1,7 +1,7 @@
from collections import OrderedDict
import copy
import os
from textwrap import fill
from textwrap import fill, dedent
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

from dash.development.base_component import _explicitize_args
from dash.exceptions import NonExistentEventException
Expand Down Expand Up @@ -65,11 +65,8 @@ def __init__(self, {default_argtext}):
_explicit_args = kwargs.pop('_explicit_args')
_locals = locals()
_locals.update(kwargs) # For wildcard attrs and excess named props
args = {{k: _locals[k] for k in _explicit_args if k != 'children'}}
for k in {required_props}:
if k not in args:
raise TypeError(
'Required argument `' + k + '` was not specified.')
args = {args}
{required_validation}
super({typename}, self).__init__({argtext})
'''

Expand All @@ -87,18 +84,40 @@ def __init__(self, {default_argtext}):
description=description,
prop_reorder_exceptions=prop_reorder_exceptions,
).replace("\r\n", "\n")
required_args = required_props(filtered_props)
is_children_required = 'children' in required_args
required_args = list(filter(lambda arg: arg != 'children', required_args))
alexcjohnson marked this conversation as resolved.
Show resolved Hide resolved

prohibit_events(props)

# pylint: disable=unused-variable
prop_keys = list(props.keys())
if "children" in props:
if "children" in props and "children" in list_of_valid_keys:
prop_keys.remove("children")
default_argtext = "children=None, "
args = "{k: _locals[k] for k in _explicit_args if k != 'children'}"
argtext = "children=children, **args"
else:
default_argtext = ""
args = "{k: _locals[k] for k in _explicit_args}"
argtext = "**args"

if len(required_args) == 0:
required_validation = ""
else:
required_validation = f"""
for k in {required_args}:
if k not in args:
raise TypeError(
'Required argument `' + k + '` was not specified.')
"""

if is_children_required:
required_validation += """
if 'children' not in _explicit_args:
raise TypeError('Required argument children was not specified.')
"""

default_arglist = [
(
f"{p:s}=Component.REQUIRED"
Expand All @@ -121,8 +140,8 @@ def __init__(self, {default_argtext}):
)

default_argtext += ", ".join(default_arglist + ["**kwargs"])
required_args = required_props(filtered_props)
nodes = collect_nodes({k: v for k, v in props.items() if k != "children"})

return c.format(
typename=typename,
namespace=namespace,
Expand All @@ -131,8 +150,9 @@ def __init__(self, {default_argtext}):
list_of_valid_keys=list_of_valid_keys,
docstring=docstring,
default_argtext=default_argtext,
args=args,
argtext=argtext,
required_props=required_args,
required_validation=required_validation,
children_props=nodes,
base_nodes=filter_base_nodes(nodes) + ["children"],
)
Expand Down
3 changes: 2 additions & 1 deletion dash/testing/application_runners.py
Expand Up @@ -13,6 +13,7 @@
import runpy
import requests
import psutil

# pylint: disable=no-member
import multiprocess

Expand Down Expand Up @@ -216,7 +217,7 @@ def __init__(self, keep_open=False, stop_timeout=3):

# pylint: disable=arguments-differ
def start(self, app, start_timeout=3, **kwargs):
self.port = kwargs.get('port', 8050)
self.port = kwargs.get("port", 8050)

def target():
app.scripts.config.serve_locally = True
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/devtools/test_hot_reload.py
Expand Up @@ -88,7 +88,8 @@ def new_text(n):

try:
until(
lambda: dash_duo_mp.driver.execute_script("return window.cheese") == "gouda",
lambda: dash_duo_mp.driver.execute_script("return window.cheese")
== "gouda",
timeout=10,
)
finally:
Expand All @@ -97,7 +98,8 @@ def new_text(n):
f.write(old_hard)

until(
lambda: dash_duo_mp.driver.execute_script("return window.cheese") == "roquefort",
lambda: dash_duo_mp.driver.execute_script("return window.cheese")
== "roquefort",
timeout=10,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/renderer/test_component_as_prop.py
Expand Up @@ -102,7 +102,7 @@ def test_rdcap001_component_as_prop(dash_duo):
"id": "multi2",
"first": Span("foo"),
"second": Span("bar"),
}
},
],
),
]
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_generation.py
Expand Up @@ -9,6 +9,7 @@
TypeScriptComponent,
TypeScriptClassComponent,
StandardComponent,
RequiredChildrenComponent,
)
from dash_test_components import StyledComponent
from dash.html import Button, Div
Expand Down Expand Up @@ -99,3 +100,10 @@ def test_gene003_max_props():

with pytest.raises(TypeError):
MyNestedComponent(valuey="nor this")


def test_gene004_required_children_prop():
with pytest.raises(TypeError):
RequiredChildrenComponent()

RequiredChildrenComponent(children='worked')