Skip to content

Commit

Permalink
Add type hinting to dataeditor events (#4210)
Browse files Browse the repository at this point in the history
  • Loading branch information
adhami3310 authored Oct 22, 2024
1 parent 3ab750f commit c103ab5
Show file tree
Hide file tree
Showing 29 changed files with 342 additions and 71 deletions.
4 changes: 3 additions & 1 deletion reflex/components/base/error_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from reflex.vars.base import Var


def on_error_spec(error: Var, info: Var[Dict[str, str]]) -> Tuple[Var[str], Var[str]]:
def on_error_spec(
error: Var[Dict[str, str]], info: Var[Dict[str, str]]
) -> Tuple[Var[str], Var[str]]:
"""The spec for the on_error event handler.
Args:
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/base/error_boundary.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from reflex.style import Style
from reflex.vars.base import Var

def on_error_spec(
error: Var, info: Var[Dict[str, str]]
error: Var[Dict[str, str]], info: Var[Dict[str, str]]
) -> Tuple[Var[str], Var[str]]: ...

class ErrorBoundary(Component):
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/core/clipboard.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Clipboard(Fragment):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_paste: Optional[EventType] = None,
on_paste: Optional[EventType[list[tuple[str, str]]]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
88 changes: 82 additions & 6 deletions reflex/components/datadisplay/dataeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import Enum
from typing import Any, Dict, List, Literal, Optional, Tuple, Union

from typing_extensions import TypedDict

from reflex.base import Base
from reflex.components.component import Component, NoSSRComponent
from reflex.components.literals import LiteralRowMarker
Expand Down Expand Up @@ -120,6 +122,78 @@ def on_edit_spec(pos, data: dict[str, Any]):
return [pos, data]


class Bounds(TypedDict):
"""The bounds of the group header."""

x: int
y: int
width: int
height: int


class CompatSelection(TypedDict):
"""The selection."""

items: list


class Rectangle(TypedDict):
"""The bounds of the group header."""

x: int
y: int
width: int
height: int


class GridSelectionCurrent(TypedDict):
"""The current selection."""

cell: list[int]
range: Rectangle
rangeStack: list[Rectangle]


class GridSelection(TypedDict):
"""The grid selection."""

current: Optional[GridSelectionCurrent]
columns: CompatSelection
rows: CompatSelection


class GroupHeaderClickedEventArgs(TypedDict):
"""The arguments for the group header clicked event."""

kind: str
group: str
location: list[int]
bounds: Bounds
isEdge: bool
shiftKey: bool
ctrlKey: bool
metaKey: bool
isTouch: bool
localEventX: int
localEventY: int
button: int
buttons: int
scrollEdge: list[int]


class GridCell(TypedDict):
"""The grid cell."""

span: Optional[List[int]]


class GridColumn(TypedDict):
"""The grid column."""

title: str
group: Optional[str]


class DataEditor(NoSSRComponent):
"""The DataEditor Component."""

Expand Down Expand Up @@ -238,10 +312,12 @@ class DataEditor(NoSSRComponent):
on_group_header_clicked: EventHandler[on_edit_spec]

# Fired when a group header is right-clicked.
on_group_header_context_menu: EventHandler[lambda grp_idx, data: [grp_idx, data]]
on_group_header_context_menu: EventHandler[
identity_event(int, GroupHeaderClickedEventArgs)
]

# Fired when a group header is renamed.
on_group_header_renamed: EventHandler[lambda idx, val: [idx, val]]
on_group_header_renamed: EventHandler[identity_event(str, str)]

# Fired when a header is clicked.
on_header_clicked: EventHandler[identity_event(Tuple[int, int])]
Expand All @@ -250,16 +326,16 @@ class DataEditor(NoSSRComponent):
on_header_context_menu: EventHandler[identity_event(Tuple[int, int])]

# Fired when a header menu item is clicked.
on_header_menu_click: EventHandler[lambda col, pos: [col, pos]]
on_header_menu_click: EventHandler[identity_event(int, Rectangle)]

# Fired when an item is hovered.
on_item_hovered: EventHandler[identity_event(Tuple[int, int])]

# Fired when a selection is deleted.
on_delete: EventHandler[lambda selection: [selection]]
on_delete: EventHandler[identity_event(GridSelection)]

# Fired when editing is finished.
on_finished_editing: EventHandler[lambda new_value, movement: [new_value, movement]]
on_finished_editing: EventHandler[identity_event(Union[GridCell, None], list[int])]

# Fired when a row is appended.
on_row_appended: EventHandler[empty_event]
Expand All @@ -268,7 +344,7 @@ class DataEditor(NoSSRComponent):
on_selection_cleared: EventHandler[empty_event]

# Fired when a column is resized.
on_column_resize: EventHandler[lambda col, width: [col, width]]
on_column_resize: EventHandler[identity_event(GridColumn, int)]

def add_imports(self) -> ImportDict:
"""Add imports for the component.
Expand Down
78 changes: 66 additions & 12 deletions reflex/components/datadisplay/dataeditor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from enum import Enum
from typing import Any, Dict, List, Literal, Optional, Union, overload

from typing_extensions import TypedDict

from reflex.base import Base
from reflex.components.component import NoSSRComponent
from reflex.event import EventType
Expand Down Expand Up @@ -78,6 +80,54 @@ class DataEditorTheme(Base):

def on_edit_spec(pos, data: dict[str, Any]): ...

class Bounds(TypedDict):
x: int
y: int
width: int
height: int

class CompatSelection(TypedDict):
items: list

class Rectangle(TypedDict):
x: int
y: int
width: int
height: int

class GridSelectionCurrent(TypedDict):
cell: list[int]
range: Rectangle
rangeStack: list[Rectangle]

class GridSelection(TypedDict):
current: Optional[GridSelectionCurrent]
columns: CompatSelection
rows: CompatSelection

class GroupHeaderClickedEventArgs(TypedDict):
kind: str
group: str
location: list[int]
bounds: Bounds
isEdge: bool
shiftKey: bool
ctrlKey: bool
metaKey: bool
isTouch: bool
localEventX: int
localEventY: int
button: int
buttons: int
scrollEdge: list[int]

class GridCell(TypedDict):
span: Optional[List[int]]

class GridColumn(TypedDict):
title: str
group: Optional[str]

class DataEditor(NoSSRComponent):
def add_imports(self) -> ImportDict: ...
def add_hooks(self) -> list[str]: ...
Expand Down Expand Up @@ -136,24 +186,28 @@ class DataEditor(NoSSRComponent):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_cell_activated: Optional[EventType] = None,
on_cell_clicked: Optional[EventType] = None,
on_cell_context_menu: Optional[EventType] = None,
on_cell_activated: Optional[EventType[tuple[int, int]]] = None,
on_cell_clicked: Optional[EventType[tuple[int, int]]] = None,
on_cell_context_menu: Optional[EventType[tuple[int, int]]] = None,
on_cell_edited: Optional[EventType] = None,
on_click: Optional[EventType[[]]] = None,
on_column_resize: Optional[EventType] = None,
on_column_resize: Optional[EventType[GridColumn, int]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_delete: Optional[EventType] = None,
on_delete: Optional[EventType[GridSelection]] = None,
on_double_click: Optional[EventType[[]]] = None,
on_finished_editing: Optional[EventType] = None,
on_finished_editing: Optional[
EventType[Union[GridCell, None], list[int]]
] = None,
on_focus: Optional[EventType[[]]] = None,
on_group_header_clicked: Optional[EventType] = None,
on_group_header_context_menu: Optional[EventType] = None,
on_group_header_renamed: Optional[EventType] = None,
on_header_clicked: Optional[EventType] = None,
on_header_context_menu: Optional[EventType] = None,
on_header_menu_click: Optional[EventType] = None,
on_item_hovered: Optional[EventType] = None,
on_group_header_context_menu: Optional[
EventType[int, GroupHeaderClickedEventArgs]
] = None,
on_group_header_renamed: Optional[EventType[str, str]] = None,
on_header_clicked: Optional[EventType[tuple[int, int]]] = None,
on_header_context_menu: Optional[EventType[tuple[int, int]]] = None,
on_header_menu_click: Optional[EventType[int, Rectangle]] = None,
on_item_hovered: Optional[EventType[tuple[int, int]]] = None,
on_mount: Optional[EventType[[]]] = None,
on_mouse_down: Optional[EventType[[]]] = None,
on_mouse_enter: Optional[EventType[[]]] = None,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/moment/moment.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Moment(NoSSRComponent):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_change: Optional[EventType] = None,
on_change: Optional[EventType[str]] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/radix/primitives/drawer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DrawerRoot(DrawerComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down Expand Up @@ -511,7 +511,7 @@ class Drawer(ComponentNamespace):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/color_mode.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class ColorModeSwitch(Switch):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_change: Optional[EventType] = None,
on_change: Optional[EventType[bool]] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/components/alert_dialog.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AlertDialogRoot(RadixThemesComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
6 changes: 3 additions & 3 deletions reflex/components/radix/themes/components/checkbox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Checkbox(RadixThemesComponent):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_change: Optional[EventType] = None,
on_change: Optional[EventType[bool]] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
Expand Down Expand Up @@ -263,7 +263,7 @@ class HighLevelCheckbox(RadixThemesComponent):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_change: Optional[EventType] = None,
on_change: Optional[EventType[bool]] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
Expand Down Expand Up @@ -407,7 +407,7 @@ class CheckboxNamespace(ComponentNamespace):
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
on_blur: Optional[EventType[[]]] = None,
on_change: Optional[EventType] = None,
on_change: Optional[EventType[bool]] = None,
on_click: Optional[EventType[[]]] = None,
on_context_menu: Optional[EventType[[]]] = None,
on_double_click: Optional[EventType[[]]] = None,
Expand Down
2 changes: 1 addition & 1 deletion reflex/components/radix/themes/components/context_menu.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ContextMenuRoot(RadixThemesComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/radix/themes/components/dialog.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DialogRoot(RadixThemesComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down Expand Up @@ -382,7 +382,7 @@ class Dialog(ComponentNamespace):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
4 changes: 2 additions & 2 deletions reflex/components/radix/themes/components/dropdown_menu.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DropdownMenuRoot(RadixThemesComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down Expand Up @@ -363,7 +363,7 @@ class DropdownMenuSub(RadixThemesComponent):
on_mouse_out: Optional[EventType[[]]] = None,
on_mouse_over: Optional[EventType[[]]] = None,
on_mouse_up: Optional[EventType[[]]] = None,
on_open_change: Optional[EventType] = None,
on_open_change: Optional[EventType[bool]] = None,
on_scroll: Optional[EventType[[]]] = None,
on_unmount: Optional[EventType[[]]] = None,
**props,
Expand Down
Loading

0 comments on commit c103ab5

Please sign in to comment.