Skip to content

Commit

Permalink
Merge branch 'pr-562-fix-reflow' from 'ZihaoZhou:main'
Browse files Browse the repository at this point in the history
  • Loading branch information
zewebdev1337 committed May 28, 2024
2 parents baf5431 + e2e743a commit d83ce66
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/components/Chat/ChatContent/Message/View/EditView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const EditView = ({
content,
setIsEdit,
messageIndex,
sticky,
sticky
}: {
content: string;
setIsEdit: React.Dispatch<React.SetStateAction<boolean>>;
Expand All @@ -28,6 +28,7 @@ const EditView = ({
const [_content, _setContent] = useState<string>(content);
const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const textareaRef = React.createRef<HTMLTextAreaElement>();
const bufferRef = React.createRef<HTMLTextAreaElement>();

const { t } = useTranslation();

Expand Down Expand Up @@ -105,18 +106,23 @@ const EditView = ({
handleSubmit();
};

useEffect(() => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
const adjustTextareaHeight = () => {
if (textareaRef.current && bufferRef.current) {
const textarea = textareaRef.current;
const buffer = bufferRef.current;

buffer.style.height = 'auto';
buffer.style.height = buffer.scrollHeight + 'px';
textarea.style.height = buffer.scrollHeight + 'px';
}
};

useEffect(() => {
adjustTextareaHeight();
}, [_content]);

useEffect(() => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
}
adjustTextareaHeight();
}, []);

return (
Expand All @@ -128,6 +134,7 @@ const EditView = ({
: ''
}`}
>
<div className="relative">
<textarea
ref={textareaRef}
className='m-0 resize-none rounded-lg bg-transparent overflow-y-hidden focus:ring-0 focus-visible:ring-0 leading-7 w-full placeholder:text-gray-500/40'
Expand All @@ -139,6 +146,19 @@ const EditView = ({
onKeyDown={handleKeyDown}
rows={1}
></textarea>
<textarea
ref={bufferRef}
className='m-0 resize-none rounded-lg bg-transparent overflow-y-hidden focus:ring-0 focus-visible:ring-0 leading-7 w-full placeholder:text-gray-500/40 absolute top-0 left-0'
style={{
position: 'absolute',
visibility: 'hidden',
overflow: 'hidden'
}}
value={_content}
readOnly
rows={1}
></textarea>
</div>
</div>
<EditViewButtons
sticky={sticky}
Expand Down

0 comments on commit d83ce66

Please sign in to comment.