-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicketDatePicker.tsx
More file actions
167 lines (155 loc) · 5.79 KB
/
TicketDatePicker.tsx
File metadata and controls
167 lines (155 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { useEffect, useState } from 'react';
import DatePicker from 'react-datepicker';
import { ko } from 'date-fns/locale';
import 'react-datepicker/dist/react-datepicker.css';
import { TicketState } from '../model/TicketContext';
import { formatDateLocalString } from '../../../shared/lib/date';
interface DatePickerProps {
className?: string;
ticketState?: TicketState['ticketState'];
setTicketState?: React.Dispatch<React.SetStateAction<TicketState['ticketState']>>;
isLabel?: boolean;
onDateChange: (dates: { startDate: string; endDate: string; startTime: string; endTime: string }) => void;
}
const TicketDatePicker = ({
className,
ticketState,
setTicketState,
isLabel = false,
onDateChange,
}: DatePickerProps) => {
const [startDate, setStartDate] = useState<Date | null>(
ticketState?.startDate ? new Date(ticketState.startDate) : new Date()
);
const [endDate, setEndDate] = useState<Date | null>(
ticketState?.endDate ? new Date(ticketState.endDate) : new Date()
);
const [startTime, setStartTime] = useState<string>('06:00');
const [endTime, setEndTime] = useState<string>('23:00');
const generateTimeOptions = () => {
const options = [];
for (let i = 0; i < 24; i++) {
for (let j = 0; j < 4; j++) {
const hour = i.toString().padStart(2, '0');
const minute = (j * 15).toString().padStart(2, '0');
options.push(`${hour}:${minute}`);
}
}
return options;
};
const timeOptions = generateTimeOptions();
useEffect(() => {
const newStartDate = startDate ? formatDateLocalString(startDate, startTime) : '';
const newEndDate = endDate ? formatDateLocalString(endDate, endTime) : '';
if (setTicketState) {
setTicketState(prev => ({
...prev,
startDate: newStartDate,
endDate: newEndDate,
}));
}
onDateChange?.({
startDate: newStartDate,
endDate: newEndDate,
startTime,
endTime,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [startDate, endDate, startTime, endTime]);
return (
<div className={`flex flex-col w-full ${className}`}>
<div className="flex flex-wrap lg:flex-nowrap items-center justify-between gap-2">
<div className="flex flex-col w-full sm:w-auto gap-2">
{!isLabel && <span className="text-sm font-medium">시작 날짜</span>}
<div className="flex gap-1">
<DatePicker
id="startDate"
selected={startDate}
onChange={(date: Date | null) => setStartDate(date)}
locale={ko}
dateFormat="MM월 dd일"
className="w-20 h-9 md:w-24 md:h-10 border border-placeholderText text-sm md:text-md rounded-[5px] p-2"
renderCustomHeader={({
date,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div className="flex justify-center gap-4">
<button onClick={decreaseMonth} disabled={prevMonthButtonDisabled} className="mb-1">
<
</button>
<span>
{date.getFullYear()}년 {date.getMonth() + 1}월
</span>
<button onClick={increaseMonth} disabled={nextMonthButtonDisabled} className="mb-1">
>
</button>
</div>
)}
/>
<select
id="startTime"
value={startTime}
onChange={e => setStartTime(e.target.value)}
className="w-20 h-9 md:w-24 md:h-10 border border-placeholderText text-sm md:text-md rounded-[5px] p-2"
>
{timeOptions.map(time => (
<option key={time} value={time}>
{time}
</option>
))}
</select>
</div>
</div>
{isLabel && <span className="text-2xl hidden lg:inline">></span>}
<div className="flex flex-col w-full sm:w-auto gap-2">
{!isLabel && <span className="text-sm font-medium">종료 날짜</span>}
<div className="flex gap-1">
<DatePicker
id="endDate"
selected={endDate}
onChange={(date: Date | null) => setEndDate(date)}
locale={ko}
dateFormat="MM월 dd일"
className="w-20 h-9 md:w-24 md:h-10 border border-placeholderText text-sm md:text-md rounded-[5px] p-2"
renderCustomHeader={({
date,
decreaseMonth,
increaseMonth,
prevMonthButtonDisabled,
nextMonthButtonDisabled,
}) => (
<div className="flex justify-center gap-4">
<button onClick={decreaseMonth} disabled={prevMonthButtonDisabled} className="mb-1">
<
</button>
<span>
{date.getFullYear()}년 {date.getMonth() + 1}월
</span>
<button onClick={increaseMonth} disabled={nextMonthButtonDisabled} className="mb-1">
>
</button>
</div>
)}
/>
<select
id="endTime"
value={endTime}
onChange={e => setEndTime(e.target.value)}
className="w-20 h-9 md:w-24 md:h-10 border border-placeholderText text-sm md:text-md rounded-[5px] p-2"
>
{timeOptions.map(time => (
<option key={time} value={time}>
{time}
</option>
))}
</select>
</div>
</div>
</div>
</div>
);
};
export default TicketDatePicker;