@@ -6,78 +6,69 @@ import { getFormatDatetime } from '../../utils/datetime';
66
77interface 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 >
0 commit comments