Skip to content

Commit

Permalink
CW-instant-chat-message-edit
Browse files Browse the repository at this point in the history
Added focus on open
  • Loading branch information
MeyerPV committed Oct 11, 2024
1 parent 44eef1d commit 94de9b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/shared/components/Chat/EditMessageInput/EditMessageInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState, useRef } from "react";
import React, { useEffect, useMemo, useState, useRef, useLayoutEffect } from "react";
import classNames from "classnames";
import { useCommonMembers } from "@/pages/OldCommon/hooks";
import { Loader } from "@/shared/components";
Expand All @@ -13,6 +13,7 @@ import {
import { parseStringToTextEditorValue } from "@/shared/ui-kit/TextEditor/utils";
import { emptyFunction } from "@/shared/utils";
import styles from "./EditMessageInput.module.scss";
import { BaseTextEditorHandles } from "@/shared/ui-kit/TextEditor/BaseTextEditor";

interface Props {
discussionMessage: DiscussionMessage;
Expand All @@ -29,6 +30,8 @@ export default function EditMessageInput({
isLoading,
updateMessage,
}: Props) {
const textInputRef = useRef<BaseTextEditorHandles>(null);
const editorRef = useRef<HTMLElement>(null);
const inputContainerRef = useRef<HTMLDivElement>(null);
const [message, setMessage] = useState(() =>
parseStringToTextEditorValue(discussionMessage.text),
Expand All @@ -37,6 +40,10 @@ export default function EditMessageInput({
commonId: discussionMessage.commonId,
});

useLayoutEffect(() => {
textInputRef?.current?.focus?.();
},[discussionMessage]);

const handleMessageUpdate = () => {
updateMessage(message);
};
Expand All @@ -56,6 +63,7 @@ export default function EditMessageInput({
return (
<div ref={inputContainerRef} className={styles.container}>
<BaseTextEditor
ref={textInputRef}
className={styles.input}
emojiPickerContainerClassName={styles.pickerContainer}
emojiContainerClassName={styles.emojiContainer}
Expand All @@ -66,6 +74,7 @@ export default function EditMessageInput({
onClearFinished={emptyFunction}
size={TextEditorSize.Auto}
inputContainerRef={inputContainerRef}
editorRef={editorRef}
/>

<div className={styles.buttonContainer}>
Expand Down
11 changes: 10 additions & 1 deletion src/shared/ui-kit/TextEditor/BaseTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,18 @@ const BaseTextEditor = forwardRef<BaseTextEditorHandles, TextEditorProps>((props
useImperativeHandle(ref, () => ({
focus: () => {
if (editorRef) {
const end = EditorSlate.end(editor, []);

// Move the selection to the end
Transforms.select(editor, end);

// Focus the editor DOM node
const editorEl = ReactEditor.toDOMNode(editor, editor);
editorEl.focus();
}

// Ensure the editor itself is focused programmatically
ReactEditor.focus(editor);
}
},
clear: () => {
clearInput();
Expand Down

0 comments on commit 94de9b9

Please sign in to comment.