Skip to content

Commit

Permalink
design(create-question-modal): 질문 생성 모달 디자인 적용한다 (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
leegwae authored and github-actions[bot] committed Aug 23, 2023
1 parent aa65e63 commit 89a3bab
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
75 changes: 54 additions & 21 deletions src/components/panel/CreateQuestionModal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { Panel } from '@/lib/api/panel';
import type { ChangeEvent } from 'react';

import React, { useState } from 'react';
import React, { useCallback, useRef, useState } from 'react';

import { flushSync } from 'react-dom';

import { Button } from '@/components/system/Button';
import { useCreateQuestionMutation } from '@/hooks/queries/question';

interface Props {
Expand All @@ -11,6 +15,16 @@ interface Props {
export function CreateQuestionModal({ panelId, close }: Props): JSX.Element {
const [content, setContent] = useState('');
const createQuestionMutation = useCreateQuestionMutation(panelId);
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const textareaRefCallback = useCallback(
(node: HTMLTextAreaElement | null) => {
if (node) {
textareaRef.current = node;
textareaRef.current.focus();
}
},
[],
);

function handleSubmit(): void {
createQuestionMutation.mutate(
Expand All @@ -28,29 +42,48 @@ export function CreateQuestionModal({ panelId, close }: Props): JSX.Element {
);
}

function handleChange(e: ChangeEvent<HTMLTextAreaElement>): void {
flushSync(() => {
setContent(e.target.value);
});

if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
}
}

return (
<div>
<span>{content.length}</span>
<input
<div className="flex flex-col p-5">
<span className="ml-auto text-grey-dark">
<span className={content.length > 200 ? 'text-danger' : 'text-inherit'}>
{content.length}
</span>
/200자
</span>
<textarea
ref={textareaRefCallback}
className="rounded-md outline-none resize-none overflow-hidden focus:bg-off-white mt-2 mb-6 p-1"
placeholder="실시간으로 질문해보세요!"
value={content}
onChange={(e) => {
setContent(e.target.value);
}}
onChange={handleChange}
/>
<button type="button" onClick={close}>
취소
</button>
<button
type="submit"
disabled={
content.length === 0 ||
content.length > 200 ||
createQuestionMutation.isLoading
}
onClick={handleSubmit}
>
질문 생성
</button>
<div className="flex gap-4 justify-end items-center">
<Button type="button" variant="secondary" onClick={close}>
취소
</Button>
<Button
type="submit"
disabled={
content.length === 0 ||
content.length > 200 ||
createQuestionMutation.isLoading
}
onClick={handleSubmit}
>
질문 생성
</Button>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('CreatePanelModal', () => {
const contentInput = screen.getByRole('textbox');
fireEvent.change(contentInput, { target: { value: '안녕하세요' } });

expect(screen.getByText(/5/)).toBeInTheDocument();
expect(screen.getByText(/5/)).toBeInTheDocument();
});

it('사용자가 0자 혹은 200자 초과로 입력하면 제출 버튼을 비활성화한다', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
useLoaderData,
} from 'react-router-dom';

import { CreateQuestionModal } from '@/components/panel/CreateQuestionModal';
import { InfiniteQuestionList } from '@/components/panel/InfiniteQuestionList';
import { PanelHeader } from '@/components/panel/PanelHeader';
import { ModalController } from '@/components/system/ModalController';
Expand Down Expand Up @@ -51,7 +52,7 @@ export function Panel(): JSX.Element {
function handleOpenModal(): void {
overlay.open(({ close }) => (
<ModalController close={close} aria-label="질문 생성 모달">
질문 생성 모달
<CreateQuestionModal close={close} panelId={panel.id} />
</ModalController>
));
}
Expand Down

0 comments on commit 89a3bab

Please sign in to comment.