diff --git a/app/components/search_list_component.html.erb b/app/components/search_list_component.html.erb index d8063b9..1d5bb5f 100644 --- a/app/components/search_list_component.html.erb +++ b/app/components/search_list_component.html.erb @@ -1,11 +1,14 @@ -<%= form if form? %> -<% if items? %> +<%= content_tag :div, data: { controller: "search-list" } do %> + <%= form if form? %>
diff --git a/app/components/search_list_form_component.html.erb b/app/components/search_list_form_component.html.erb index e6f27bd..a3c32b6 100644 --- a/app/components/search_list_form_component.html.erb +++ b/app/components/search_list_form_component.html.erb @@ -1,5 +1,5 @@ <%= form_tag class: "relative", data: { turbo_frame: "list_#{@resource.plural}" } do |f| %> - <%= f.input_search @search_field, @search_value %> + <%= f.input_search @search_field, @search_value, data: { search_list_target: 'searchField' } %> <%= f.button do %> diff --git a/app/components/search_list_item_component.rb b/app/components/search_list_item_component.rb index 1113cc6..f815549 100644 --- a/app/components/search_list_item_component.rb +++ b/app/components/search_list_item_component.rb @@ -9,30 +9,41 @@ def initialize(item, label: :name) end def active? - current_page?(kit_product_path(@item)) + @item.persisted? && current_page?(kit_product_path(@item)) end def call html_options = { aria: { label: "Edit this #{@item.model_name.singular}" }, - data: { turbo_frame: "#{@item.model_name.singular}_form" }, + data: { + turbo_frame: "#{@item.model_name.singular}_form", + action: 'click->search-list#selectItem', + search_list_target: 'item', + }, class: class_names( 'flex items-center justify-between w-full p-2 rounded', { 'bg-indigo-100': active?, + 'text-slate-300': @item.new_record?, } ), } turbo_frame_tag dom_id(@item, 'list') do content_tag :li, class: '-mx-2' do - link_to @item, html_options do - content_tag :div, class: 'flex items-center truncate' do - content_tag :div, class: 'truncate' do - content_tag :div, @label, class: 'text-sm font-medium text-slate-800' - end - end + link_to_if(@item.persisted?, item_tag, @item, html_options) do + content_tag :span, item_tag, html_options.except(:data) end end end end + + private + + def item_tag + content_tag :div, class: 'flex items-center truncate' do + content_tag :div, class: 'truncate' do + content_tag :div, @label, class: 'text-sm font-medium' + end + end + end end diff --git a/app/javascript/controllers/index.js b/app/javascript/controllers/index.js index 373c3ed..0d62f03 100644 --- a/app/javascript/controllers/index.js +++ b/app/javascript/controllers/index.js @@ -3,3 +3,6 @@ // ./bin/rails generate stimulus controllerName import { application } from "./application" + +import SearchListController from "./search_list_controller" +application.register("search-list", SearchListController) diff --git a/app/javascript/controllers/search_list_controller.js b/app/javascript/controllers/search_list_controller.js new file mode 100644 index 0000000..9973035 --- /dev/null +++ b/app/javascript/controllers/search_list_controller.js @@ -0,0 +1,21 @@ +import { Controller } from "@hotwired/stimulus" +import debounce from 'lodash/debounce'; + +// Connects to data-controller="search-list" +export default class extends Controller { + static values = { url: String }; + static targets = ['searchField', 'item']; + + connect() { + this.searchFieldTarget.addEventListener("keyup", debounce(this.search, 300)); + } + + selectItem(event) { + this.itemTargets.forEach(item => item.classList.remove('bg-indigo-100')); + event.target.closest('a').classList.add('bg-indigo-100'); + } + + search(event) { + event.target.form.requestSubmit(); + } +} diff --git a/app/views/kit/products/index.html+turbo_frame.erb b/app/views/kit/products/index.html+turbo_frame.erb index 770f9b1..d81f0fb 100644 --- a/app/views/kit/products/index.html+turbo_frame.erb +++ b/app/views/kit/products/index.html+turbo_frame.erb @@ -1,4 +1,3 @@ -<%= render SearchListComponent.new do |c| %> - <%= c.with_form(ransack: @q) %> +<%= render SearchListComponent.new(resource: @q.object.model_name) do |c| %> <%= @kit_products.each { |kit_product| c.with_item(kit_product) } %> <% end %> diff --git a/package.json b/package.json index d4df82a..236c085 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "alpinejs": "^3.10.4", "autoprefixer": "^10.4.12", "esbuild": "^0.15.12", + "lodash": "^4.17.21", "postcss": "^8.4.18", "tailwindcss": "^3.2.1" }, diff --git a/yarn.lock b/yarn.lock index 0bc50be..c77520d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -435,6 +435,11 @@ lilconfig@^2.0.5, lilconfig@^2.0.6: resolved "https://registry.yarnpkg.com/lilconfig/-/lilconfig-2.0.6.tgz#32a384558bd58af3d4c6e077dd1ad1d397bc69d4" integrity sha512-9JROoBW7pobfsx+Sq2JsASvCo6Pfo6WWoUW79HuB1BCoBXD4PLWJPqDF6fNj67pqBYTbAHkE57M1kS/+L1neOg== +lodash@^4.17.21: + version "4.17.21" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" + integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== + merge2@^1.3.0: version "1.4.1" resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"