Skip to content

Commit 6ea6c04

Browse files
authored
improve upload state handlers ergonomics (#5843)
1 parent 42e77b1 commit 6ea6c04

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

pyi_hashes.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"reflex/components/core/helmet.pyi": "43f8497c8fafe51e29dca1dd535d143a",
2020
"reflex/components/core/html.pyi": "ea5919db8c8172913185977df900f36b",
2121
"reflex/components/core/sticky.pyi": "a9b4492e423f1dd4ccbf270c8ea90157",
22-
"reflex/components/core/upload.pyi": "77e828bbc55dd6593efdba1504e0cb5e",
22+
"reflex/components/core/upload.pyi": "6dc28804a6dddf903e31162e87c1b023",
2323
"reflex/components/core/window_events.pyi": "76bf03a273a1fbbb3b333e10d5d08c30",
2424
"reflex/components/datadisplay/__init__.pyi": "52755871369acbfd3a96b46b9a11d32e",
2525
"reflex/components/datadisplay/code.pyi": "b86769987ef4d1cbdddb461be88539fd",

reflex/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
)
7070
from reflex.components.core.breakpoints import set_breakpoints
7171
from reflex.components.core.sticky import sticky
72-
from reflex.components.core.upload import Upload, get_upload_dir
7372
from reflex.components.radix import themes
7473
from reflex.components.sonner.toast import toast
7574
from reflex.config import get_config
@@ -682,6 +681,8 @@ def _add_default_endpoints(self):
682681

683682
def _add_optional_endpoints(self):
684683
"""Add optional api endpoints (_upload)."""
684+
from reflex.components.core.upload import Upload, get_upload_dir
685+
685686
if not self._api:
686687
return
687688
upload_is_used_marker = (

reflex/components/core/upload.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Any, ClassVar
88

9+
from reflex.app import UploadFile
910
from reflex.components.base.fragment import Fragment
1011
from reflex.components.component import (
1112
Component,
@@ -29,7 +30,9 @@
2930
call_event_fn,
3031
call_event_handler,
3132
parse_args_spec,
33+
passthrough_event_spec,
3234
run_script,
35+
upload_files,
3336
)
3437
from reflex.style import Style
3538
from reflex.utils import format
@@ -168,16 +171,7 @@ def get_upload_url(file_path: str | Var[str]) -> Var[str]:
168171
return Var.create(f"{uploaded_files_url_prefix}/{file_path}")
169172

170173

171-
def _on_drop_spec(files: Var) -> tuple[Var[Any]]:
172-
"""Args spec for the on_drop event trigger.
173-
174-
Args:
175-
files: The files to upload.
176-
177-
Returns:
178-
Signature for on_drop handler including the files to upload.
179-
"""
180-
return (files,)
174+
_on_drop_spec = passthrough_event_spec(list[UploadFile])
181175

182176

183177
def _default_drop_rejected(rejected_files: ArrayVar[list[dict[str, Any]]]) -> EventSpec:
@@ -302,19 +296,21 @@ def create(cls, *children, **props) -> Component:
302296
}
303297

304298
# Create the component.
305-
upload_props["id"] = props.get("id", DEFAULT_UPLOAD_ID)
299+
upload_props["id"] = upload_id = props.get("id", DEFAULT_UPLOAD_ID)
306300

307301
if upload_props.get("on_drop") is None:
308302
# If on_drop is not provided, save files to be uploaded later.
309-
upload_props["on_drop"] = upload_file(upload_props["id"])
303+
upload_props["on_drop"] = upload_file(upload_id)
310304
else:
311305
on_drop = (
312306
[on_drop_prop]
313307
if not isinstance(on_drop_prop := upload_props["on_drop"], Sequence)
314308
else list(on_drop_prop)
315309
)
316310
for ix, event in enumerate(on_drop):
317-
if isinstance(event, (EventHandler, EventSpec)):
311+
if isinstance(event, EventHandler):
312+
event = event(upload_files(upload_id))
313+
if isinstance(event, EventSpec):
318314
# Call the lambda to get the event chain.
319315
event = call_event_handler(event, _on_drop_spec)
320316
elif isinstance(event, Callable):

0 commit comments

Comments
 (0)