defmodule MvWeb.Components.SearchBarComponent do @moduledoc """ Provides the SearchBar Live-Component. - uses the DaisyUI search input field - sends search_changed event to parent live view with a query """ use MvWeb, :live_component @impl true def update(%{query: query}, socket) do socket = socket |> assign_new(:query, fn -> query || "" end) |> assign_new(:placeholder, fn -> gettext("Search...") end) {:ok, socket} end @impl true def render(assigns) do ~H"""
""" end @impl true # Function to handle the search def handle_event("search", %{"query" => q}, socket) do # Forward a high level message to the parent send(self(), {:search_changed, q}) {:noreply, assign(socket, :query, q)} end # Clears the query and resets the result set to the unfiltered list (ยง1.3). def handle_event("clear_search", _params, socket) do send(self(), {:search_changed, ""}) {:noreply, assign(socket, :query, "")} end end