Improved Command class management.

This commit is contained in:
2025-12-19 21:12:55 +01:00
parent b26abc4257
commit 1347f12618
23 changed files with 349 additions and 169 deletions

View File

@@ -1,7 +1,7 @@
from fasthtml.components import *
from myfasthtml.core.bindings import Binding
from myfasthtml.core.commands import Command
from myfasthtml.core.commands import Command, CommandTemplate
from myfasthtml.core.utils import merge_classes
@@ -14,7 +14,7 @@ class Ids:
class mk:
@staticmethod
def button(element, command: Command = None, binding: Binding = None, **kwargs):
def button(element, command: Command | CommandTemplate = None, binding: Binding = None, **kwargs):
"""
Defines a static method for creating a Button object with specific configurations.
@@ -33,7 +33,7 @@ class mk:
@staticmethod
def dialog_buttons(ok_title: str = "OK",
cancel_title: str = "Cancel",
on_ok: Command = None,
on_ok: Command | CommandTemplate = None,
on_cancel: Command = None,
cls=None):
return Div(
@@ -52,7 +52,7 @@ class mk:
can_hover=False,
tooltip=None,
cls='',
command: Command = None,
command: Command | CommandTemplate = None,
binding: Binding = None,
**kwargs):
"""
@@ -92,7 +92,7 @@ class mk:
icon=None,
size: str = "sm",
cls='',
command: Command = None,
command: Command | CommandTemplate = None,
binding: Binding = None,
**kwargs):
merged_cls = merge_classes("flex", cls, kwargs)
@@ -109,7 +109,10 @@ class mk:
replace("xl", "32"))
@staticmethod
def manage_command(ft, command: Command):
def manage_command(ft, command: Command | CommandTemplate):
if isinstance(command, CommandTemplate):
command = command.command
if command:
ft = command.bind_ft(ft)
@@ -130,7 +133,7 @@ class mk:
return ft
@staticmethod
def mk(ft, command: Command = None, binding: Binding = None, init_binding=True):
def mk(ft, command: Command | CommandTemplate = None, binding: Binding = None, init_binding=True):
ft = mk.manage_command(ft, command) if command else ft
ft = mk.manage_binding(ft, binding, init_binding=init_binding) if binding else ft
return ft