Skip to content

Commit 212d5a8

Browse files
committed
feat : value 날짜 버튼 활성화
1 parent f0b7c75 commit 212d5a8

File tree

4 files changed

+48
-59
lines changed

4 files changed

+48
-59
lines changed

package/src/assets/ReactDatepicker.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
height: 36px;
7171
cursor: pointer;
7272
}
73+
.react-datepicker__datepicker-button[data-active='true'] {
74+
background-color: #333;
75+
color: #fff;
76+
}
7377
.react-datepicker__datepicker-button:hover {
7478
background-color: #f7f7f7;
7579
}

package/src/components/Container.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,6 @@ function Container({ initValue = new Date(), onChange }: Iprops) {
8585
}
8686
}, [value, onChange]);
8787

88-
// const [centuryPage, setCenturyPage] = useState<number>(0);
89-
// const [decadePage, setDecadePage] = useState<number>(0);
90-
// const [yearPage, setYearPage] = useState<number>(0);
91-
// const [monthPage, setMonthPage] = useState<number>(0);
92-
93-
// 2041-07-15
94-
// centuryPage 21
95-
// decadePage 205
96-
// yearPage 2041
97-
// monthPage 24487
98-
9988
return (
10089
<div className={`${NAME_SPACE}__wrapper`}>
10190
<div className={`${NAME_SPACE}__input-container`}>
@@ -116,7 +105,11 @@ function Container({ initValue = new Date(), onChange }: Iprops) {
116105
/>
117106
<div className={`${NAME_SPACE}__datepicker`}>
118107
{viewType === 'month' && (
119-
<ViewMonth monthPage={monthPage} setValue={setValue} />
108+
<ViewMonth
109+
monthPage={monthPage}
110+
value={value}
111+
setValue={setValue}
112+
/>
120113
)}
121114
{viewType === 'year' && (
122115
<ViewYear

package/src/components/view/Month.tsx

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,69 @@ import { getFormatDatetime } from '../../utils/datetime';
66

77
interface Iprops {
88
monthPage: number;
9+
value: Date;
910
setValue: (value: Date) => void;
1011
}
1112

12-
function ViewMonth({ monthPage, setValue }: Iprops) {
13+
function ViewMonth({ monthPage, value, setValue }: Iprops) {
1314
const year = Math.ceil(monthPage / 12);
1415
const month = monthPage % 12 || 12;
1516
const firstDay = new Date(year, month - 1, 1).getDay(); // 이달 1일의 요일
1617
const lastDateValue = new Date(year, month, 0); // 이달 말 일의 Date 객체
1718
const lastDate = lastDateValue.getDate(); // 이달 말 일
1819
const lastDay = lastDateValue.getDay(); // 이달 말 일의 요일
1920
const prevLastDate = new Date(year, month - 1, 0).getDate(); // 이전달의 말 일
21+
const formatedValue = getFormatDatetime(value, 'YYYY-MM-DD');
22+
23+
const renderDateButton = (
24+
date: number,
25+
thisValue: Date,
26+
classNameModifier = ''
27+
) => {
28+
const day = thisValue.getDay();
29+
const formatedThisValue = getFormatDatetime(thisValue, 'YYYY-MM-DD');
30+
31+
return (
32+
<button
33+
type="button"
34+
className={`${NAME_SPACE}__datepicker-button ${classNameModifier}`}
35+
key={date}
36+
title={formatedThisValue}
37+
data-day={day}
38+
data-active={formatedValue === formatedThisValue}
39+
onClick={() => setValue(thisValue)}
40+
>
41+
{date}
42+
</button>
43+
);
44+
};
2045

2146
return (
2247
<div className={`${NAME_SPACE}__month-view`}>
2348
{Array.apply(0, Array(firstDay)).map((x, i) => {
24-
// prevMonth
25-
2649
const date = prevLastDate - (firstDay - i - 1);
27-
const value = new Date(-1, monthPage + 22, date);
28-
const day = value.getDay();
29-
const title = getFormatDatetime(value, 'YYYY-MM-DD');
50+
const thisValue = new Date(-1, monthPage + 22, date);
3051

31-
return (
32-
<button
33-
type="button"
34-
className={`${NAME_SPACE}__datepicker-button ${NAME_SPACE}__neighbor-button`}
35-
key={i}
36-
title={title}
37-
data-day={day}
38-
onClick={() => setValue(value)}
39-
>
40-
{date}
41-
</button>
52+
return renderDateButton(
53+
date,
54+
thisValue,
55+
`${NAME_SPACE}__neighbor-button`
4256
);
4357
})}
4458
{Array.apply(0, Array(lastDate)).map((x, i) => {
45-
// thisMonth
4659
const date = i + 1;
47-
const value = new Date(-1, monthPage + 23, date);
48-
const day = value.getDay();
49-
const title = getFormatDatetime(value, 'YYYY-MM-DD');
60+
const thisValue = new Date(-1, monthPage + 23, date);
5061

51-
return (
52-
<button
53-
type="button"
54-
className={`${NAME_SPACE}__datepicker-button`}
55-
key={i}
56-
title={title}
57-
data-day={day}
58-
onClick={() => setValue(value)}
59-
>
60-
{date}
61-
</button>
62-
);
62+
return renderDateButton(date, thisValue);
6363
})}
6464
{Array.apply(0, Array(6 - lastDay)).map((x, i) => {
6565
const date = i + 1;
66-
const value = new Date(-1, monthPage + 24, date);
67-
const day = value.getDay();
68-
const title = getFormatDatetime(value, 'YYYY-MM-DD');
66+
const thisValue = new Date(-1, monthPage + 24, date);
6967

70-
return (
71-
<button
72-
type="button"
73-
className={`${NAME_SPACE}__datepicker-button ${NAME_SPACE}__neighbor-button`}
74-
key={i}
75-
title={title}
76-
data-day={day}
77-
onClick={() => setValue(value)}
78-
>
79-
{date}
80-
</button>
68+
return renderDateButton(
69+
date,
70+
thisValue,
71+
`${NAME_SPACE}__neighbor-button`
8172
);
8273
})}
8374
</div>

test/src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function App() {
1313
console.log('value', value);
1414
}}
1515
/>
16+
<Datepicker />
1617
</div>
1718
);
1819
}

0 commit comments

Comments
 (0)