Skip to content

Commit 4291395

Browse files
committed
feat: 그룹 생성, 수정에 사용할 form 추가
1 parent 0ba8ee7 commit 4291395

File tree

10 files changed

+238
-36
lines changed

10 files changed

+238
-36
lines changed

src/components/EditGroup/EditGroupForm.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { FC } from 'react';
2+
import GroupFormNameInput from './GroupFormNameInput';
3+
import GroupFormDescriptionInput from './GroupFormDescriptionInput';
4+
import GroupFormDateInput from './GroupFormDateInput';
5+
import UserInvite from '../common/UserInvite';
6+
import GroupFormMemoInput from './GroupFormMemoInput';
7+
8+
interface Props {
9+
name?: string;
10+
description?: string;
11+
startDate?: string;
12+
endDate?: string;
13+
memo?: string;
14+
onSubmit: React.FormEventHandler<HTMLFormElement>;
15+
}
16+
17+
const GroupForm: FC<Props> = ({
18+
name = '',
19+
description = '',
20+
startDate = new Date().toString(),
21+
endDate = new Date().toString(),
22+
memo = '',
23+
onSubmit,
24+
}) => {
25+
return (
26+
<form onSubmit={onSubmit} className="container mx-auto flex max-w-sm flex-1 flex-col gap-4 pb-[50px] pt-4">
27+
<div className="flex h-full w-full flex-1 flex-col gap-4">
28+
<GroupFormNameInput name={name} />
29+
<GroupFormDescriptionInput description={description} />
30+
<GroupFormDateInput startDate={startDate} endDate={endDate} />
31+
<UserInvite />
32+
<GroupFormMemoInput memo={memo} />
33+
</div>
34+
<button type="submit" className="btn btn-outline btn-primary w-full">
35+
저장
36+
</button>
37+
</form>
38+
);
39+
};
40+
41+
export default GroupForm;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { FC } from 'react';
2+
import { dateToYYMMDD } from '@/utils/dateUtils';
3+
4+
interface Props {
5+
startDate: string;
6+
endDate: string;
7+
}
8+
9+
const GroupFormDateInput: FC<Props> = ({ startDate, endDate }) => {
10+
return (
11+
<div className="w-full">
12+
<label className="label label-text w-full text-start">모임 설정 기간 *</label>
13+
<div className="input input-bordered flex w-full flex-row items-center gap-1 p-0">
14+
<label className="hidden" htmlFor="group-start-date-input">
15+
모임 시작
16+
</label>
17+
<input
18+
className="input border-none"
19+
type="date"
20+
defaultValue={dateToYYMMDD(new Date(startDate))}
21+
id="group-start-date-input"
22+
name="startDate"
23+
/>
24+
~
25+
<label className="hidden" htmlFor="group-end-date-input">
26+
모임 종료
27+
</label>
28+
<input
29+
className="input border-none"
30+
type="date"
31+
id="group-end-date-input"
32+
defaultValue={dateToYYMMDD(new Date(endDate))}
33+
name="endDate"
34+
/>
35+
</div>
36+
</div>
37+
);
38+
};
39+
40+
export default GroupFormDateInput;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FC } from 'react';
2+
import InputForm from '../common/InputForm';
3+
4+
interface Props {
5+
description: string;
6+
}
7+
8+
const GroupFormDescriptionInput: FC<Props> = ({ description }) => {
9+
return (
10+
<InputForm
11+
id="group-description-input"
12+
type="text"
13+
defaultValue={description}
14+
placeholder="모임 설명을 입력하세요"
15+
onChange={() => {}}
16+
title="모임 설명"
17+
hint=""
18+
name="description"
19+
/>
20+
);
21+
};
22+
23+
export default GroupFormDescriptionInput;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { FC } from 'react';
2+
3+
interface Props {
4+
memo: string;
5+
}
6+
7+
const GroupFormMemoInput: FC<Props> = ({ memo }) => {
8+
return (
9+
<div>
10+
<label className="label label-text w-full text-start">메모</label>
11+
<textarea
12+
id="group-memo-input"
13+
className="input input-bordered h-24 w-full"
14+
rows={10}
15+
placeholder="메모를 입력하세요"
16+
defaultValue={memo}
17+
onChange={() => {}}
18+
title="메모"
19+
name="memo"
20+
/>
21+
<p className="label-text-alt">최대 500자 까지 입력할 수 있습니다.</p>
22+
</div>
23+
);
24+
};
25+
26+
export default GroupFormMemoInput;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { FC } from 'react';
2+
import InputForm from '../common/InputForm';
3+
4+
interface Props {
5+
name: string;
6+
}
7+
8+
const GroupFormNameInput: FC<Props> = ({ name = '' }) => {
9+
return (
10+
<InputForm
11+
id="group-name-input"
12+
type="text"
13+
defaultValue={name}
14+
placeholder="모임명을 입력하세요"
15+
onChange={() => {}}
16+
title="모임명 *"
17+
hint=""
18+
name="name"
19+
/>
20+
);
21+
};
22+
23+
export default GroupFormNameInput;

