upload.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """A file upload component."""
  2. from __future__ import annotations
  3. from typing import Dict, List, Optional
  4. from reflex.components.component import EVENT_ARG, Component
  5. from reflex.components.forms.input import Input
  6. from reflex.components.layout.box import Box
  7. from reflex.event import EventChain
  8. from reflex.vars import BaseVar, Var
  9. files_state = "const [files, setFiles] = useState([]);"
  10. upload_file = BaseVar(name="e => setFiles((files) => e)", type_=EventChain)
  11. # Use this var along with the Upload component to render the list of selected files.
  12. selected_files = BaseVar(name="files.map((f) => f.name)", type_=List[str])
  13. class Upload(Component):
  14. """A file upload component."""
  15. library = "react-dropzone"
  16. tag = "ReactDropzone"
  17. is_default = True
  18. # The list of accepted file types. This should be a dictionary of MIME types as keys and array of file formats as
  19. # values.
  20. # supported MIME types: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  21. accept: Var[Optional[Dict[str, List]]]
  22. # Whether the dropzone is disabled.
  23. disabled: Var[bool]
  24. # The maximum number of files that can be uploaded.
  25. max_files: Var[int]
  26. # The maximum file size (bytes) that can be uploaded.
  27. max_size: Var[int]
  28. # The minimum file size (bytes) that can be uploaded.
  29. min_size: Var[int]
  30. # Whether to allow multiple files to be uploaded.
  31. multiple: Var[bool] = True # type: ignore
  32. # Whether to disable click to upload.
  33. no_click: Var[bool]
  34. # Whether to disable drag and drop.
  35. no_drag: Var[bool]
  36. # Whether to disable using the space/enter keys to upload.
  37. no_keyboard: Var[bool]
  38. @classmethod
  39. def create(cls, *children, **props) -> Component:
  40. """Create an upload component.
  41. Args:
  42. children: The children of the component.
  43. props: The properties of the component.
  44. Returns:
  45. The upload component.
  46. """
  47. # get only upload component props
  48. supported_props = cls.get_props()
  49. upload_props = {
  50. key: value for key, value in props.items() if key in supported_props
  51. }
  52. # The file input to use.
  53. upload = Input.create(type_="file")
  54. upload.special_props = {BaseVar(name="{...getInputProps()}", type_=None)}
  55. # The dropzone to use.
  56. zone = Box.create(
  57. upload,
  58. *children,
  59. **{k: v for k, v in props.items() if k not in supported_props},
  60. )
  61. zone.special_props = {BaseVar(name="{...getRootProps()}", type_=None)}
  62. # Create the component.
  63. return super().create(zone, on_drop=upload_file, **upload_props)
  64. def get_controlled_triggers(self) -> Dict[str, Var]:
  65. """Get the event triggers that pass the component's value to the handler.
  66. Returns:
  67. A dict mapping the event trigger to the var that is passed to the handler.
  68. """
  69. return {
  70. "on_drop": EVENT_ARG,
  71. }
  72. def _render(self):
  73. out = super()._render()
  74. out.args = ("getRootProps", "getInputProps")
  75. return out
  76. def _get_hooks(self) -> str | None:
  77. return (super()._get_hooks() or "") + files_state