|
20 | 20 | ) |
21 | 21 | from reflex.style import Style |
22 | 22 | from reflex.utils import console, format, imports, types |
| 23 | +from reflex.utils.serializers import serializer |
23 | 24 | from reflex.vars import BaseVar, ImportVar, Var |
24 | 25 |
|
25 | 26 |
|
@@ -400,6 +401,20 @@ def get_initial_props(cls) -> Set[str]: |
400 | 401 | """ |
401 | 402 | return set() |
402 | 403 |
|
| 404 | + @classmethod |
| 405 | + def get_component_props(cls) -> set[str]: |
| 406 | + """Get the props that expected a component as value. |
| 407 | +
|
| 408 | + Returns: |
| 409 | + The components props. |
| 410 | + """ |
| 411 | + return { |
| 412 | + name |
| 413 | + for name, field in cls.get_fields().items() |
| 414 | + if name in cls.get_props() |
| 415 | + and types._issubclass(field.outer_type_, Component) |
| 416 | + } |
| 417 | + |
403 | 418 | @classmethod |
404 | 419 | def create(cls, *children, **props) -> Component: |
405 | 420 | """Create the component. |
@@ -596,19 +611,48 @@ def get_dynamic_imports(self) -> Set[str]: |
596 | 611 | # Return the dynamic imports |
597 | 612 | return dynamic_imports |
598 | 613 |
|
599 | | - def _get_dependencies_imports(self): |
600 | | - return { |
601 | | - dep: {ImportVar(tag=None, render=False)} for dep in self.lib_dependencies |
602 | | - } |
| 614 | + def _get_props_imports(self) -> imports.ImportDict: |
| 615 | + """Get the imports needed for components props. |
| 616 | +
|
| 617 | + Returns: |
| 618 | + The imports for the components props of the component. |
| 619 | + """ |
| 620 | + return imports.merge_imports( |
| 621 | + *[ |
| 622 | + getattr(self, prop).get_imports() |
| 623 | + for prop in self.get_component_props() |
| 624 | + if getattr(self, prop) is not None |
| 625 | + ] |
| 626 | + ) |
| 627 | + |
| 628 | + def _get_dependencies_imports(self) -> imports.ImportDict: |
| 629 | + """Get the imports from lib_dependencies for installing. |
| 630 | +
|
| 631 | + Returns: |
| 632 | + The dependencies imports of the component. |
| 633 | + """ |
| 634 | + return imports.merge_imports( |
| 635 | + {dep: {ImportVar(tag=None, render=False)} for dep in self.lib_dependencies} |
| 636 | + ) |
603 | 637 |
|
604 | 638 | def _get_imports(self) -> imports.ImportDict: |
605 | | - imports = {} |
| 639 | + """Get all the libraries and fields that are used by the component. |
| 640 | +
|
| 641 | + Returns: |
| 642 | + The imports needed by the component. |
| 643 | + """ |
| 644 | + _imports = {} |
606 | 645 | if self.library is not None and self.tag is not None: |
607 | | - imports[self.library] = {self.import_var} |
608 | | - return {**self._get_dependencies_imports(), **imports} |
| 646 | + _imports[self.library] = {self.import_var} |
| 647 | + |
| 648 | + return imports.merge_imports( |
| 649 | + self._get_props_imports(), |
| 650 | + self._get_dependencies_imports(), |
| 651 | + _imports, |
| 652 | + ) |
609 | 653 |
|
610 | 654 | def get_imports(self) -> imports.ImportDict: |
611 | | - """Get all the libraries and fields that are used by the component. |
| 655 | + """Get all the libraries and fields that are used by the component and its children. |
612 | 656 |
|
613 | 657 | Returns: |
614 | 658 | The import dict with the required imports. |
@@ -988,3 +1032,16 @@ def _get_dynamic_imports(self) -> str: |
988 | 1032 | else "" |
989 | 1033 | ) |
990 | 1034 | return "".join((library_import, mod_import, opts_fragment)) |
| 1035 | + |
| 1036 | + |
| 1037 | +@serializer |
| 1038 | +def serialize_component(comp: Component): |
| 1039 | + """Serialize a component. |
| 1040 | +
|
| 1041 | + Args: |
| 1042 | + comp: The component to serialize. |
| 1043 | +
|
| 1044 | + Returns: |
| 1045 | + The serialized component. |
| 1046 | + """ |
| 1047 | + return str(comp) |
0 commit comments