From ec2a4431be960245149fa08d104b00b585532844 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sat, 28 Jan 2023 02:15:50 +0100 Subject: [PATCH] Management: Fix cursor jumping when editing chat description (#2371) --- src/components/right/management/ManageChannel.tsx | 1 + src/components/right/management/ManageGroup.tsx | 1 + src/components/ui/TextArea.tsx | 11 +++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/components/right/management/ManageChannel.tsx b/src/components/right/management/ManageChannel.tsx index 5143cc57b..5e3feb067 100644 --- a/src/components/right/management/ManageChannel.tsx +++ b/src/components/right/management/ManageChannel.tsx @@ -240,6 +240,7 @@ const ManageChannel: FC = ({ maxLength={CHANNEL_MAX_DESCRIPTION} maxLengthIndicator={(CHANNEL_MAX_DESCRIPTION - about.length).toString()} disabled={!canChangeInfo} + noReplaceNewlines /> {chat.isCreator && ( diff --git a/src/components/right/management/ManageGroup.tsx b/src/components/right/management/ManageGroup.tsx index b57bc9860..bb166d4e7 100644 --- a/src/components/right/management/ManageGroup.tsx +++ b/src/components/right/management/ManageGroup.tsx @@ -321,6 +321,7 @@ const ManageGroup: FC = ({ onChange={handleAboutChange} value={about} disabled={!canChangeInfo} + noReplaceNewlines /> {chat.isCreator && ( diff --git a/src/components/ui/TextArea.tsx b/src/components/ui/TextArea.tsx index 1ab739f22..0efa961eb 100644 --- a/src/components/ui/TextArea.tsx +++ b/src/components/ui/TextArea.tsx @@ -28,6 +28,7 @@ type OwnProps = { onKeyDown?: (e: React.KeyboardEvent) => void; onBlur?: (e: React.FocusEvent) => void; onPaste?: (e: React.ClipboardEvent) => void; + noReplaceNewlines?: boolean; }; const TextArea: FC = ({ @@ -52,6 +53,7 @@ const TextArea: FC = ({ onKeyDown, onBlur, onPaste, + noReplaceNewlines, }) => { // eslint-disable-next-line no-null/no-null let textareaRef = useRef(null); @@ -79,11 +81,16 @@ const TextArea: FC = ({ }, []); const handleChange = useCallback((e: ChangeEvent) => { - e.currentTarget.value = e.currentTarget.value.replace(/\n/, ''); + if (!noReplaceNewlines) { + const previousSelectionEnd = e.currentTarget.selectionEnd; + // TDesktop replaces newlines with spaces as well + e.currentTarget.value = e.currentTarget.value.replace(/\n/g, ' '); + e.currentTarget.selectionEnd = previousSelectionEnd; + } e.currentTarget.style.height = '0'; e.currentTarget.style.height = `${e.currentTarget.scrollHeight}px`; onChange?.(e); - }, [onChange]); + }, [noReplaceNewlines, onChange]); return (