src/tests/EditGrouptForm.test.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/tests/GroupForm.test.tsx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import EditGroupForm from '@/components/GroupForm/GroupForm';
2+
import { dateToYYMMDD } from '@/utils/dateUtils';
3+
import { act, render, screen } from '@testing-library/react';
4+
import userEvent from '@testing-library/user-event';
5+
import { describe, expect, it } from 'vitest';
6+
7+
describe('EditGroupForm', () => {
8+
it('수정하는 경우 기본값을 전달할 수 있어야 한다.', () => {
9+
const startDate = new Date().toString();
10+
const endDate = new Date().toString();
11+
12+
render(
13+
<EditGroupForm
14+
onSubmit={() => {}}
15+
name="name"
16+
description="description"
17+
startDate={startDate}
18+
endDate={endDate}
19+
memo="memo"
20+
/>,
21+
);
22+
const groupNameInput = screen.getByPlaceholderText(//) as HTMLInputElement;
23+
const groupDescriptionInput = screen.getByPlaceholderText(/ /) as HTMLInputElement;
24+
const groupDateStartInput = screen.getByLabelText(/ /) as HTMLInputElement;
25+
const groupDateEndInput = screen.getByLabelText(/ /) as HTMLInputElement;
26+
const groupMemoInput = screen.getByPlaceholderText(//) as HTMLInputElement;
27+
28+
expect(groupNameInput.value).toBe('name');
29+
expect(groupDescriptionInput.value).toBe('description');
30+
expect(groupDateStartInput.value).toBe(dateToYYMMDD(new Date(startDate)));
31+
expect(groupDateEndInput.value).toBe(dateToYYMMDD(new Date(endDate)));
32+
expect(groupMemoInput.value).toBe('memo');
33+
});
34+
it('모임명, 설명, 메모를 수정할 수 있어야 한다.', async () => {
35+
const user = userEvent.setup();
36+
render(<EditGroupForm onSubmit={() => {}} />);
37+
38+
const groupNameInput = screen.getByPlaceholderText(//) as HTMLInputElement;
39+
const groupDescriptionInput = screen.getByPlaceholderText(/ /) as HTMLInputElement;
40+
const groupDateStartInput = screen.getByLabelText(/ /) as HTMLInputElement;
41+
const groupDateEndInput = screen.getByLabelText(/ /) as HTMLInputElement;
42+
const groupMemoInput = screen.getByPlaceholderText(//) as HTMLInputElement;
43+
44+
expect(groupNameInput).toBeInTheDocument();
45+
expect(groupDescriptionInput).toBeInTheDocument();
46+
expect(groupDateStartInput).toBeInTheDocument();
47+
expect(groupDateEndInput).toBeInTheDocument();
48+
expect(groupMemoInput).toBeInTheDocument();
49+
50+
await act(async () => {
51+
// 기존 값이 존재하는 경우 type을 하게 되면 기존 값에 이를 추가하려고 함.
52+
// 그러므로 clear를 통해 기존 날짜 값을 초기화 해줘야 함.
53+
await user.clear(groupDateStartInput);
54+
await user.clear(groupDateEndInput);
55+
56+
await user.type(groupNameInput, 'name');
57+
await user.type(groupDescriptionInput, 'description');
58+
await user.type(groupMemoInput, 'memo');
59+
await user.type(groupDateStartInput, '2022-01-01');
60+
await user.type(groupDateEndInput, '2022-01-02');
61+
});
62+
63+
expect(groupNameInput).toHaveValue('name');
64+
expect(groupDescriptionInput).toHaveValue('description');
65+
expect(groupMemoInput).toHaveValue('memo');
66+
expect(groupDateStartInput).toHaveValue('2022-01-01');
67+
expect(groupDateEndInput).toHaveValue('2022-01-02');
68+
});
69+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { dateToYYMMDD } from '../dateUtils';
3+
4+
describe('dateUtils', () => {
5+
it('should return a date string', () => {
6+
const date = new Date('2021-09-01');
7+
const result = dateToYYMMDD(date);
8+
expect(result).toBe('2021-09-01');
9+
});
10+
});

src/utils/dateUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const dateToYYMMDD = (date: Date) => {
2+
const year = date.getFullYear();
3+
const month = date.getMonth() + 1;
4+
const day = date.getDate();
5+
return `${year}-${month < 10 ? `0${month}` : month}-${day < 10 ? `0${day}` : day}`;
6+
};

0 commit comments

Comments
 (0)