Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 날짜를 선택할 수 있는 캘린더 UI 구현 #68

Merged
merged 12 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions frontend/.storybook/preview.ts

This file was deleted.

28 changes: 28 additions & 0 deletions frontend/.storybook/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Global, ThemeProvider } from '@emotion/react';
import type { Preview } from '@storybook/react';
import React from 'react';

import globalStyles from '../src/styles/global';
import theme from '../src/styles/theme';

const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
},
};

export default preview;

export const decorators = [
(Story) => (
<ThemeProvider theme={theme}>
<Global styles={globalStyles} />
<Story />
</ThemeProvider>
),
];
64 changes: 64 additions & 0 deletions frontend/src/components/_common/Calendar/Calendar.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { Meta, StoryObj } from '@storybook/react';
import { useState } from 'react';

import Button from './index';
import Calendar from './index';

const meta = {
title: 'Components/Calendar',
component: Calendar,
tags: ['autodocs'],

parameters: {
layout: 'centered',
},
argTypes: {
hasDate: {
description: '선택된 날짜들',
type: 'function',
control: {
disable: true,
},
},
onDateClick: {
description: '선택된 날짜 리스트에 특정 날짜를 추가하거나 제거할 수 있는 함수',
},
},
decorators: [
(Story, context) => {
const [selectedDates, setSelectedDates] = useState<string[]>([]);

const hasDate = (date: string) => selectedDates.includes(date);

const handleDateClick = (date: string) => {
setSelectedDates((prevDates) =>
hasDate(date) ? prevDates.filter((d) => d !== date) : [...prevDates, date],
);
};

return (
<Story
args={{
...context.args,
hasDate,
onDateClick: handleDateClick,
}}
/>
);
},
],
} satisfies Meta<typeof Button>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Playground: Story = {
args: {
hasDate: () => false,
onDateClick: () => {},
},
render: (args) => {
return <Calendar hasDate={args.hasDate} onDateClick={args.onDateClick} />;
},
};
86 changes: 86 additions & 0 deletions frontend/src/components/_common/Calendar/Calendar.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { css } from '@emotion/react';

export const s_calendarContainer = css`
display: flex;
flex-direction: column;
align-items: center;
`;

export const s_calendarContent = css`
display: grid;
grid-auto-rows: 40px;
grid-template-columns: repeat(7, 1fr);
width: 100%;
`;

export const s_dayOfWeekContainer = css`
margin-bottom: 2rem;
`;

export const s_dayOfWeek = css`
display: flex;
align-items: center;
justify-content: center;

width: 100%;
min-width: 40px;
height: 40px;
min-height: 40px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2]

여기 군데군데 px로 구현하신 부분이 보이는 것 같습니다..! rem으로 통일하는 것이 좋아보여서 리뷰 남깁니다 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rem으로 통일하기로 했는데 완전히 잊고 있었네요..수정할게요!!


font-size: 1.2rem;
font-weight: normal;
color: gray;
`;

export const s_monthHeader = css`
display: flex;
align-items: center;
justify-content: space-between;

width: 100%;
margin-bottom: 20px;
padding: 0 10px;

font-size: 1.5rem;
font-weight: bold;
`;

export const s_monthNavigation = css`
cursor: pointer;
`;

export const s_daySlotButton = css`
display: flex;
align-items: center;
justify-content: center;

width: 100%;
min-width: 36px;
height: 36px;

background-color: transparent;
border: none;
`;

// TODO : 공휴일 색 변경 논의 필요(@해리)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오👍👍 TODO 코멘트에 작업자 멘션한 부분 좋네요!!
멘션할 때 Giuhub id로 하는 방법도 있는 것 같아요! 자동 완성 할 수 장점이 있습니다 ㅎㅎ
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오...좋은데요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

익스텐션 뭐죵!?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hwinkr 혹시 자동완성 익스텐션이 있어야 가능한가요? 지금 해리는 안 되나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안돼요...ㅋㅋㅋ

// TODO : s_todayDaySlot 추가 예정(@해리)
export const s_daySlot = (isHoliday: boolean) => css`
cursor: pointer;
font-size: 1.5rem;
color: ${isHoliday ? 'red' : '#000'};
`;

