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:
parent
602186f7a4
commit
d238af4573
3 changed files with 25 additions and 10 deletions
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
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 %>
|
<%= 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 %>
|
Loading…
Add table
Add a link
Reference in a new issue