From 8614591ffb2cb7221fa33a7258ef69d1852d27b7 Mon Sep 17 00:00:00 2001 From: Matt Lindsey Date: Sat, 7 Dec 2024 00:24:33 -0500 Subject: [PATCH] Refactor js and restore focus fix --- .../stimulus/inline_edit_controller.js | 12 ---------- app/javascript/stimulus/search_controller.js | 23 +++++++++++++++++++ app/views/messages/_nav_column.html.erb | 11 +++++---- 3 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 app/javascript/stimulus/search_controller.js diff --git a/app/javascript/stimulus/inline_edit_controller.js b/app/javascript/stimulus/inline_edit_controller.js index f4f481d1f..bd66ff18a 100644 --- a/app/javascript/stimulus/inline_edit_controller.js +++ b/app/javascript/stimulus/inline_edit_controller.js @@ -26,16 +26,4 @@ export default class extends Controller { this.inputTarget.value = this.originalText this.submitForm() } - - clear() { - this.inputTarget.value = "" - this.submitForm() - } - - search() { - clearTimeout(this.timeout) - this.timeout = setTimeout(() => { - this.element.requestSubmit() - }, 900) - } } \ No newline at end of file diff --git a/app/javascript/stimulus/search_controller.js b/app/javascript/stimulus/search_controller.js new file mode 100644 index 000000000..321076c35 --- /dev/null +++ b/app/javascript/stimulus/search_controller.js @@ -0,0 +1,23 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static targets = [ "input" ] + + cursorToEnd() { + this.inputTarget.selectionStart = + this.inputTarget.selectionEnd = + this.inputTarget.value.length + } + + clear() { + this.inputTarget.value = "" + this.element.requestSubmit() + } + + search() { + clearTimeout(this.timeout) + this.timeout = setTimeout(() => { + this.element.requestSubmit() + }, 900) + } +} \ No newline at end of file diff --git a/app/views/messages/_nav_column.html.erb b/app/views/messages/_nav_column.html.erb index a6b15932b..cfd3e5848 100644 --- a/app/views/messages/_nav_column.html.erb +++ b/app/views/messages/_nav_column.html.erb @@ -29,12 +29,13 @@ id="search" class="pt-6 text-gray-950 dark:text-gray-100 select-none" > - <%= form_with(url: new_assistant_message_path(@assistant), data: { controller: "inline-edit" }, class: "contents", method: :get) do |form| %> + <%= form_with(url: new_assistant_message_path(@assistant), data: { controller: "search" }, class: "contents", method: :get) do |form| %>
<%= form.text_field :query, value: @query, placeholder: "Search", id: "search-input", + autofocus: @query.nil? ? false : true, class: %| w-full p-2 py-1 @@ -42,10 +43,10 @@ text-black dark:text-gray-800 |, data: { - inline_edit_target: "input", + search_target: "input", action: %| - keydown.enter->inline-edit#cursorToEnd - input->inline-edit#search + keydown.enter->search#cursorToEnd + input->search#search %| } %> @@ -53,7 +54,7 @@ variant: :mini, size: 18, class: "cursor-pointer absolute right-1 top-1/2 transform -translate-y-1/2", - data: { action: "click->inline-edit#clear" } + data: { action: "click->search#clear" } %>
<% end %>