export const s_selectedDaySlot = (isSelected: boolean) => css`
display: flex;
align-items: center;
justify-content: center;

width: 36px;
height: 36px;

${isSelected &&
css`
background-color: #fcc;
border-radius: 50%;
`}
`;
65 changes: 65 additions & 0 deletions frontend/src/components/_common/Calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import useCalendarInfo from '@hooks/useCalendarInfo/useCalendarInfo';

import {
s_calendarContainer,
s_calendarContent,
s_dayOfWeek,
s_dayOfWeekContainer,
s_daySlot,
s_daySlotButton,
s_monthHeader,
s_monthNavigation,
s_selectedDaySlot,
} from './Calendar.styles';

const DAY_OF_WEEK = ['월', '화', '수', '목', '금', '토', '일'] as const;

// TODO : 선택된 날짜에 대한 강조 색을 외부에서 주입받을 수 있도록 props 수정 예정 (@해리)
interface CalendarProps {
hasDate: (date: string) => boolean;
onDateClick: (date: string) => void;
}

export default function Calendar({ hasDate, onDateClick }: CalendarProps) {
const { yearMonthInfo, handleGetDayInfo, handlePrevMonth, handleNextMonth } = useCalendarInfo();
const { year, month, daySlotCount } = yearMonthInfo;

return (
<div css={s_calendarContainer} aria-label={`${year}년 ${month}월 달력`}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aria-label 명시.. 👍 대 해 리

<header css={s_monthHeader}>
{/* TODO : 캘린더 헤더 버튼 스타일 수정 예정 (@해리) */}
<button css={s_monthNavigation} onClick={handlePrevMonth}>
{'<'}
</button>
<span>
{year}년 {month}월
</span>
<button css={s_monthNavigation} onClick={handleNextMonth}>
{'>'}
</button>
</header>
<section css={[s_calendarContent, s_dayOfWeekContainer]}>
{DAY_OF_WEEK.map((day) => (
<div key={day} css={s_dayOfWeek}>
{day}
</div>
))}
</section>
<section css={s_calendarContent}>
{Array.from({ length: daySlotCount }, (_, index) => {
// TODO : isToday 변수를 활용한 스타일 변경 논의 필요 (@해리)
const { date, dateString, isDate, isToday, isHoliday } = handleGetDayInfo(index);
const isSelectedDate = hasDate(dateString);

return (
<button key={dateString} onClick={() => onDateClick(dateString)} css={s_daySlotButton}>
<span css={[s_daySlot(isHoliday), s_selectedDaySlot(isSelectedDate)]}>
{isDate ? date : ''}
</span>
</button>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3]
날짜 선택을 할 수 없는 첫 줄의 빈 공간에도 button이 삽입되는데, 웹 표준에 맞게 구현하기 위해서는 버튼이 없도록 만들어야 할 것 같아요! 급한 것은 아니니 추후에 수정해도 괜찮구요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh...모든 요소들을 다 버튼으로 하니 날짜가 아닌 경우에도 cursor : pointer 속성이 적용되고 있었네요...! 이 부분은 접근성을 위해서라도 빠르게 수정할게요!!

);
})}
</section>
</div>
);
}
51 changes: 51 additions & 0 deletions frontend/src/hooks/useCalendarInfo/useCalendarInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useState } from 'react';

import { getDayInfo, getYearMonthInfo } from './useCalendarInfo.utils';

