Skip to content

Commit

Permalink
refactor: focusRef 유틸함수 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
YuHyun-P committed Dec 13, 2023
1 parent 3759802 commit 79dbec2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/frontend/src/components/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

import { ENTER_KEY, ESC_KEY } from "../../constants/event";
import { createClassManipulator } from "../../utils/classList";
import { focusRef } from "../../utils/refObject";

import * as styles from "./Editor.css";
import { useTextareaCursor } from "./useTextareaCursor";
Expand Down Expand Up @@ -46,7 +47,7 @@ export function Editor({ initialFile, onSubmit }: EditorProps) {
setMode("command");
setInputValue(nextInputValue);
setInputReadonly(true);
textareaRef.current?.focus();
focusRef(textareaRef);
};

const handleTextareaOnChange: ChangeEventHandler<HTMLTextAreaElement> = (
Expand All @@ -71,7 +72,7 @@ export function Editor({ initialFile, onSubmit }: EditorProps) {

setInputValue(key);
setInputReadonly(false);
inputRef.current?.focus();
focusRef(inputRef);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/frontend/src/pages/quizzes/[id].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from "../../reducers/terminalReducer";
import { Categories, Quiz, QuizGitGraphCommit } from "../../types/quiz";
import { TerminalContentType } from "../../types/terminalType";
import { focusRef } from "../../utils/refObject";
import { scrollIntoView } from "../../utils/scroll";
import { isString } from "../../utils/typeGuard";

Expand Down Expand Up @@ -112,7 +113,7 @@ export default function QuizPage({ quiz }: { quiz: Quiz }) {
fetchGitGraphDataRef?.current(numId);
terminalDispatch({ type: TerminalActionTypes.reset });
clearTextContent(terminalInputRef);
terminalInputRef.current?.focus();
focusRef(terminalInputRef);
toast.success("문제가 성공적으로 초기화되었습니다!");
} catch (error) {
toast.error(
Expand All @@ -127,13 +128,13 @@ export default function QuizPage({ quiz }: { quiz: Quiz }) {
}
terminalDispatch({ type: TerminalActionTypes.reset });
clearTextContent(terminalInputRef);
terminalInputRef.current?.focus();
focusRef(terminalInputRef);
}, [id]);

useEffect(() => {
scrollIntoView(terminalInputRef);
clearTextContent(terminalInputRef);
terminalInputRef.current?.focus();
focusRef(terminalInputRef);
}, [contentArray]);

const { barRef, topRef, handleBarHover } = useResizableSplitView();
Expand Down
5 changes: 5 additions & 0 deletions packages/frontend/src/utils/refObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RefObject } from "react";

export function focusRef<T extends HTMLOrSVGElement>(ref: RefObject<T>) {
ref.current?.focus();
}

0 comments on commit 79dbec2

Please sign in to comment.