feat: add auto scroll to editor functionality (#671)

* feat: add auto scroll to editor functionality

* test: mocked scrollIntoView function in ThreadView test cases
This commit is contained in:
Awais Ansari
2024-02-27 13:01:52 +05:00
committed by GitHub
parent ddb6c96f1d
commit 8fc666500a
3 changed files with 14 additions and 3 deletions

View File

@@ -48,7 +48,8 @@ const ActionsDropdown = ({
}
}, [actions, isPostingEnabled]);
const onClickButton = useCallback(() => {
const onClickButton = useCallback((event) => {
event.preventDefault();
setTarget(buttonRef.current);
open();
}, [open]);

View File

@@ -212,6 +212,7 @@ describe('ThreadView', () => {
})];
});
axiosMock.onGet(`${courseConfigApiUrl}${courseId}/`).reply(200, { isPostingEnabled: true });
window.HTMLElement.prototype.scrollIntoView = jest.fn();
await executeThunk(fetchCourseConfig(courseId), store.dispatch, store.getState);
await executeThunk(fetchCourseCohorts(courseId), store.dispatch, store.getState);

View File

@@ -1,4 +1,6 @@
import React, { useCallback, useContext, useRef } from 'react';
import React, {
useCallback, useContext, useEffect, useRef,
} from 'react';
import PropTypes from 'prop-types';
import { Formik } from 'formik';
@@ -35,6 +37,7 @@ const CommentEditor = ({
} = comment;
const intl = useIntl();
const editorRef = useRef(null);
const formRef = useRef(null);
const { authenticatedUser } = useContext(AppContext);
const { enableInContextSidebar } = useContext(DiscussionContext);
const userHasModerationPrivileges = useSelector(selectUserHasModerationPrivileges);
@@ -88,6 +91,12 @@ const CommentEditor = ({
// the current comment id, or the current comment parent or the curren thread.
const editorId = `comment-editor-${id || parentId || threadId}`;
useEffect(() => {
if (formRef.current) {
formRef.current.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, [formRef]);
return (
<Formik
initialValues={initialValues}
@@ -103,7 +112,7 @@ const CommentEditor = ({
handleChange,
resetForm,
}) => (
<Form onSubmit={handleSubmit} className={formClasses}>
<Form onSubmit={handleSubmit} className={formClasses} ref={formRef}>
{canDisplayEditReason && (
<Form.Group
isInvalid={isFormikFieldInvalid('editReasonCode', {