mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 23:15:24 +02:00
refactor: use Stimulus controller instead of inline script for chat input focus
This commit is contained in:
parent
602186f7a4
commit
d238af4573
3 changed files with 25 additions and 10 deletions
|
@ -57,4 +57,10 @@ export default class extends Controller {
|
|||
#scrollToBottom = () => {
|
||||
this.messagesTarget.scrollTop = this.messagesTarget.scrollHeight;
|
||||
};
|
||||
|
||||
focusInput() {
|
||||
if (this.hasInputTarget) {
|
||||
this.inputTarget.focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
app/javascript/controllers/focus_chat_input_controller.js
Normal file
16
app/javascript/controllers/focus_chat_input_controller.js
Normal 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();
|
||||
}
|
||||
}
|
|
@ -8,14 +8,7 @@
|
|||
<%= render "messages/chat_form", chat: @chat %>
|
||||
<% end %>
|
||||
|
||||
<%# Focus the input after form replacement %>
|
||||
<%= turbo_stream.after "chat-form" do %>
|
||||
<script>
|
||||
requestAnimationFrame(() => {
|
||||
const textarea = document.querySelector('[data-chat-target="input"]');
|
||||
if (textarea) {
|
||||
textarea.focus();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
<%# Focus the input after form replacement using Stimulus %>
|
||||
<%= turbo_stream.append "messages" do %>
|
||||
<div data-controller="focus-chat-input"></div>
|
||||
<% end %>
|
Loading…
Add table
Add a link
Reference in a new issue