props.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """A class that holds props to be passed or applied to a component."""
  2. from __future__ import annotations
  3. from reflex.base import Base
  4. from reflex.utils import format
  5. from reflex.vars.object import LiteralObjectVar
  6. class PropsBase(Base):
  7. """Base for a class containing props that can be serialized as a JS object."""
  8. def json(self) -> str:
  9. """Convert the object to a json-like string.
  10. Vars will be unwrapped so they can represent actual JS var names and functions.
  11. Keys will be converted to camelCase.
  12. Returns:
  13. The object as a Javascript Object literal.
  14. """
  15. return LiteralObjectVar.create(
  16. {format.to_camel_case(key): value for key, value in self.dict().items()}
  17. ).json()
  18. def dict(self, *args, **kwargs):
  19. """Convert the object to a dictionary.
  20. Keys will be converted to camelCase.
  21. Args:
  22. *args: Arguments to pass to the parent class.
  23. **kwargs: Keyword arguments to pass to the parent class.
  24. Returns:
  25. The object as a dictionary.
  26. """
  27. return {
  28. format.to_camel_case(key): value
  29. for key, value in super().dict(*args, **kwargs).items()
  30. }