Source code for gam.ast.event
from typing import Literal, Union, List, Optional
from pydantic import Field
from gam.ast.base_ast import ASTNode
from gam.ast.stmt import FunctionDeclaration
from gam.ast.expr import Identifier, Expression
[docs]
class EventHandler(ASTNode):
"""
Represents an event handler that responds to specific events.
For example:
```
Button(
events=[
EventHandler(
event=Event(event_type='click'),
handler_function=Identifier(name='handleClick')
),
EventHandler(
event=Event(event_type='change'),
handler_function=Identifier(name='handleChange')
),
]
)
```
"""
name: Literal['EventHandler'] = Field(default='EventHandler')
event_type: Literal['click', 'input', 'change', 'submit'] = Field(
description="The type of event this handler responds to"
)
handler_function: Union[Identifier, FunctionDeclaration] = Field(
description="The function that handles the event"
)
args: Optional[List[Expression]] = Field(
default=None,
description="The arguments to pass to the handler function. Used for rendering the event handler." \
"Usually used in the component that support for loop. e.g. Card, DataTable, etc."
)