@@ -5,7 +5,7 @@ import Calendar from "../components/Calendar";
5
5
import Footer from "../components/Footer" ;
6
6
import Input from "../components/Input" ;
7
7
import Shortcuts from "../components/Shortcuts" ;
8
- import { COLORS , DEFAULT_COLOR } from "../constants" ;
8
+ import { COLORS , DATE_FORMAT , DEFAULT_COLOR , LANGUAGE } from "../constants" ;
9
9
import DatepickerContext from "../contexts/DatepickerContext" ;
10
10
import { formatDate , nextMonth , previousMonth } from "../helpers" ;
11
11
import useOnClickOutside from "../hooks" ;
@@ -66,13 +66,13 @@ const Datepicker: React.FC<Props> = ({
66
66
placeholder = null ,
67
67
separator = "~" ,
68
68
startFrom = null ,
69
- i18n = "en" ,
69
+ i18n = LANGUAGE ,
70
70
disabled = false ,
71
71
inputClassName = null ,
72
72
containerClassName = null ,
73
73
toggleClassName = null ,
74
74
toggleIcon = undefined ,
75
- displayFormat = "YYYY-MM-DD" ,
75
+ displayFormat = DATE_FORMAT ,
76
76
readOnly = false ,
77
77
minDate = null ,
78
78
maxDate = null ,
@@ -91,14 +91,13 @@ const Datepicker: React.FC<Props> = ({
91
91
const [ firstDate , setFirstDate ] = useState < dayjs . Dayjs > (
92
92
startFrom && dayjs ( startFrom ) . isValid ( ) ? dayjs ( startFrom ) : dayjs ( )
93
93
) ;
94
+ const [ secondDate , setSecondDate ] = useState < dayjs . Dayjs > ( nextMonth ( firstDate ) ) ;
94
95
const [ period , setPeriod ] = useState < Period > ( {
95
96
start : null ,
96
97
end : null
97
98
} ) ;
98
- const [ secondDate , setSecondDate ] = useState < dayjs . Dayjs > ( nextMonth ( firstDate ) ) ;
99
99
const [ dayHover , setDayHover ] = useState < string | null > ( null ) ;
100
100
const [ inputText , setInputText ] = useState < string > ( "" ) ;
101
-
102
101
const [ inputRef , setInputRef ] = useState ( React . createRef < HTMLInputElement > ( ) ) ;
103
102
104
103
// Custom Hooks use
@@ -133,6 +132,7 @@ const Datepicker: React.FC<Props> = ({
133
132
}
134
133
} , [ ] ) ;
135
134
135
+ /* Start First */
136
136
const firstGotoDate = useCallback (
137
137
( date : dayjs . Dayjs ) => {
138
138
const newDate = dayjs ( formatDate ( date ) ) ;
@@ -153,6 +153,22 @@ const Datepicker: React.FC<Props> = ({
153
153
firstGotoDate ( nextMonth ( firstDate ) ) ;
154
154
} , [ firstDate , firstGotoDate ] ) ;
155
155
156
+ const changeFirstMonth = useCallback (
157
+ ( month : number ) => {
158
+ firstGotoDate ( dayjs ( `${ firstDate . year ( ) } -${ month < 10 ? "0" : "" } ${ month } -01` ) ) ;
159
+ } ,
160
+ [ firstDate , firstGotoDate ]
161
+ ) ;
162
+
163
+ const changeFirstYear = useCallback (
164
+ ( year : number ) => {
165
+ firstGotoDate ( dayjs ( `${ year } -${ firstDate . month ( ) + 1 } -01` ) ) ;
166
+ } ,
167
+ [ firstDate , firstGotoDate ]
168
+ ) ;
169
+ /* End First */
170
+
171
+ /* Start Second */
156
172
const secondGotoDate = useCallback (
157
173
( date : dayjs . Dayjs ) => {
158
174
const newDate = dayjs ( formatDate ( date , displayFormat ) ) ;
@@ -173,33 +189,20 @@ const Datepicker: React.FC<Props> = ({
173
189
setSecondDate ( nextMonth ( secondDate ) ) ;
174
190
} , [ secondDate ] ) ;
175
191
176
- const changeFirstMonth = useCallback (
177
- ( month : number ) => {
178
- firstGotoDate ( dayjs ( `${ firstDate . year ( ) } -${ month < 10 ? "0" : "" } ${ month } -01` ) ) ;
179
- } ,
180
- [ firstDate , firstGotoDate ]
181
- ) ;
182
-
183
192
const changeSecondMonth = useCallback (
184
193
( month : number ) => {
185
194
secondGotoDate ( dayjs ( `${ secondDate . year ( ) } -${ month < 10 ? "0" : "" } ${ month } -01` ) ) ;
186
195
} ,
187
196
[ secondDate , secondGotoDate ]
188
197
) ;
189
198
190
- const changeFirstYear = useCallback (
191
- ( year : number ) => {
192
- firstGotoDate ( dayjs ( `${ year } -${ firstDate . month ( ) + 1 } -01` ) ) ;
193
- } ,
194
- [ firstDate , firstGotoDate ]
195
- ) ;
196
-
197
199
const changeSecondYear = useCallback (
198
200
( year : number ) => {
199
201
secondGotoDate ( dayjs ( `${ year } -${ secondDate . month ( ) + 1 } -01` ) ) ;
200
202
} ,
201
203
[ secondDate , secondGotoDate ]
202
204
) ;
205
+ /* End Second */
203
206
204
207
// UseEffects & UseLayoutEffect
205
208
useEffect ( ( ) => {
@@ -251,7 +254,7 @@ const Datepicker: React.FC<Props> = ({
251
254
252
255
useEffect ( ( ) => {
253
256
if ( startFrom && dayjs ( startFrom ) . isValid ( ) ) {
254
- if ( value != null && value . startDate != null ) {
257
+ if ( value ? .startDate != null ) {
255
258
setFirstDate ( dayjs ( value . startDate ) ) ;
256
259
setSecondDate ( nextMonth ( dayjs ( value . startDate ) ) ) ;
257
260
} else {
@@ -261,7 +264,7 @@ const Datepicker: React.FC<Props> = ({
261
264
}
262
265
} , [ startFrom , value ] ) ;
263
266
264
- // Variable
267
+ // Variables
265
268
const colorPrimary = useMemo ( ( ) => {
266
269
if ( COLORS . includes ( primaryColor ) ) {
267
270
return primaryColor ;
0 commit comments