1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 07:25:19 +02:00

refactor: use Stimulus controller instead of inline script for chat input focus

This commit is contained in:
Huy Nguyen Quang 2025-06-12 09:34:04 +07:00
parent 602186f7a4
commit d238af4573
3 changed files with 25 additions and 10 deletions

View file

@ -57,4 +57,10 @@ export default class extends Controller {
#scrollToBottom = () => { #scrollToBottom = () => {
this.messagesTarget.scrollTop = this.messagesTarget.scrollHeight; this.messagesTarget.scrollTop = this.messagesTarget.scrollHeight;
}; };
focusInput() {
if (this.hasInputTarget) {
this.inputTarget.focus();
}
}
} }

View file

@ -0,0 +1,16 @@
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
connect() {
const chatController = this.application.getControllerForElementAndIdentifier(
document.querySelector('[data-controller~="chat"]'),
"chat"
);
if (chatController) {
chatController.focusInput();
}
this.element.remove();
}
}

View file

@ -8,14 +8,7 @@
<%= render "messages/chat_form", chat: @chat %> <%= render "messages/chat_form", chat: @chat %>
<% end %> <% end %>
<%# Focus the input after form replacement %> <%# Focus the input after form replacement using Stimulus %>
<%= turbo_stream.after "chat-form" do %> <%= turbo_stream.append "messages" do %>
<script> <div data-controller="focus-chat-input"></div>
requestAnimationFrame(() => {
const textarea = document.querySelector('[data-chat-target="input"]');
if (textarea) {
textarea.focus();
}
});
</script>
<% end %> <% end %>