Skip to content

Commit 9465410

Browse files
committed
maybe ?
1 parent 41dddef commit 9465410

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

reflex/components/component.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
Type,
2222
TypeVar,
2323
Union,
24+
cast,
2425
get_args,
2526
get_origin,
2627
)
2728

29+
import pydantic.v1
30+
import pydantic.v1.fields
31+
2832
import reflex.state
2933
from reflex.base import Base
3034
from reflex.compiler.templates import STATEFUL_COMPONENT
@@ -73,16 +77,16 @@ class BaseComponent(Base, ABC):
7377
"""
7478

7579
# The children nested within the component.
76-
children: list[BaseComponent] = []
80+
children: list[BaseComponent] = pydantic.v1.Field(default_factory=list)
7781

7882
# The library that the component is based on.
7983
library: str | None = None
8084

8185
# List here the non-react dependency needed by `library`
82-
lib_dependencies: list[str] = []
86+
lib_dependencies: list[str] = pydantic.v1.Field(default_factory=list)
8387

8488
# List here the dependencies that need to be transpiled by Next.js
85-
transpile_packages: list[str] = []
89+
transpile_packages: list[str] = pydantic.v1.Field(default_factory=list)
8690

8791
# The tag to use when rendering the component.
8892
tag: str | None = None
@@ -262,10 +266,12 @@ class Component(BaseComponent, ABC):
262266
"""A component with style, event trigger and other props."""
263267

264268
# The style of the component.
265-
style: Style = Style()
269+
style: Style = pydantic.v1.Field(default_factory=Style)
266270

267271
# A mapping from event triggers to event chains.
268-
event_triggers: dict[str, EventChain | Var] = {}
272+
event_triggers: dict[str, EventChain | Var] = pydantic.v1.Field(
273+
default_factory=dict
274+
)
269275

270276
# The alias for the tag.
271277
alias: str | None = None
@@ -283,28 +289,30 @@ class Component(BaseComponent, ABC):
283289
class_name: Any = None
284290

285291
# Special component props.
286-
special_props: list[Var] = []
292+
special_props: list[Var] = pydantic.v1.Field(default_factory=list)
287293

288294
# Whether the component should take the focus once the page is loaded
289295
autofocus: bool = False
290296

291297
# components that cannot be children
292-
_invalid_children: list[str] = []
298+
_invalid_children: ClassVar[list[str]] = []
293299

294300
# only components that are allowed as children
295-
_valid_children: list[str] = []
301+
_valid_children: ClassVar[list[str]] = []
296302

297303
# only components that are allowed as parent
298-
_valid_parents: list[str] = []
304+
_valid_parents: ClassVar[list[str]] = []
299305

300306
# props to change the name of
301-
_rename_props: dict[str, str] = {}
307+
_rename_props: ClassVar[dict[str, str]] = {}
302308

303309
# custom attribute
304-
custom_attrs: dict[str, Var | Any] = {}
310+
custom_attrs: dict[str, Var | Any] = pydantic.v1.Field(default_factory=dict)
305311

306312
# When to memoize this component and its children.
307-
_memoization_mode: MemoizationMode = MemoizationMode()
313+
_memoization_mode: MemoizationMode = pydantic.v1.PrivateAttr(
314+
default_factory=MemoizationMode
315+
)
308316

309317
# State class associated with this component instance
310318
State: Type[reflex.state.State] | None = None
@@ -582,9 +590,15 @@ def determine_key(value: Any):
582590
"&": style,
583591
}
584592

593+
fields_style = self.get_fields()["style"]
594+
585595
kwargs["style"] = Style(
586596
{
587-
**self.get_fields()["style"].default,
597+
**(
598+
fields_style.default_factory()
599+
if fields_style.default_factory
600+
else fields_style.default
601+
),
588602
**style,
589603
**{attr: value for attr, value in kwargs.items() if attr not in fields},
590604
}

0 commit comments

Comments
 (0)