|
26 | 26 | from typing_extensions import Self, TypeAliasType, TypedDict, TypeVarTuple, Unpack |
27 | 27 |
|
28 | 28 | from reflex import constants |
| 29 | +from reflex.components.field import BaseField |
29 | 30 | from reflex.constants.compiler import CompileVars, Hooks, Imports |
30 | 31 | from reflex.constants.state import FRONTEND_EVENT_STATE |
31 | 32 | from reflex.utils import format |
@@ -1684,6 +1685,31 @@ def parse_args_spec(arg_spec: ArgsSpec | Sequence[ArgsSpec]): |
1684 | 1685 | ), annotations |
1685 | 1686 |
|
1686 | 1687 |
|
| 1688 | +def args_specs_from_fields( |
| 1689 | + fields_dict: Mapping[str, BaseField], |
| 1690 | +) -> dict[str, ArgsSpec | Sequence[ArgsSpec]]: |
| 1691 | + """Get the event triggers and arg specs from the given fields. |
| 1692 | +
|
| 1693 | + Args: |
| 1694 | + fields_dict: The fields, keyed by name |
| 1695 | +
|
| 1696 | + Returns: |
| 1697 | + The args spec for any field annotated as EventHandler. |
| 1698 | + """ |
| 1699 | + return { |
| 1700 | + name: ( |
| 1701 | + metadata[0] |
| 1702 | + if ( |
| 1703 | + (metadata := getattr(field.annotated_type, "__metadata__", None)) |
| 1704 | + is not None |
| 1705 | + ) |
| 1706 | + else no_args_event_spec |
| 1707 | + ) |
| 1708 | + for name, field in fields_dict.items() |
| 1709 | + if field.type_origin is EventHandler |
| 1710 | + } |
| 1711 | + |
| 1712 | + |
1687 | 1713 | def check_fn_match_arg_spec( |
1688 | 1714 | user_func: Callable, |
1689 | 1715 | user_func_parameters: Mapping[str, inspect.Parameter], |
@@ -2436,6 +2462,7 @@ def wrapper( |
2436 | 2462 | check_fn_match_arg_spec = staticmethod(check_fn_match_arg_spec) |
2437 | 2463 | resolve_annotation = staticmethod(resolve_annotation) |
2438 | 2464 | parse_args_spec = staticmethod(parse_args_spec) |
| 2465 | + args_specs_from_fields = staticmethod(args_specs_from_fields) |
2439 | 2466 | unwrap_var_annotation = staticmethod(unwrap_var_annotation) |
2440 | 2467 | get_fn_signature = staticmethod(get_fn_signature) |
2441 | 2468 |
|
|
0 commit comments