1
- import moment from 'moment'
2
1
import YearDropdown from './year_dropdown'
3
2
import MonthDropdown from './month_dropdown'
4
3
import Month from './month'
5
4
import Time from './time'
6
5
import React from 'react'
7
6
import PropTypes from 'prop-types'
8
7
import classnames from 'classnames'
9
- import { isSameDay , allDaysDisabledBefore , allDaysDisabledAfter , getEffectiveMinDate , getEffectiveMaxDate } from './date_utils'
8
+ import {
9
+ getUTCOffset ,
10
+ newDateWithOffset ,
11
+ setMonth ,
12
+ getMonth ,
13
+ addMonths ,
14
+ subtractMonths ,
15
+ getStartOfWeek ,
16
+ getStartOfDate ,
17
+ addDays ,
18
+ cloneDate ,
19
+ formatDate ,
20
+ localizeDate ,
21
+ setYear ,
22
+ getYear ,
23
+ isBefore ,
24
+ isAfter ,
25
+ getLocaleData ,
26
+ getWeekdayShortInLocale ,
27
+ getWeekdayMinInLocale ,
28
+
29
+ isSameDay ,
30
+ allDaysDisabledBefore ,
31
+ allDaysDisabledAfter ,
32
+ getEffectiveMinDate ,
33
+ getEffectiveMaxDate
34
+ } from './date_utils'
10
35
11
36
const DROPDOWN_FOCUS_CLASSNAMES = [
12
37
'react-datepicker__year-select' ,
@@ -75,7 +100,7 @@ export default class Calendar extends React.Component {
75
100
static get defaultProps ( ) {
76
101
return {
77
102
onDropdownFocus : ( ) => { } ,
78
- utcOffset : moment . utc ( ) . utcOffset ( ) ,
103
+ utcOffset : getUTCOffset ( ) ,
79
104
monthsShown : 1 ,
80
105
forceShowMonthNavigation : false
81
106
}
@@ -84,7 +109,7 @@ export default class Calendar extends React.Component {
84
109
constructor ( props ) {
85
110
super ( props )
86
111
this . state = {
87
- date : this . localizeMoment ( this . getDateInView ( ) ) ,
112
+ date : this . localizeDate ( this . getDateInView ( ) ) ,
88
113
selectingDate : null ,
89
114
monthContainer : this . monthContainer
90
115
}
@@ -102,11 +127,11 @@ export default class Calendar extends React.Component {
102
127
componentWillReceiveProps ( nextProps ) {
103
128
if ( nextProps . preSelection && ! isSameDay ( nextProps . preSelection , this . props . preSelection ) ) {
104
129
this . setState ( {
105
- date : this . localizeMoment ( nextProps . preSelection )
130
+ date : this . localizeDate ( nextProps . preSelection )
106
131
} )
107
132
} else if ( nextProps . openToDate && ! isSameDay ( nextProps . openToDate , this . props . openToDate ) ) {
108
133
this . setState ( {
109
- date : this . localizeMoment ( nextProps . openToDate )
134
+ date : this . localizeDate ( nextProps . openToDate )
110
135
} )
111
136
}
112
137
}
@@ -125,31 +150,31 @@ export default class Calendar extends React.Component {
125
150
const { preSelection, selected, openToDate, utcOffset } = this . props
126
151
const minDate = getEffectiveMinDate ( this . props )
127
152
const maxDate = getEffectiveMaxDate ( this . props )
128
- const current = moment . utc ( ) . utcOffset ( utcOffset )
153
+ const current = newDateWithOffset ( utcOffset )
129
154
const initialDate = openToDate || selected || preSelection
130
155
if ( initialDate ) {
131
156
return initialDate
132
157
} else {
133
- if ( minDate && current . isBefore ( minDate ) ) {
158
+ if ( minDate && isBefore ( current , minDate ) ) {
134
159
return minDate
135
- } else if ( maxDate && current . isAfter ( maxDate ) ) {
160
+ } else if ( maxDate && isAfter ( current , maxDate ) ) {
136
161
return maxDate
137
162
}
138
163
}
139
164
return current
140
165
}
141
166
142
- localizeMoment = date => date . clone ( ) . locale ( this . props . locale || moment . locale ( ) )
167
+ localizeDate = date => localizeDate ( date , this . props . locale )
143
168
144
169
increaseMonth = ( ) => {
145
170
this . setState ( {
146
- date : this . state . date . clone ( ) . add ( 1 , 'month' )
171
+ date : addMonths ( cloneDate ( this . state . date ) , 1 )
147
172
} , ( ) => this . handleMonthChange ( this . state . date ) )
148
173
}
149
174
150
175
decreaseMonth = ( ) => {
151
176
this . setState ( {
152
- date : this . state . date . clone ( ) . subtract ( 1 , 'month' )
177
+ date : subtractMonths ( cloneDate ( this . state . date ) , 1 )
153
178
} , ( ) => this . handleMonthChange ( this . state . date ) )
154
179
}
155
180
@@ -167,18 +192,18 @@ export default class Calendar extends React.Component {
167
192
168
193
changeYear = ( year ) => {
169
194
this . setState ( {
170
- date : this . state . date . clone ( ) . set ( 'year' , year )
195
+ date : setYear ( cloneDate ( this . state . date ) , year )
171
196
} )
172
197
}
173
198
174
199
changeMonth = ( month ) => {
175
200
this . setState ( {
176
- date : this . state . date . clone ( ) . set ( 'month' , month )
201
+ date : setMonth ( cloneDate ( this . state . date ) , month )
177
202
} , ( ) => this . handleMonthChange ( this . state . date ) )
178
203
}
179
204
180
205
header = ( date = this . state . date ) => {
181
- const startOfWeek = date . clone ( ) . startOf ( 'week' )
206
+ const startOfWeek = getStartOfWeek ( cloneDate ( date ) )
182
207
const dayNames = [ ]
183
208
if ( this . props . showWeekNumbers ) {
184
209
dayNames . push (
@@ -188,10 +213,11 @@ export default class Calendar extends React.Component {
188
213
)
189
214
}
190
215
return dayNames . concat ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] . map ( offset => {
191
- const day = startOfWeek . clone ( ) . add ( offset , 'days' )
216
+ const day = addDays ( cloneDate ( startOfWeek ) , offset )
217
+ const localeData = getLocaleData ( day )
192
218
const weekDayName = this . props . useWeekdaysShort
193
- ? day . localeData ( ) . weekdaysShort ( day )
194
- : day . localeData ( ) . weekdaysMin ( day )
219
+ ? getWeekdayShortInLocale ( localeData , day )
220
+ : getWeekdayMinInLocale ( localeData , day )
195
221
return (
196
222
< div key = { offset } className = "react-datepicker__day-name" >
197
223
{ weekDayName }
@@ -238,7 +264,7 @@ export default class Calendar extends React.Component {
238
264
}
239
265
return (
240
266
< div className = { classes . join ( ' ' ) } >
241
- { date . format ( this . props . dateFormat ) }
267
+ { formatDate ( date , this . props . dateFormat ) }
242
268
</ div >
243
269
)
244
270
}
@@ -253,7 +279,7 @@ export default class Calendar extends React.Component {
253
279
onChange = { this . changeYear }
254
280
minDate = { this . props . minDate }
255
281
maxDate = { this . props . maxDate }
256
- year = { this . state . date . year ( ) }
282
+ year = { getYear ( this . state . date ) }
257
283
scrollableYearDropdown = { this . props . scrollableYearDropdown }
258
284
yearDropdownItemNumber = { this . props . yearDropdownItemNumber } />
259
285
)
@@ -269,7 +295,7 @@ export default class Calendar extends React.Component {
269
295
locale = { this . props . locale }
270
296
dateFormat = { this . props . dateFormat }
271
297
onChange = { this . changeMonth }
272
- month = { this . state . date . month ( ) } />
298
+ month = { getMonth ( this . state . date ) } />
273
299
)
274
300
}
275
301
@@ -280,7 +306,7 @@ export default class Calendar extends React.Component {
280
306
return (
281
307
< div
282
308
className = "react-datepicker__today-button"
283
- onClick = { e => this . props . onSelect ( moment . utc ( ) . utcOffset ( this . props . utcOffset ) . startOf ( 'date' ) , e ) } >
309
+ onClick = { e => this . props . onSelect ( getStartOfDate ( newDateWithOffset ( this . props . utcOffset ) ) , e ) } >
284
310
{ this . props . todayButton }
285
311
</ div >
286
312
)
@@ -289,7 +315,7 @@ export default class Calendar extends React.Component {
289
315
renderMonths = ( ) => {
290
316
var monthList = [ ]
291
317
for ( var i = 0 ; i < this . props . monthsShown ; ++ i ) {
292
- var monthDate = this . state . date . clone ( ) . add ( i , 'M' )
318
+ var monthDate = addMonths ( cloneDate ( this . state . date ) , i )
293
319
var monthKey = `month-${ i } `
294
320
monthList . push (
295
321
< div key = { monthKey } ref = { div => { this . monthContainer = div } } className = "react-datepicker__month-container" >
0 commit comments