First implementation of bindings
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
from fasthtml.components import *
|
||||
|
||||
from myfasthtml.core.commands import Command
|
||||
|
||||
|
||||
def mk_button(element, command: Command = None, **kwargs):
|
||||
if command is None:
|
||||
return Button(element, **kwargs)
|
||||
|
||||
htmx = command.get_htmx_params()
|
||||
return Button(element, **htmx, **kwargs)
|
||||
55
src/myfasthtml/controls/helpers.py
Normal file
55
src/myfasthtml/controls/helpers.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from fasthtml.components import *
|
||||
|
||||
from myfasthtml.core.bindings import Binding
|
||||
from myfasthtml.core.commands import Command
|
||||
from myfasthtml.core.utils import merge_classes
|
||||
|
||||
|
||||
class mk:
|
||||
|
||||
@staticmethod
|
||||
def button(element, command: Command = None, binding: Binding = None, **kwargs):
|
||||
return mk.mk(Button(element, **kwargs), command=command, binding=binding)
|
||||
|
||||
@staticmethod
|
||||
def icon(icon, size=20,
|
||||
can_select=True,
|
||||
can_hover=False,
|
||||
cls='',
|
||||
command: Command = None,
|
||||
binding: Binding = None,
|
||||
**kwargs):
|
||||
merged_cls = merge_classes(f"mf-icon-{size}",
|
||||
'icon-btn' if can_select else '',
|
||||
'mmt-btn' if can_hover else '',
|
||||
cls,
|
||||
kwargs)
|
||||
|
||||
return mk.mk(Div(icon, cls=merged_cls, **kwargs), command=command, binding=binding)
|
||||
|
||||
@staticmethod
|
||||
def manage_command(ft, command: Command):
|
||||
if command:
|
||||
ft = command.bind_ft(ft)
|
||||
|
||||
return ft
|
||||
|
||||
@staticmethod
|
||||
def manage_binding(ft, binding: Binding, ft_attr=None, init_binding=True):
|
||||
if not binding:
|
||||
return ft
|
||||
|
||||
binding.bind_ft(ft, ft_attr)
|
||||
if init_binding:
|
||||
binding.init()
|
||||
# as it is the first binding, remove the hx-swap-oob
|
||||
if "hx-swap-oob" in ft.attrs:
|
||||
del ft.attrs["hx-swap-oob"]
|
||||
|
||||
return ft
|
||||
|
||||
@staticmethod
|
||||
def mk(ft, command: Command = None, binding: Binding = None, init_binding=True):
|
||||
ft = mk.manage_command(ft, command)
|
||||
ft = mk.manage_binding(ft, binding, init_binding=init_binding)
|
||||
return ft
|
||||
Reference in New Issue
Block a user