props.py 906 B

123456789101112131415161718192021222324252627282930
  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.utils.serializers import serialize
  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 format.unwrap_vars(
  16. self.__config__.json_dumps(
  17. {
  18. format.to_camel_case(key): value
  19. for key, value in self.dict().items()
  20. },
  21. default=serialize,
  22. )
  23. )