Adding search control

This commit is contained in:
2025-11-15 23:49:37 +01:00
parent 5ee671c6df
commit 09c4217cb6
4 changed files with 69 additions and 4 deletions

View File

@@ -0,0 +1,55 @@
import logging
from fasthtml.components import *
from myfasthtml.controls.BaseCommands import BaseCommands
from myfasthtml.controls.helpers import Ids, mk
from myfasthtml.core.commands import Command
from myfasthtml.core.instances import MultipleInstance
logger = logging.getLogger("Search")
class Commands(BaseCommands):
def search(self):
return (Command("Search", f"Search {self._owner.items_names}", self._owner.search).
htmx(trigger="keyup changed delay:300ms"))
class Search(MultipleInstance):
def __init__(self, session, items_names=None, template=None, _id=None):
super().__init__(session, Ids.Search, _id=_id)
self.items_names = items_names
self.items = []
self.filtered = []
self.template = template or Div
self.commands = Commands(self)
def set_items(self, items):
self.items = items
self.filtered = self.items
return self
def search(self, query):
logger.debug(f"search {query=}")
pass
def _mk_search_results(self):
return [self.template(item) for item in self.filtered]
def render(self):
return Div(
# mk.mk(Input(name="query", id=f"{self._id}-search", type="text", placeholder="Search...", cls="input input-xs"),
# command=self.commands.search()),
mk.mk(Input(name="query", id=f"{self._id}-search", type="search", placeholder="Search...", cls="input input-xs"),
),
Div(
*self._mk_search_results(),
id=f"{self._id}-results",
cls="mf-search-results",
),
id=f"{self._id}",
)
def __ft__(self):
return self.render()