upload.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """A file upload component."""
  2. from typing import Dict
  3. from pynecone.components.component import EVENT_ARG, Component
  4. from pynecone.components.forms.input import Input
  5. from pynecone.components.layout.box import Box
  6. from pynecone.event import EventChain
  7. from pynecone.var import BaseVar, Var
  8. upload_file = BaseVar(name="e => File(e)", type_=EventChain)
  9. class Upload(Component):
  10. """A file upload component."""
  11. library = "react-dropzone"
  12. tag = "ReactDropzone"
  13. @classmethod
  14. def create(cls, *children, **props) -> Component:
  15. """Create an upload component.
  16. Args:
  17. children: The children of the component.
  18. props: The properties of the component.
  19. Returns:
  20. The upload component.
  21. """
  22. # The file input to use.
  23. upload = Input.create(type_="file")
  24. upload.special_props = {BaseVar(name="{...getInputProps()}", type_=None)}
  25. # The dropzone to use.
  26. zone = Box.create(upload, *children, **props)
  27. zone.special_props = {BaseVar(name="{...getRootProps()}", type_=None)}
  28. # Create the component.
  29. return super().create(zone, on_drop=upload_file)
  30. @classmethod
  31. def get_controlled_triggers(cls) -> Dict[str, Var]:
  32. """Get the event triggers that pass the component's value to the handler.
  33. Returns:
  34. A dict mapping the event trigger to the var that is passed to the handler.
  35. """
  36. return {
  37. "on_drop": EVENT_ARG,
  38. }
  39. def _render(self):
  40. out = super()._render()
  41. out.args = ("getRootProps", "getInputProps")
  42. return out