|
1 | | -import { useCalendarComponent } from '../lib'; |
2 | | -import { getWeekDayList } from '../lib/utils/locale'; |
| 1 | +import { useEffect } from 'react'; |
| 2 | + |
| 3 | +import useCalendarComponent from '../lib'; |
| 4 | +import { getWeekDayList, getMonthList } from '../lib/utils/locale'; |
| 5 | +import LeftArrow from './components/LeftArrow'; |
| 6 | +import RightArrow from './components/RightArrow'; |
3 | 7 |
|
4 | 8 | const weekDayList = getWeekDayList(); |
| 9 | +const monthList = getMonthList(); |
5 | 10 |
|
6 | 11 | function App() { |
7 | 12 | const { |
8 | 13 | displayedYear, |
9 | 14 | displayedMonth, |
10 | | - addMonth, |
11 | | - addYear, |
| 15 | + selectedDate, |
| 16 | + changeDisplayedMonth, |
| 17 | + changeDisplayedYear, |
12 | 18 | getDateCellInfos, |
13 | | - setSelectedDate, |
14 | 19 | } = useCalendarComponent(); |
15 | 20 |
|
| 21 | + useEffect(() => { |
| 22 | + const { year, month, dayOfMonth } = selectedDate; |
| 23 | + console.log(new Date(year, month, dayOfMonth).toLocaleString()); |
| 24 | + }, [selectedDate]); |
| 25 | + |
16 | 26 | const dateCells = getDateCellInfos().map( |
17 | | - ({ key, year, month, monthStatus, dayOfMonth, isToday, isSelected }) => { |
18 | | - let className = 'c-day-container'; |
19 | | - if (monthStatus !== 'current') className = `${className} disabled`; |
20 | | - if (isToday) className = `${className} highlight`; |
21 | | - if (isSelected) className = `${className} highlight-green`; |
| 27 | + ({ key, monthStatus, dayOfMonth, isToday, isSelected, selectThisDate }) => { |
| 28 | + let className = 'rounded-full'; |
| 29 | + if (monthStatus !== 'current') className = `${className} text-slate-300`; |
| 30 | + if (isSelected) className = `${className} bg-green-200`; |
| 31 | + if (isToday && !isSelected) className = `${className} bg-amber-200`; |
22 | 32 |
|
23 | 33 | return ( |
24 | | - <div |
| 34 | + <button |
25 | 35 | key={key} |
26 | 36 | className={className} |
27 | 37 | onClick={() => { |
28 | | - setSelectedDate( |
29 | | - { |
30 | | - year, |
31 | | - month, |
32 | | - dayOfMonth, |
33 | | - }, |
34 | | - { shouldChangePanel: true } |
35 | | - ); |
| 38 | + selectThisDate({ changeDisplayedValues: true }); |
36 | 39 | }}> |
37 | | - <div className='cdc-day'> |
| 40 | + <div className='flex items-center justify-center'> |
38 | 41 | <span>{dayOfMonth}</span> |
39 | 42 | </div> |
40 | | - </div> |
| 43 | + </button> |
41 | 44 | ); |
42 | 45 | } |
43 | 46 | ); |
44 | 47 |
|
45 | 48 | return ( |
46 | | - <div className='mdp-container'> |
47 | | - <div className='mdpc-head'> |
48 | | - <div className='mdpch-button'> |
49 | | - <div className='mdpchb-inner' onClick={() => addYear(-1)}> |
50 | | - <span className='mdpchbi-left-arrows'></span> |
51 | | - </div> |
52 | | - </div> |
53 | | - <div className='mdpch-button'> |
54 | | - <div className='mdpchb-inner' onClick={() => addMonth(-1)}> |
55 | | - <span className='mdpchbi-left-arrow'></span> |
56 | | - </div> |
57 | | - </div> |
58 | | - <div className='mdpch-container'> |
59 | | - <div className='mdpchc-year'>{displayedYear}</div> |
60 | | - <div className='mdpchc-month'>{displayedMonth + 1}</div> |
61 | | - </div> |
62 | | - <div className='mdpch-button'> |
63 | | - <div className='mdpchb-inner' onClick={() => addMonth(1)}> |
64 | | - <span className='mdpchbi-right-arrow'></span> |
65 | | - </div> |
66 | | - </div> |
67 | | - <div className='mdpch-button' onClick={() => addYear(1)}> |
68 | | - <div className='mdpchb-inner'> |
69 | | - <span className='mdpchbi-right-arrows'></span> |
70 | | - </div> |
| 49 | + <div className='bg-white shadow-2xl rounded-3xl p-8 w-[500px]'> |
| 50 | + <div className='flex justify-between items-center'> |
| 51 | + <LeftArrow arrowAmount={2} onClick={() => changeDisplayedYear(-1)} /> |
| 52 | + <LeftArrow onClick={() => changeDisplayedMonth(-1)} /> |
| 53 | + <div className='flex flex-col justify-center items-center'> |
| 54 | + <span className='text-lg'>{displayedYear}</span> |
| 55 | + <span className='text-sm'>{monthList[displayedMonth]}</span> |
71 | 56 | </div> |
| 57 | + <RightArrow onClick={() => changeDisplayedMonth(1)} /> |
| 58 | + <RightArrow arrowAmount={2} onClick={() => changeDisplayedYear(1)} /> |
72 | 59 | </div> |
73 | | - <div className='mdpc-body'> |
74 | | - <div className='c-container'> |
75 | | - <div className='cc-head'> |
76 | | - {weekDayList.map((d, i) => ( |
77 | | - <div key={i} className='cch-name'> |
78 | | - {d} |
79 | | - </div> |
80 | | - ))} |
81 | | - </div> |
82 | | - <div className='cc-body'>{dateCells}</div> |
| 60 | + <div className='mt-5'> |
| 61 | + <div className='grid grid-cols-7 mb-4'> |
| 62 | + {weekDayList.map((d, i) => ( |
| 63 | + <div key={i} className='flex items-center justify-center'> |
| 64 | + {d} |
| 65 | + </div> |
| 66 | + ))} |
83 | 67 | </div> |
| 68 | + <div className='grid grid-cols-7 gap-y-4'>{dateCells}</div> |
84 | 69 | </div> |
85 | 70 | </div> |
86 | 71 | ); |
|
0 commit comments