export default function useCalendarInfo() {
// TODO : L7 ~ L9 getCurrentDate 함수로 추상화 예정(@해리)
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const currentMonth = currentDate.getMonth() + 1;

const [year, setYear] = useState(currentYear);
const [month, setMonth] = useState(currentMonth);

const { firstDayIndex, daySlotCount } = getYearMonthInfo(year, month);

const handleGetDayInfo = (index: number) => {
return getDayInfo(year, month, firstDayIndex, index, currentDate);
};

const handlePrevMonth = () => {
// TODO : isCurrentDate 함수로 추상화(@해리)
if (year === currentYear && month === currentMonth) return;

if (month === 1) {
setYear(year - 1);
setMonth(12); // TODO : 상수화(@해리)
} else {
setMonth(month - 1);
}
};

const handleNextMonth = () => {
if (month === 12) {
setYear(year + 1);
setMonth(1);
} else {
setMonth(month + 1);
}
};

return {
yearMonthInfo: {
year,
month,
daySlotCount,
},
handleGetDayInfo,
handlePrevMonth,
handleNextMonth,
} as const;
}
38 changes: 38 additions & 0 deletions frontend/src/hooks/useCalendarInfo/useCalendarInfo.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export function getYearMonthInfo(year: number, month: number) {
const startDate = new Date(year, month - 1, 1);

/*
로직 설명(@해리)
- 월요일을 index 0으로 변경하기 위해서 나머지 연산자를 활용한다.
- 자바스크립트 데이트 객체는 기본적으로 일요일이 인덱스가 0인데, 모모 달력은 월요일을 인덱스를 0으로 만들어줘야 한다.
- 따라서, 특정 달의 시작 날짜에 대한 인덱스에 6을 더해주고 7로 나눈 나머지를 사용하는 것으로 구현했다.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로직 주석으로 설명을 해두니 이해하기 굉장히 편하네요..! 👍

const firstDayIndex = (startDate.getDay() + 6) % 7;

const endDate = new Date(year, month, 0);
const lastDate = endDate.getDate();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2]

endDatelastDate 변수명은 의미가 유사하여 쉽게 파악하기 어려운 것 같습니다.

const lastDayOfMonthDate = new Date(year, month, 0); // 해당 월의 마지막 날짜 객체
const lastDayNumber = lastDayOfMonthDate.getDate(); // 마지막 날짜의 일(day) 값

이런 느낌으로 좀 더 네이밍을 명확하게 바꿔보면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 보니까, 이해하기 힘든 것 같긴 하군요...빙봉 추천대로 수정할게요~


const daySlotCount = firstDayIndex + lastDate;

return { year, month, firstDayIndex, daySlotCount } as const;
}

export function getDayInfo(
year: number,
month: number,
firstDayIndex: number,
index: number,
currentDate: Date,
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3]

getDayInfo가 받는 인자가 꽤 많긴 하네요..! 혹시 의도된 부분일까요..?

아니라면 추후에 더 원활히 사용할 수 있도록 객체형태로 받아도 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 날짜와 관련된 정보들이 꽤 많았는데,

  • 공휴일인지
  • 선택된 날짜인지
  • index를 사용하는 것에서 실제 날짜로 표시해야 하거나
  • 날짜인지 아닌지 즉, 빈칸으로 표시해야하는 것인지 아닌지
  • 오늘인지 아닌지
    등등,,,

여러개의 함수로 분리해서 한 날짜와 관련된 정보를 구할 수 있겠지만, 날짜와 관련된 정보라는 하나의 관심사로 보고 함수의 역할이 조금 많은 것 같더라도 한 함수에서 날짜와 관련된 정보를 구하도록 했습니다~ 그래서 인자도 자연스럽게 많아진 것 같아요. 인자가 많은 경우, 객체를 사용하지 않으면 인자 전달 순서를 모두 기억하고 있어야 하니 낙타 말 처럼, 객체 형태로 바꾸는 것이 좋을 것 같네요!

const date = index - firstDayIndex + 1;
const isDate = index >= firstDayIndex;

const dateString = `${year}-${month}-${date}`;
const todayString = `${year}-${month}-${currentDate.getDate()}`;
const isToday = dateString === todayString;
// TODO : 일단은 일요일만 isHolday로 설정되도록 구현, 추후에 진짜 공휴일도 포함할 것인지 논의 필요, 찾아보니 라이브러리가 있더라구요.(@해리)
// TODO : 매직넘버 의미있는 상수화 필요(@해리)
const isHoliday = index % 7 === 6;

return { date, dateString, isDate, isToday, isHoliday } as const;
}
4 changes: 2 additions & 2 deletions frontend/src/hooks/useTimePick/useTimePick.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function isMouseEvent(event: Event): event is MouseEvent {
return event instanceof MouseEvent;
}

const decideDragEventTarget = (event: Event) => {
export function decideDragEventTarget(event: Event) {
let target;

if (isTouchEvent(event) && event.touches) {
Expand All @@ -17,7 +17,7 @@ const decideDragEventTarget = (event: Event) => {
}

return target;
};
}

export function getTableCellElement(event: Event): HTMLTableCellElement | null {
const targetElement = decideDragEventTarget(event);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/styles/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ const globalStyles = css`

button {
cursor: pointer;
border: none;
}
`;

Expand Down
Loading