Add the BookSearch component.
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-04-03 21:46:59 +03:00
parent 877bc63f9b
commit 73f69a72a5
3 changed files with 145 additions and 1 deletions
@@ -4,6 +4,7 @@ defmodule DecentralisedBookIndexWeb.Components.MyComponents do
defmacro __using__(_) do
quote do
import MyComponents.BookCard, only: [book_card: 1]
import MyComponents.BookSearch, only: [book_search: 1]
import MyComponents.AuthorCard, only: [author_card: 1]
import MyComponents.AuthorSearch, only: [author_search: 1]
@@ -0,0 +1,95 @@
defmodule DecentralisedBookIndexWeb.Components.MyComponents.BookSearch do
use Phoenix.Component
use DecentralisedBookIndexWeb, :verified_routes
attr :search_query, :string, default: ""
attr :search_mode, :string, default: "title"
attr :search_mode_options, :list, required: true
attr :select_options, :list, required: true
attr :selected_option, :string, required: true
def book_search(assigns) do
~H"""
<div class="relative bg-white shadow-md dark:bg-gray-800 sm:rounded-lg">
<div class="flex flex-col items-center justify-between p-4 space-y-3 md:flex-row md:space-y-0 md:space-x-4">
<div class="w-full md:w-1/2">
<form class="flex items-center">
<label for="simple-search" class="sr-only">Search</label>
<div class="relative w-full">
<div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
<svg aria-hidden="true" class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
</svg>
</div>
<input name="query" type="text" id="simple-search" class="block w-full p-2 pl-10 text-sm text-gray-900 border border-gray-300 rounded-lg bg-gray-50 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="Search" value={@search_query}>
</div>
</form>
</div>
<div class="flex flex-col items-stretch justify-end flex-shrink-0 w-full space-y-2 md:w-auto md:flex-row md:space-y-0 md:items-center md:space-x-3">
<.link patch={~p"/books/new"}>
<button type="button" class="flex items-center justify-center px-4 py-2 text-sm font-medium text-black dark:text-white rounded-lg bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 focus:outline-none dark:focus:ring-primary-800">
<svg class="h-3.5 w-3.5 mr-2" fill="currentColor" viewbox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path clip-rule="evenodd" fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" />
</svg>
Add book
</button>
</.link>
<.dropdown_settings
search_mode={@search_mode}
search_mode_options={@search_mode_options}
select_options={@select_options}
selected_option={@selected_option}
/>
</div>
</div>
</div>
"""
end
attr :search_mode, :string, default: "title"
attr :search_mode_options, :list, required: true
attr :select_options, :list, required: true
attr :selected_option, :string, required: true
def dropdown_settings(assigns) do
~H"""
<div class="flex justify-end px-4">
<button id="dropdownButtonSearchBook" data-dropdown-toggle="dropdownSearchBook" class="inline-block text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-1.5" type="button">
<span class="sr-only">Open dropdown</span>
<svg class="w-5 h-5 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
<path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M20 6H10m0 0a2 2 0 1 0-4 0m4 0a2 2 0 1 1-4 0m0 0H4m16 6h-2m0 0a2 2 0 1 0-4 0m4 0a2 2 0 1 1-4 0m0 0H4m16 6H10m0 0a2 2 0 1 0-4 0m4 0a2 2 0 1 1-4 0m0 0H4"/>
</svg>
</button>
<!-- Dropdown menu -->
<div id="dropdownSearchBook" class="z-10 hidden text-base list-none bg-white divide-y divide-gray-100 rounded-lg shadow-sm w-44 dark:bg-gray-700">
<div class="flex items-center w-full space-x-3 md:w-auto">
<form phx-change="change-search-mode">
<select name="search_mode" id="search_mode" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<%= for {name, value} <- @search_mode_options do %>
<%= if value == @search_mode do %>
<option value={value} selected>{name}</option>
<% else %>
<option value={value}>{name}</option>
<% end %>
<% end %>
</select>
</form>
</div>
<div class="flex items-center w-full space-x-3 md:w-auto">
<form phx-change="change-sort">
<select name="sort_by" id="sort_by" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<%= for {name, value} <- @select_options do %>
<%= if value == @selected_option do %>
<option value={value} selected>{name}</option>
<% else %>
<option value={value}>{name}</option>
<% end %>
<% end %>
</select>
</form>
</div>
</div>
</div>
"""
end
end