Source code for gam.ast.view
from typing import Literal, List, Optional, Union
from pydantic import Field
from gam.ast.expr import StructLiteral
from gam.ast.base_ast import ASTNode
from gam.ast.stmt import StructDeclaration, FunctionDeclaration
from gam.ast.expr import Identifier, Property
from gam.ast.event import EventHandler
[docs]
class ForLoop(ASTNode):
"""Represents a for loop in the view.
"""
name: Literal['ForLoop'] = Field(default='ForLoop')
iterable: Identifier = Field(
description="The iterable to iterate over"
)
item: Identifier = Field(
description="The item to iterate over"
)
key: Identifier = Field(
description="The key of the item",
default=Identifier(uqn="id")
)
[docs]
class UIComponent(ASTNode):
"""Represents a UI component in the view.
Each component is a UI element that can be rendered in the view.
"""
name: Literal['UIComponent'] = Field(default='UIComponent')
component_type: str = Field(
description="Type of component (Button, Input, DataTable, etc.)"
)
model: Optional[Identifier] = Field(
default=None,
description="Instance of the model of the component. e.g. user, product, etc." \
"Idea here is to bind the component to a model instance. e.g. user.name, product.price, etc." \
"Some UI components need the data from the model to be displayed. e.g. DataTable, Dropdown, etc." \
"Should be a global variable. e.g. global.toDoList"
)
events: List[EventHandler] = Field(
default_factory=list,
description="Event handlers for this component. e.g. click, input, change, submit"
)
nested_components: List["UIComponent"] = Field(
default_factory=list,
description="Nested components in the component. e.g. Button, Input, DataTable, etc." \
"Nested components are components that are rendered inside the component." \
"And used to build complex UI components." \
"""For example:
Card(
title="User Management",
description="Manage users and their permissions",
nested_components=[
Text(text="Manage users and their permissions"),
Button(
label="Add User",
events=[EventHandler(
event=Event(event_type='click'),
handler_function=Identifier(
name='addUserHandler'
)
)]
),
]
"""
)
for_loop: Optional[ForLoop] = Field(
description="For loop to iterate over the data",
default=None
)
[docs]
class Text(UIComponent):
"""Represents a text component.
"""
name: Literal['Text'] = Field(default='Text')
component_type: Literal['text'] = Field(
default='text',
description="Type of component (Button, Input, DataTable, etc.)"
)
text: str = Field(
description="Text to display" \
"It supports interpolation of variables. e.g. {{ todo.title }}" \
"Should be a global variable. e.g. global.toDoList" \
"Or from a for loop. e.g. {{ todo.title }}"
)
type: Literal['primary', 'secondary', 'success', 'danger', 'warning', 'info'] = Field(
description="Type of the text",
default='primary'
)
size: Optional[Literal['small', 'large']] = Field(
description="Size of the text",
default=None
)
ellipsis: Optional[bool] = Field(
description="Whether the text is ellipsis",
default=False
)
[docs]
class Button(UIComponent):
"""Represents a button component.
For example:
```
Button(
component_type='button',
props=ComponentProps(properties=[
Property(name='label', value='Click me')
]),
events=[
EventHandler(
event=Event(event_type='click'),
handler_function=Identifier(name='handleClick')
)
]
)
"""
name: Literal['Button'] = Field(default='Button')
component_type: Literal['button'] = Field(
default='button',
description="Type of component (Button, Input, DataTable, etc.)"
)
label: str = Field(
description="Label of the button"
)
type: Literal['primary', 'secondary', 'success', 'danger', 'warning', 'info'] = Field(
description="Type of button",
default='primary'
)
plain: Optional[bool] = Field(
description="Whether the button is plain",
default=False
)
round: Optional[bool] = Field(
description="Whether the button is round",
default=False
)
circle: Optional[bool] = Field(
description="Whether the button is circle",
default=False
)
disabled: Optional[bool] = Field(
description="Whether the button is disabled",
default=False
)
size: Optional[Literal['small', 'large']] = Field(
description="Size of the button",
default=None
)
[docs]
class Input(UIComponent):
"""Represents an input component.
"""
name: Literal['Input'] = Field(default='Input')
component_type: Literal['input'] = Field(
default='input',
description="Type of component (Button, Input, DataTable, etc.)"
)
placeholder: Optional[str] = Field(
description="Placeholder of the input",
default=None
)
disabled: Optional[bool] = Field(
description="Whether the input is disabled",
default=False
)
size: Optional[Literal['small', 'large']] = Field(
description="Size of the input",
default=None
)
type: Literal['text', 'password', 'email', 'number', 'textarea', 'url'] = Field(
description="Type of the input",
default='text'
)
clearable: Optional[bool] = Field(
description="Whether the input is clearable",
default=False
)
auto_resize: Optional[bool] = Field(
description="Whether the input is auto-resizable",
default=False
)
[docs]
class FormItem(UIComponent):
"""Represents a form item component.
Use this to group a list/one or more of input/select/checkbox/radio components.
"""
name: Literal['FormItem'] = Field(default='FormItem')
component_type: Literal['form-item'] = Field(
default='form-item',
description="Type of component (Button, Input, DataTable, etc.)"
)
label: str = Field(
description="Label of the form item"
)
[docs]
class Card(UIComponent):
"""Represents a card component.
"""
name: Literal['Card'] = Field(default='Card')
component_type: Literal['card'] = Field(
default='card',
description="Type of component (Button, Input, DataTable, etc.)"
)
[docs]
class ViewComponent(UIComponent):
"""Represents a view component.
"""
name: Literal['ViewComponent'] = Field(default='ViewComponent')
component_type: Literal['view'] = Field(
default='view',
description="Type of component (Button, Input, DataTable, etc.)"
)
view_id: str = Field(
description="The id of the view page component. e.g. 'ToDoList', 'UserProfile', etc."
)
[docs]
class ViewPage(ASTNode):
"""Represents a view page.
"""
name: Literal['ViewPage'] = Field(default='ViewPage')
id: str = Field(
description="The id of the view. e.g. 'user_management_view'"
)
title: str = Field(
description="The title of the view. e.g. 'User Management'"
)
description: str = Field(
description="The description of the view. e.g. 'Manage users and their permissions'"
)
components: List[UIComponent] = Field(
default_factory=list,
description="Components in the view. e.g. Button, Input, DataTable, etc."
)